Hive table join with update - sql

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 ;

Related

Pull record between 2 columns

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;

Outer join and unique records in each table

TableA
------
IntId EvId Name Phone1
===== ==== ==== ======
100 aaa xxx 11111
101 bbb yyy 22222
102 ccc zzz 33333
103 bbb asd 44444
104 bbb sdf 55555
TableB
------
IntId ASId Grp Phone2
===== ==== ==== ======
201 bbb yyy 6666
202 ccc zzz 7777
203 bbb asd 8888
204 bbb kkf 9999
205 ddd esd 0000
206 eee ffr 1001
I want to join TableA with TableB using TableA.EvId = TableB.ASId to output {IndId, EvId, Name, Phone1, Grp, Phone2} (using outerjoin as I want all records in TableA with matching TableB columns)
I could do it using below query, but it is giving me duplicates since EvId and ASId have duplicates.
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.EvId = b.ASId
If EvId are duplicates, consider that record which has max IntId. Same rule for TableB
My final output should be like this - all unique EvId's in TableA using outerjoin on B
IntId EvId Name Phone1 Phone2 Grp
100 aaa xxx 11111 null null
104 bbb sdf 55555 9999 kkf
102 ccc zzz 33333 7777 zzz
Can you please help me with the query?
You can use ROW_NUMBER():
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a LEFT OUTER JOIN
(SELECT b.*,
ROW_NUMBER() OVER (PARTITION BY ASId ORDER BY IntID DESC) as seqnum
FROM TableB b
) b
ON a.EvId = b.ASId AND b.seqnum = 1;

how to get Unique data from one table and the other

If I want to display Order_Number data1 data2 data3 (most current by date changed OtherData1, OtherData2, OtherData3) date_changed the problem is I wasn't just one line, I was getting multiple lines for each order number.
What I would love to get is
1, a, f,q, cc,ccc,abc, 12/2/2014, bob
3, c, b,h, aa,aaa,abc, 12/2/2014, bob
Thanks!
I was working with
SELECT
t.Order_Number,
cr.data1, cr.data2, cr.data3,
t.OtherData1, t.OtherData2, t.OtherData3,
x.date_changed, cr.name
FROM
(SELECT
Order_Number,
Max(date_changed) as date_changed
FROM
table2
GROUP BY
Order_Number) x
JOIN
table2 t ON x.date_changed = t.date_changed
LEFT JOIN
table1 cr ON x.Order_Number = cr.Order_Number
WHERE cr.name = 'bob'
Here are example tables.
Table1:
Order_Number data1 data2 data3 name
1 a f q bob
2 b g g john
3 c b h bob
4 d s j john
Table2:
Order_Number date_changed OtherData1 OtherData2 OtherData3
1 11/30/2014 aa aaa abc
1 12/1/2014 bb bbb def
1 12/2/2014 cc ccc abc
3 12/1/2014 dd aaa def
2 11/30/2014 dd bbb abc
2 12/1/2014 ss ccc def
3 12/2/2014 aa aaa abc
4 11/26/2014 fc wer wsd
Your Join to config_log (Table2) needs to include the entire composite key if you want to retrieve unique rows.
JOIN
conf_log t ON x.date_changed = t.date_changed
And x.Order_Number = t.Order_number
I think you need to have 2 sub querys:
SELECT
Data.Order_Number,
Data.data1, Data.data2, Data.data3,
Data.OtherData1, Data.OtherData2, Data.OtherData3,
Data.date_changed, Data.name
FROM
(SELECT
Order_Number,
Max(date_changed) as date_changed
FROM
table2
GROUP BY
Order_Number) x
JOIN (SELECT
t.Order_Number,
cr.data1, cr.data2, cr.data3,
t.OtherData1, t.OtherData2, t.OtherData3,
t.date_changed, cr.name
FROM table2 t
JOIN table1 cr
ON t.Order_Number = cr.Order_Number) AS Data
ON x.date_changed = data.date_changed
AND x.Order_Number = data.Order_number
WHERE cr.name = 'bob'
The fact that you had cr.name in the where clause means the LEFT JOIN had the same affect as just JOIN.

Update a column to the same value based on constraints from other columns

I have a table like this on SQL Server:
code description percentage
123 abc 1
123 oke 0
123 cfd 0
234 kde 2
234 kfc 0
234 kfc 0
How can I update the description of all '0 percentage' records to non-zero percentage record for each code group? e.g. the result I'm after is:
code description percentage
123 abc 1
123 abc 0
123 abc 0
234 kde 2
234 kde 0
234 kde 0
Update T1 set description = T2.Description from YourTable T1
inner join
( select code, description from yourTable group by code, description
where percentage <> 0 ) T2 on T1.Code = T2.Code
where T1.Code = 0
Edited
This consider that each group has only zero on only one another description.
Try
UPDATE Table
SET description=(SELECT TOP 1 description
FROM Table t
WHERE t.code = Table.code AND percentage<>'0')
WHERE percentage='0'

Sql Inner Join first record only if Exists Take Next one

This one is support hard for me. I can do inner join with first result only, but if exist I want take 2nd result.
THIS IS MY TABLE A
ID NAME VALUE
1 A 123
2 B 456
3 C 789
4 A 456
TABLE B
BID BNAME BVALUE
1 A ABC
2 A CDE
3 B 845
4 C 1234
MY SELECT SQL:
SELECT * FROM A
CROSS APPLY (
SELECT TOP 1 *
FROM B
WHERE A.Name = B.BName
) BB
It return
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 1 A ABC
Please help, I want this result:
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 2 A CDE
I accept tmp table and any kind of query :(
Following clarification in the comments that both tables will always have matching rows.
WITH A
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY ID) AS RN
FROM TableA),
B
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY BNAME ORDER BY BID) AS RN
FROM TableB)
SELECT A.ID,
A.NAME,
A.VALUE,
B.BID,
B.BNAME,
B.BVALUE
FROM A
JOIN B
ON A.NAME = B.BNAME
AND A.RN = B.RN