I have 2 tables
table1
ID VendorID
100 11190
200 99999
table2
ID VendorID
100 11190
100 11190
200 12523
200 53266
My expect result and my goal if ID and VendorID from table1 match with ID and VendorID from table2 then flag NO
table1ID table1Vendor table2ID table2Vendor Code
100 12345 100 12345 No
100 12345 100 12345 No
100 12345 100 45678 No
200 56489 200 11111 Use
200 56489 200 22222 Use
My query
SELECT a.id as table1ID, a.vendorid as table1Vendor, b.id as table2ID, b.Vendorid as table2Vendor
, case
when a.vendorid <> b.Vendorid
then 'Use' else 'No'
end as Code
FROM table_1 a
JOIN table_2 b on a.id = b.id
But I got
table1ID table1Vendor table2ID table2Vendor Code
100 12345 100 12345 No
100 12345 100 12345 No
100 12345 100 45678 Use
200 56489 200 11111 Use
200 56489 200 22222 Use
You can see row 3 is incorrect, should code as NO cause 12345(table1vendor) still match with 12345(table2vendor)
Not sure why, need some help. Thank you.
You are only checking the same row for a match, but according to your logic you need to check all rows, therefore you need another sub-query.
SELECT a.ID AS table1ID, a.VendorID AS table1Vendor, b.ID AS table2ID, b.VendorID as table2Vendor
, CASE WHEN EXISTS (SELECT 1 FROM table_2 b1 WHERE b1.id = a.ID AND b1.VendorID = a.VendorID) THEN 'No' ELSE 'Use' END AS Code
FROM table_1 a
JOIN table_2 b ON a.ID = b.ID;
Related
I'm having a trouble writing a query in ORACLE. I have a Table that contains values. for example:
ID quantity partID
123 50 10
100 20 10
100 30 11
123 null 8
456 null 100
789 25 123
456 50 9
I want to get all rows that has same ID but quantities to be 50 and null (exact same pairs of 50 and null only). for the given example I would like to get:
ID quantity partID
123 50 10
123 null 8
456 50 9
456 null 100
I tried inner join but it doesn't provide the exact output as expected.
You may try :
select ID, quantity, partID
from tab
where ID in
(
select ID
from tab
where nvl(quantity,50)=50
group by ID
having count(distinct nvl(quantity,0) )>1
);
ID QUANTITY PARTID
123 50 10
123 (null) 8
456 (null) 100
456 50 9
SQL Fiddle Demo
P.S. you may get the same results by commenting out having count(ID)=2 also but for those cases there may not exist one of 50 or null for values of quantity.
You can use exists:
select t.*
from t
where (t.quantity = 50 and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value is null)
) or
(t.quantity is null and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value = 50)
) ;
I have a query and I want to look up the values from other table as a reference but not mess up my current query results. I think I have to use a Outer Left Join, but not sure how to incorporate that with my current query.
My current query looks similar to this:
SELECT a.primary_key,
a.phase,
b.project_number,
c.LENGTH,
d.color
FROM TableA a,
TableB b,
TableC c,
TableD d
WHERE c.primary_key = a.PROJECT_ID
AND b.primary_key = a.PROJECT_ID
AND b.primary_key = d.project_ID
AND (c.date IS NULL OR c.number IS NULL)
AND d.color IN ('black','red','blue')
ORDER BY 1
Now, that gives me a table of 50 results. 'TableContacts' has the look up value to my b.project_number. So say my table of 50 results, only 10 of them have b.project_number, I need the lookup values from 'TableContacts' to also show in my results, but I don't want that to affect my results and cut it down to 10, I still need my original 50 results, just with that additional information. Help?
Just add the CONTACTS table to your joins:
SELECT a.primary_key,
a.phase,
b.project_number,
c.LENGTH,
d.color,
ct.lookup_value --<< this is from the CONTACTS table
FROM TableA a
JOIN TableB b ON b.primary_key = a.PROJECT_ID
JOIN TableC c ON c.primary_key = a.PROJECT_ID
JOIN TableD d ON b.primary_key = d.project_ID
LEFT JOIN contacts ct ON ct.some_column = b.project_Number --<< this is the outer join to the CONTACTS table
WHERE (c.date IS NULL OR c.number IS NULL)
AND d.color IN ('black','red','blue')
ORDER BY 1
As you obfuscated your table and column names it's hard to guess how exactly the join condition on the CONTACTS table should look like.
You could use this approach:
Create a table with the results of your current query:
create table t1 as
SELECT a.primary_key,
a.phase,
b.project_number,
c.LENGTH_col,
d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
AND b.primary_key = a.PROJECT_ID
AND b.primary_key = d.project_ID
AND (c.date_col IS NULL OR c.number_col IS NULL)
AND d.color IN ('black','red','blue')
ORDER BY 1;
Use a union to get the desired results:
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;
Illustration by creating dummy data:
select * from a;
PRIMARY_KEY | PROJECT_ID | PHASE
1 100 Phase-1
2 200 Phase-2
3 300 Phase-3
4 400 Phase-4
5 500 Phase-5
select * from b;
PRIMARY_KEY | PROJECT_NUMBER
100 null
200 2000
300 3000
400 null
500 5000
select * from c;
PRIMARY_KEY | NUMBER_COL | LENGTH_COL | DATE_COL
100 null 99 null
200 null 99 null
300 null 99 null
400 null 99 null
500 null 99 null
select * from d;
PROJECT_ID | COLOR
100 black
200 red
300 blue
400 black
500 yellow
select * from TableContacts;
PROJECT_NUMBER | LOOKUP_COLUMN
1000 l-1000
2000 l-2000
3000 l-3000
4000 l-4000
5000 l-5000
Existing query in question returns this:
SELECT a.primary_key,
a.phase,
b.project_number,
c.LENGTH_col,
d.color
FROM a,b,c,d
WHERE c.primary_key = a.PROJECT_ID
AND b.primary_key = a.PROJECT_ID
AND b.primary_key = d.project_ID
AND (c.date_col IS NULL OR c.number_col IS NULL)
AND d.color IN ('black','red','blue')
ORDER BY 1;
PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR
1 Phase-1 null 99 black
2 Phase-2 2000 99 red
3 Phase-3 3000 99 blue
4 Phase-4 null 99 black
The goal is to populate the lookup_column where project_number is not null. Running the union query provided at start of answer:
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, TableContacts.lookup_column
from t1, TableContacts
where t1.project_number = TableContacts.project_number
UNION
select t1.primary_key, t1.phase, t1.project_number, t1.length_col, t1.color, null
from t1 where t1.project_number is null;
PRIMARY_KEY | PHASE | PROJECT_NUMBER | LENGTH_COL | COLOR | LOOKUP_COLUMN
1 Phase-1 null 99 black null
2 Phase-2 2000 99 red l-2000
3 Phase-3 3000 99 blue l-3000
4 Phase-4 null 99 black null
I am a bit stuck , Can any one help me here. I have two tables with the below structure.
Table 1
Id String
Code1 String
Code2 String
Table 2
Id String
UserCode String
UniversalCode String
What i need to do is to replace all value in code1 and code2 with UniversalCode from the table 2. To be more clear , if code1 matches UserCode then repalce Code1 with universalCode and again for the same record if code2 matches with usercode then replace it with UniversalCode. If there is no match retain the value for code1 and code2. I need to have all records from table 1. Table 1 and Table 2 are connected via Id.
I tried the below for one column but got stuck adding code2
SELECT
Id,
CASE WHEN a.Code1 = b.UserCode then b.UniversalCode else a.Code1 end
from table1 a
LEFT OUTER JOIN table2 b ON (a.Id = b.Id and a.code1 = b.UserCode);
Any suggestions to get this done ? , The real scenario has 5-6 columns where i need to apply the same logic.
Test Data
Table 1
1,123,ABCD
1,ABCD,123
1,456,BCD
1,BCD,789
1,789,100
Table 2
1,123,XXX
1,456,YYY
1,789,ZZZ
2,123,XXX
2,456,YYY
2,789,ZZZ
Output
1,XXX,ABCD
1,ABCD,XXX
1,YYY,BCD
1,BCD,ZZZ
1,ZZZ,100
output with a.id=b.id in Join(Please refer below comments for this output)
1 123 XXX 100 100
1 123 123 100 100
1 123 123 100 100
1 ABCD ABCD 101 101
1 ABCD ABCD 101 101
1 ABCD ABCD 101 101
1 456 456 DEF DEF
1 456 YYY DEF DEF
1 456 456 DEF DEF
1 BCD BCD 789 789
1 BCD BCD 789 789
1 BCD BCD 789 ZZZ
1 789 789 CDE CDE
1 789 789 CDE CDE
1 789 ZZZ CDE CDE
1 100 100 HBT HBT
1 100 100 HBT HBT
1 100 100 HBT HBT
1 100 100 123 XXX
1 100 100 123 123
1 100 100 123 123
I'm not sure if I understand you but something like this should help you.
SELECT
a.Id
, CASE WHEN a.Code1 = b.UserCode THEN b.UniversalCode ELSE a.Code1 END AS Code1
, CASE WHEN a.Code2 = b.UserCode THEN b.UniversalCode ELSE a.Code2 END AS Code2
FROM table1 a
LEFT OUTER JOIN
table2 b
ON a.Id = b.Id;
We can try the below query:-
SELECT
Id,
CASE WHEN Code1 = UserCode then UniversalCode else Code1 end,
CASE WHEN Code2= UserCode then UniversalCode else Code2 end
from
(select a.id,a.Code1,a.Code2,b.UserCode,b.UniversalCode
from table1 a
LEFT OUTER JOIN table2 b ON (a.Id = b.Id and a.code1 = b.UserCode) union
select a.id,a.Code1,a.Code2,b.UserCode,b.UniversalCode
from table1 a
LEFT OUTER JOIN table2 b ON (a.Id = b.Id and a.code1 = b.UniversalCode)) dat ;
I have type column in my table, type column value is HOT andNOT. from that i want to display HOT and NOT values of the column in to one line.
Example
Table1
Period ID Total
11/2011 101 250
12/2011 102 350
11/2011 103 450
....
Table2
Period ID Type Value
11/2011 101 NOT 500
11/2011 101 HOT 200
12/2011 102 NOT 300
12/2011 102 HOT 200
....
I want to display type (Hot and Not) in to one line
Expected Output
Period ID NOT HOT Total
11/2011 101 500 200 250
12/2011 102 300 200 350
11/2011 103 300 400 450
....
How to make a query.
If I can assume that table1 has a primary key of (Period, ID) and table2 has a primary key of (Period, ID, Type), then you can do:
select
t1.period
, t1.id
, t2n.value [not]
, t2h.value [hot]
, t1.total
from
table1 t1
left join table2 t2n
on t1.period = t2n.period
and t1.id = t2n.id
and t2n.type = 'Not'
left join table2 t2h
on t1.period = t2h.period
and t1.id = t2h.id
and t2h.type = 'Hot'
This will retrieve all rows from table 1, with their corresponding "not" and "hot" counterparts, respective to t2n and t2h above.
Did you try this?
select base.period, base.id, sum( notchild.value ) as notsum, sum( hotchild.value ) as hotsum, base.total
from table1 base
left outer join table2 notchild
on base.period = notchild.period and base.id = notchild.id and notchild.type = 'NOT'
left outer join table2 hotchild
on base.period = hotchild.period and base.id = hotchild.id and hotchild.type = 'HOT'
group by base.period, base.id, base.total
You can use JOIN(Check this) to do this. Join your two tables and perform a SELECT on it.
TableA
ID MatchID1 MatchID2
1 1002 120
3 1003 141
5 1006 150
6 1008 140
TableB
ID MatchID1 MatchID2 Status
1 1002 120
2 1002 120
3 1002 120
4 1003 200
5 1006 150
6 1008 150
7 1008 140
I want to populate TableB col = status with 'FAIL' if:
ALL matchID2 for its MatchID1 from tableB is not equal to the matchID2 for its corresponding MAatchID1 in tableA
In this case, tableB: 120 corresponds to 1002, same is true for tableA, hence not fail.
Expected result:
ID MatchID1 MatchID2 Status
1 1002 120 NULL
2 1002 120 NULL
3 1002 120 NULL
4 1003 200 FAIL
5 1006 150 NULL
6 1008 150 FAIL
7 1008 140 FAIL
Note:
If even one record(match02) is not matching, fail whole set for match01. like for id 6&7 in tableB. Thanks in advance.
UPDATE a
SET Status = 'FAIL'
FROM TableA a
INNER JOIN (
SELECT a.MatchID1
FROM TableA a
INNER JOIN b ON a.MatchID1 = b.MatchID1 AND a.MatchID2 <> b.MatchID2
GROUP BY a.MatchID1
) x ON a.MatchID1 = x.MatchID1
update B
set status = 'FAIL'
From tableB B
INNER JOIN (SELECT B.matchID1 FROM TableB B
GROUP BY B.matchID1
HAVING MAX(matchID1)<> MIN(MatchID2)) B1
ON B.matchid1 = B1.MatchID1
UPDATE B
SET Status = 'FAIL'
FROM TableB B
INNER JOIN TableA A
ON A.MatchID1 = B.matchID1
WHERE A.matchID2 <> B.matchID2
re-reading.. still don't know what you asked...
suggest breaking down your effort into smaller chunks.
it seems you need several things:
a small query to get just the rows from the first table that do or do not have different match numbers. write that.
a small query to find if match number from the second table are found in the first table. write that.
an update that sets values based on the first two queries.
hth
I think you are looking for an update via a join. Try this;
UPDATE TableB
SET TableB.Status = CASE WHEN TableA.ID IS NULL THEN 'FAIL' ELSE NULL END
FROM TableB
LEFT JOIN TableA
ON TableB.MatchID1 = TableA.MatchID1
AND TableB.MatchID2 = TableA.MatchID2
You didn't say which RDBMS you are using, the above is for SQL Server.