I have a table with columns id, product_type, product_id, quantity all as integer.
I need select data from another tables depending on product_type. For example. If product_type is 0 then select from tableA, if product_type is 1 then select from tableB etc. I tryied to find solution how to create select but unsuccessfully. Can someone help me please. I appreciate every help. Thank you.
Join the master table with the individual product tables, but use a left join and include the product_type = x filter in the join condition so that only the desired records are actually joined.
This will result in many NULL values; use coalesce to get a non-NULL value for the output:
SELECT sales.id,
sales.quantity,
sales.product_type,
coalesce(tableA.name, tableB.name) AS name,
coalesce(tableA.color, tableB.color) AS color,
tableA.numberOfAs -- is NULL for other product types
FROM sales
LEFT JOIN tableA ON sales.product_type = 0 AND
sales.product_id = tableA.product_id
LEFT JOIN tableB ON sales.product_type = 1 AND
sales.product_id = tableB.product_id
WHERE ...
Sounds like you need a CASE statement
SELECT
CASE
WHEN product_type = 1 THEN (SELECT column FROM table1 WHERE ...)
WHEN product_type = 2 THEN (SELECT column FROM table2 WHERE ...)
END
FROM table
Probably could be made more efficient by using JOINS but it would depend on your schema.
Related
I have below mentioned two data sets, Table A and Table B, and I am trying to get to Table C as output dataset using Table A and TableB, can you help me with SQL query to come up wit this out put. I am mainly trying to calculate DueAmount column in TableC, and logic to derive this column is mentioned in Calculation column
Data Screenshot:
TableA, TableB and Output screenshot
I thought about trying the logic in which Table A can be expanded to multiple row for each period , and then join TableA with TableB, but I am looking for some logic which will be more efficient for large number of ID's.
The below will generate the results desired:
SELECT p.PeriodId, CASE WHEN b.BAmt < a.AAmt THEN 0 ELSE b.BAmt - a.AAmt END AS DueAmt
FROM (SELECT PeriodId FROM TableA UNION SELECT PeriodId FROM TableB) p
CROSS APPLY (SELECT SUM(Amount) AS AAmt FROM TableA WHERE PeriodId <= p.PeriodId) a
CROSS APPLY (SELECT SUM(Amount) AS BAmt FROM TableB WHERE PeriodId <= p.PeriodId) b
I have two big tables that are very similar to each other and are made of 6 left joins. The only difference between them is in the first table to which the others are left joined, otherwise the main select clause and the rest of the tables are the same.
A simple example would be:
Create table A as
Select a.attr, b.attr, ...
From
(Select attr
From table a
Where cond1, cond2, cond3) a
Left join
(Select attr
From table) b
on a.whatever = b.whatever
Left join ...;
Create table B as
Select a.attr, b.attr ...
From
(Select attr
From table a
Where cond1) a
Left join
(Select attr
From table) b
on a.whatever = b.whatever
Left join...;
I hope this is clear. The only difference is the where conditions of table 'a' to which everything else is joined. How could I optimize this so I don't have to write two almost identical queries?
Maybe you can get rid of the restrictions of table a in first, get the result, add it when use
I need to use multiple table selections in a query in SQL. But how to reference a table selected within a query?
for example: (pseudo code)
create table C as
select distinct id, product_code
from (
select distinct id, product_code
from A where dt = '2019-06-01'
)
inner join B on (select distinct id, product_code
from A where dt='2019-06-01').id = B.id;
the code above might be wrong, but the point is that the table A could not be used directly since it's too large and it has to be specified that dt is some specific value. (so I need to select something from A for double times above). And I need to inner join the smaller A' with some other table B.
Is it possible, say, "define" that table A_ = select distinct blabla...from A ... and then join A_ with B within a query?
thanks,
You just want a table alias:
select distinct id, product_code
from (select distinct id, product_code
from table_A
where dt = '2019-06-01'
) a inner join
table_B b
on a.id = B.id;
I need help with this select statement in my relational table
(both supplier and products are composite primary key which uniquely identified each rows
Supplier Products
ABC Toys
ABC Snacks
ZXC Snacks
ZXC Food
QWE Toys
ABC Food
I need to find the supplier that does not supply toys
so i shoud only get ZXC
I try the following but it give me ABC, ZXC
select distinct Supplier
from table
where NOT (Products ='Toys');
(I am using oracle) how should my query be? Thanks
select distinct supplier
from table
where supplier not in (select supplier from table where products = 'Toys')
You can group by Supplier and use having clause evaluated to true if conditional count is equal to 0 (no matches within group):
select Supplier
from table
group by Supplier
having count(case when Products = 'Toys' then Products end) = 0
Another way to do it with NOT EXISTS
select distinct
supplier
from
table t1
where
not exists (select * from table t2 where t1.supplier = t2.supplier and t2.products = 'Toys')
Lots of ways to do this here's a couple. I've found exists and not exists to generally be the fastest but it always depends on indexes and your system. So look at execution plan for best use in your environment.
using a left join
SELECT T.SUPPLIER
FROM TABLE T
LEFT JOIN TABLE T2
on T.Supplier = T2.Supplier
and T2.Products = 'Toys'
where T2.Products is null
Using not exists.
SELECT supplier
FROM table T
WHERE NOT EXISTS (SELECT 1
FROM Table A
WHERE T.Supplier = A.Supplier and Products = 'Toys')
We have two tables table1 and table2
Table 1
Table 2
We need the resultant table as :
All this should be done in a single SQL query
Thanks in advance.
I think you can make it without the second query, I tested it and returned your expected values.
select table_2.id_pro,
product_name,
SUM(1) as Quantity,
priceprod,
SUM(1) * priceprod as 'quantity * priceprod'
from Table_2
inner join Table_1 t1 on table_2.id_pro = t1.id_pro
group by table_2.id_pro, product_name, priceprod
And my SqlFiddle test http://sqlfiddle.com/#!3/08c2ef/1
I believe this should be what you need, or fairly close anyway! You need to group up your results from your first table to get your quantity value and then join those results to your second table to be able to create your desired output.
SELECT t1.id_pro,
t2.product_name,
s.Quantity,
t2.priceperprod,
s.Quantity * t2.priceperprod
FROM table_2 t2
INNER JOIN (
SELECT COUNT(*) AS Quantity,
t.id_pro
FROM table_1 t
GROUP BY t.id_pro
) t1 ON t2.id_pro = t1.id_pro
i believe that this is correct, i just JOIN the two tables AND used group by then count how many records each group, i hope this will help you, thanks.
SELECT
A.id_pro,
A.product_name,
(count(*)) AS Quantity,
A.priceperprod,
(COUNT(*) * A.priceperprod) AS Total
FROM table_2 A
LEFT JOIN table_1 B
ON B.id_pro = A.id_pro
GROUP BY A.id_pro
Guess this should be helpful to you.
SELECT A.PRODUCT_CODE, A.PRODUCT_NAME, B.QUANTITY,
A.PRICE PRICE_PER_PRODUCT,
(B.QUANTITY * A.PRICE) TOTAL_PRICE FROM
TABLE2 A,
(SELECT X.PRODUCT, COUNT(X.PRODUCT) QUANTITY FROM TABLE1 AS X
GROUP BY X.PRODUCT) as B
WHERE A.PRODUCT_CODE = B.PRODUCT
ORDER BY A.PRODUCT_CODE
Am doing following :
Taking the entire TABLE2 aliased as A
Getting the products and its corresponding count using group by and aliasing it as B
Selecting corresponding fields from table_Aliases A and B provided A's ProductCode and B's Products are same.