Execute table2 query when it has null value in table1 - sql

I have two tables called warehouse table and order table.
Warehouse table (table1)
Customer orderno
AA 111
BB 222
Order table (table2)
Customer orderno status
AA 111 1
BB 222 2
CC 333 3
DD 444 4
My requirement is to show if any order is not available in table1 then it should select from table2 with status of (1,2,4)
I am trying with union but the results are not showing as expected.
select customer,orderNo from table1
union
select customer,orderno from table2
where status IN (1,2,4) ```
Can you please help me on this.

If you want orders with status of (1,2,4) then use below query
select t2.*
from table2 t2
where not exists (select 1 from table1 t1 where t1.orderno = t2.orderno)
and t2.status IN (1,2,4)
order by status;

you can union the tables, prioritize the results and then get the one with the higher priority for each order.
select Customer, orderno
from (
select Customer, orderno, ROW_NUMBER() over(partition by Customer, orderno order by Priority asc) as rn
from (
select Customer, orderno, 1 as Priority from #table1
union all
select Customer, orderno, 2 as Priority from #table2 where status IN (1,2,4)) o
) n
where n.rn = 1

Related

Count on UNION in Oracle

I have 3 table and I need to get the details from 2 tables where the count of UNION is greater than 1.But need to apply certain conditions as well
Table A
id entity_id name category
1 45 abcd win_1
2 46 efgh win_2
3 47 efgh1 win_2
4 48 dfgh win_5
5 49 adfgh win_4
Table B
id product_id name parent_id
1 P123 asdf win_1
2 P234 adfgh win_4
Table 3 category_list
id cat_id name
1 win_1 Households
2 win_2 Outdoors
3 win_3 Mixed
4 win_4 Omni
Now I need to have the count of UNION from Table A and Table B where they have count of cat_id greater than 1 and Table A.name != Table B.name
The result which I require is
p_id name cat_id
45 abcd win_1
P123 asdf win_1
46 efgh win_2
47 efgh1 win_2
win_5 is excluded as the count is one and win_4 should be excluded as name in Table A nd B is same.
I have run out of Ideas as i am relatively new to Oracle and DB.Any help is appreciated.
I think you can use exists to ensure that the cat_id is present in both tables
select entity_id as p_id, name, category as cat_id
from table_a a
where exists (select null from table_b where a.category = table_b.parent_id)
union
select entity_id, name, parent_id
from table_b b
where exists (select null from table_a where b.parent_id = table_a.category)
I believe you are looking for something like this -
Select T2.*
from
(Select category
from
(Select name, category from TableA
Union all
Select name, parent_id as category from TableB) t
group by category
having count(distinct name) > 1) T1
Join
(Select entity_id as Pid, name, category from TableA
Union
Select product_id as Pid, name, parent_id as category from TableB) T2
ON T1.category = T2.category;
Would you try this code.
First CTE (Common Table Expression) "list_union" gets the records for each table those have different names then makes the union. with the second CTE "list_cnt" counts the categories and finally gets the result cnt>1 with the last select statement as you pictured.
With
list_union AS (
SELECT
id,
----------
TO_CHAR(entity_id) entity_id,
----------
name,
category
FROM table_A a
WHERE NOT EXISTS(SELECT 1 FROM table_B b WHERE a.name=b.name)
----------
UNION ALL
----------
SELECT
id,
product_id,
name,
parent_id
FROM table_B b
WHERE NOT EXISTS(SELECT 1 FROM table_A a WHERE a.name=b.name)
)
,list_cnt AS (
SELECT
l.*,
----------
COUNT(*) over (PARTITION BY category) cnt
----------
FROM list_union l
)
SELECT
entity_id AS p_id,
name,
category AS cat_id
FROM list_cnt
WHERE cnt>1
ORDER BY cat_id ASC, p_id ASC
;
Just use a union all and window functions:
select ab.*
from (select ab.*,
count(distinct name) over (partition by category) as cnt
from ((select a.* from a
) union all
(select b.* from b
)
) ab
) ab
where cnt > 1;
Although you describe the problem as:
Now I need to have the count of UNION from Table A and Table B where they have count of cat_id greater than 1 and Table A.name != Table B.name
You seem to just want cat_ids that have different names across the two tables. Your sample data includes cat_id = 'win_2', which is not even in the second table.

SQL select all rows on two joined tables

I have two tables with the data of
Id Amt
1 10
2 10
Tbl2
Id Amt
1 10
I want a query that will result to this
Id Amt
1 20
2 10
I tried different joins but no luck.
Thanks!
The safest way to do this is with UNION ALL:
SELECT ID, SUM(AMT) AS AMT FROM (
SELECT ID, AMT FROM TABLE1
UNION ALL
SELECT ID, AMT FROM TABLE2
) GROUP BY ID;
By the way you can do also with a FULL OUTER JOIN being careful to group by ID:
SELECT
NVL(T1.ID, T2.ID) AS ID,
NVL(T1.AMT, 0) + NVL(T2.AMT, 0) AS AMT
FROM
(SELECT ID, SUM(AMT) AS AMT FROM TABLE1 GROUP BY ID) T1
FULL OUTER JOIN
(SELECT ID, SUM(AMT) AS AMT FROM TABLE2 GROUP BY ID) T2
ON (T1.ID = T2.ID);
Use left join:
select tbl1.id, tbl1.amt + coalesce(tbl2.amt, 0)
from tbl1 left join
tbl2
on tbl1.id = tbl2.id

Add values from count that have come from a UNION

I am attempting to add together the values_occurrence for the id's that match. For example ID number 1 has 3 and another record shows ID number 1 having 4. I want to ADD 3 + 4 based on the ID matching.
I am trying to see which ID has the most entries in each table and then add them together.
ID value_occurrence
--------------------
1 3
1 4
so far this is what I have.
SELECT ID, COUNT(ID) AS value_occurrence
FROM TABLE1
GROUP BY ID
UNION
SELECT ID, COUNT(ID) AS value_occurrence
FROM TABLE2
GROUP BY ID
ORDER BY ID ASC;
Any help would be appreciated.
Do a union all and then aggregate:
select id, count(*) as total_cnt, sum(t1) as t1_cnt, sum(t2) as t2_cnt
from ((select id, 1 as t1, 0 as t2 from table1) union all
(select id, 0, 1 from table2)
) t
group by id
order by id;

Please assist me with creating SQL Script

I am trying to join following this two table and get the sum of id #2 from table 1 and join it with table 2
TABLE # 1
customer id amount
Nick 1 150
Jack 2 100
Jack 2 130
TABLE # 2
product Typ Date id
Apple Fruit 2/19/15 1
Banana Fruit 2/19/15 2
The result I would like to see is amount get picked up for id # 2.
customer id amount product Typ Date id
Nick 1 150 Apple Fruit 2/19/15 1
Jack 2 230 Banana Fruit 2/19/15 2
Hope it makes sense,
First find the Sum of amount per customer.
Then join the result to the table2 using ID to get the result. Try this.
select * from table2 A
(
select sum(amount) amount,customer, id
from table1
group by customer, id
) B on A.Id =B.Id
Instead of Group by we can use ROW_NUMBER() and PARTITION BY.
DECLARE #TABLE1 TABLE(Customer varchar(100), Id int, Amount int)
INSERT INTO #TABLE1
SELECT 'Nick', 1, 150 UNION
SELECT 'Jack', 2, 100 UNION
SELECT 'Jack', 2, 130
DECLARE #TABLE2 TABLE(Product varchar(100), Typ Varchar(20), [Date] date, Id int)
INSERT INTO #TABLE2
SELECT 'Apple', 'Fruit', '2/19/15', 1 UNION
SELECT 'Banana', 'Fruit', '2/19/15', 2
SELECT * FROM
(
SELECT Customer, id, SUM(Amount) OVER (PARTITION BY Id) Amount,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) RN
FROM #TABLE1
)
T1
JOIN #TABLE2 T2 ON T1.Id = T2.Id
WHERE RN = 1

Select rows not in another table by comparing two table

I have following two tables TableA and TableB
TableA
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 3 1 200
4 1 2 75
5 2 2 100
6 1 3 400
7 2 3 200
TableB
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 1 2 75
I want to compare Month_Id Customer_Id Total_Amount in both tables and select Id from TableA. The output should be as follow.
Output
Id
3
5
6
7
My concept is:
SELECT TableA.Id FROM TableA
WHERE TableA.Month_Id <> TableB.MonthId AND
TableA.Customer_Id <> TableB.Customer_Id AND
TableA.Total_Amount <> TableB.Total_Amount
SELECT TableA.Id
FROM TableA
WHERE NOT EXISTS (
SELECT 1
FROM TableB
WHERE TableB.Month_Id = TableA.Month_Id
AND TableB.Customer_Id = TableA.Customer_Id
AND TableB.Total_Amount = TableA.Total_Amount
)
select Id
from (
select Id, Month_Id, Customer_Id, Total_Amount from TableA
except
select Id, Month_Id, Customer_Id, Total_Amount from TableB
) q
SELECT id FROM
(SELECT id, month_id, customer_id, total_ammount FROM TableA
EXCEPT
SELECT id, month_id, customer_id, total_ammount FROM TableB);
You can use the EXCEPT set operator:
SELECT id
FROM (SELECT * FROM table_a
EXCEPT
SELECT * FROM table_b) t
You can use Merge with WHEN NOT MATCHED
place your condition in ON <merge_search_condition>
SELECT Id FROM TableA A LEFT JOIN tableB B
ON A.Id=B.Id AND A.Month_Id =B.Month_Id
AND A.Customer_Id =B.Customer_Id
AND A.Total_Amount=b.Total_Amount
WHERE B.Id is NULL
In oracle sql it would be:
SELECT ID FROM
(SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_A
MINUS
SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_B);
Is this what you want?
(Not sure of MINUS operator in sql-server though)