| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 14:48:05
|
| Please help in getting the desired output TableA:Pdate SID type Er Ac 7-6-2007 100 1 52 268-6-2007 100 1 52 477-6-2007 101 1 54 278-6-2007 102 1 52 288-6-2007 105 1 62 38TableBPdate SID type nt1 nt27-6-2007 100 2 PR RP1 8-6-2007 101 2 PR SP110-6-207 103 2 0p DT1TableCPdate SID type FT1 FT27-6-2007 100 3 KR 23 7-6-2007 101 3 1R 218-6-2007 102 3 TR 3111-6-207 104 3 Rp VTI want output in TableD: Pdate SID type Er Ac type nt1 nt2 type FT1 FT2---------------------------------------------------------8-6-2007 100 1 52 47 2 PR RP1 3 KR 23 7-6-2007 101 1 54 27 2 PR SP1 3 1R 218-6-2007 102 1 52 28 3 TR 31 103 2 0p DT1 104 3 Rp VT8-6-2007 105 1 62 38Business Rule:If the SID doesn't exists or new record in table D It should insert the data of tablea,tableb,tablecelse if the SID exists it should update the table D. |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-12 : 15:05:43
|
| Can you reformat the output you are expecting. Its not clear enough for me to understand what value goes into what column..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 15:40:35
|
Expected Output:Pdate SID type Er Ac type nt1 nt2 type FT1 FT2---------------------------------------------------------8-6-2007 100 1 52 47 2 PR RP1 3 KR 23 7-6-2007 101 1 54 27 2 PR SP1 3 1R 218-6-2007 102 1 52 28 3 TR 31 103 2 0p DT1 104 3 Rp VT8-6-2007 105 1 62 38 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 18:10:37
|
[code]SET dateformat dmyDECLARE @TableA TABLE( Pdate datetime, SID int, type int, Er int, Ac int)INSERT INTO @TableASELECT '7-6-2007', 100, 1, 52, 26 UNION ALLSELECT '8-6-2007', 100, 1, 52, 47 UNION ALLSELECT '7-6-2007', 101, 1, 54, 27 UNION ALLSELECT '8-6-2007', 102, 1, 52, 28 UNION ALLSELECT '8-6-2007', 105, 1, 62, 38DECLARE @TableB TABLE( Pdate datetime, SID int, type int, nt1 varchar(2), nt2 varchar(3))INSERT INTO @TableBSELECT '7-6-2007', 100, 2, 'PR', 'RP1' UNION ALLSELECT '8-6-2007', 101, 2, 'PR', 'SP1' UNION ALLSELECT '10-6-2007', 103, 2, '0p', 'DT1'DECLARE @TableC TABLE( Pdate datetime, SID int, type int, FT1 varchar(2), FT2 varchar(2))INSERT INTO @TableCSELECT '7-6-2007', 100, 3, 'KR', '23' UNION ALLSELECT '7-6-2007', 101, 3, '1R', '21' UNION ALLSELECT '8-6-2007', 102, 3, 'TR', '31' UNION ALLSELECT '11-6-2007', 104, 3, 'Rp', 'VT'SELECT Pdate = MAX(Pdate), SID, a_type = MAX(a_type), Er = MAX(Er), Ac = MAX(Ac), b_type = MAX(b_type), nt1 = MAX(nt1), nt2 = MAX(nt2), c_type = MAX(c_type), FT1 = MAX(FT1), FT2 = MAX(FT2)FROM( SELECT Pdate = a.Pdate, SID = coalesce(a.SID, b.SID, c.SID), a_type = a.type, a.Er, a.Ac, b_type = b.type, b.nt1, b.nt2, c_type = c.type, c.FT1, c.FT2 FROM @TableA a FULL OUTER JOIN @TableB b ON a.SID = b.SID FULL OUTER JOIN @TableC c ON a.SID = c.SID) aGROUP BY SIDORDER BY SID/*Pdate SID a_type Er Ac b_type nt1 nt2 c_type FT1 FT2 ----------- ----------- ----------- ----------- ----------- ----------- ---- ---- ----------- ---- ---- 2007-06-08 100 1 52 47 2 PR RP1 3 KR 232007-06-07 101 1 54 27 2 PR SP1 3 1R 212007-06-08 102 1 52 28 NULL NULL NULL 3 TR 31NULL 103 NULL NULL NULL 2 0p DT1 NULL NULL NULLNULL 104 NULL NULL NULL NULL NULL NULL 3 Rp VT2007-06-08 105 1 62 38 NULL NULL NULL NULL NULL NULL(6 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 20:18:10
|
| Thanks KH!!Sorry if i m not clear about output and requirments..BRules:1.I want a query which should de duplicates the data of SID's from three tables(Tablea,Tableb,Tablec) with max pdate and insert(LOAD) the data to a new tableD. 2.After deduplicating the data loaded into table D ,again there is a possiblitiy of getting duplicates records in three tables(tablea,tableb,tablec).In these case the query should validate the tableD if the SID already exists in the TableD then the query should update the existing record with SID else it should insert. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 21:59:33
|
So does the query i posted satisfy your BR 1 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 22:25:27
|
| It is near to match...but in your query it should not contain max for other columnsa_type = MAX(a_type), Er = MAX(Er), Ac = MAX(Ac), b_type = MAX(b_type), nt1 = MAX(nt1), nt2 = MAX(nt2), c_type = MAX(c_type), FT1 = MAX(FT1), FT2 = MAX(FT2) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 22:32:51
|
[code]SET dateformat dmyDECLARE @TableA TABLE( Pdate datetime, SID int, type int, Er int, Ac int)INSERT INTO @TableASELECT '7-6-2007', 100, 1, 52, 26 UNION ALLSELECT '8-6-2007', 100, 1, 52, 47 UNION ALLSELECT '7-6-2007', 101, 1, 54, 27 UNION ALLSELECT '8-6-2007', 102, 1, 52, 28 UNION ALLSELECT '8-6-2007', 105, 1, 62, 38DECLARE @TableB TABLE( Pdate datetime, SID int, type int, nt1 varchar(2), nt2 varchar(3))INSERT INTO @TableBSELECT '7-6-2007', 100, 2, 'PR', 'RP1' UNION ALLSELECT '8-6-2007', 101, 2, 'PR', 'SP1' UNION ALLSELECT '10-6-2007', 103, 2, '0p', 'DT1'DECLARE @TableC TABLE( Pdate datetime, SID int, type int, FT1 varchar(2), FT2 varchar(2))INSERT INTO @TableCSELECT '7-6-2007', 100, 3, 'KR', '23' UNION ALLSELECT '7-6-2007', 101, 3, '1R', '21' UNION ALLSELECT '8-6-2007', 102, 3, 'TR', '31' UNION ALLSELECT '11-6-2007', 104, 3, 'Rp', 'VT'SELECT Pdate = a.Pdate, SID = coalesce(a.SID, b.SID, c.SID), a_type = a.type, a.Er, a.Ac, b_type = b.type, b.nt1, b.nt2, c_type = c.type, c.FT1, c.FT2FROM ( SELECT Pdate, SID, type, Er, Ac FROM @TableA t WHERE Pdate = (SELECT MAX(Pdate) FROM @TableA x WHERE x.SID = t.SID) ) a FULL OUTER JOIN @TableB b ON a.SID = b.SID FULL OUTER JOIN @TableC c ON a.SID = c.SIDORDER BY SID/*Pdate SID a_type Er Ac b_type nt1 nt2 c_type FT1 FT2 ---------- ----------- ----------- ----------- ----------- ----------- ---- ---- ----------- ---- ---- 2007-06-08 100 1 52 47 2 PR RP1 3 KR 232007-06-07 101 1 54 27 2 PR SP1 3 1R 212007-06-08 102 1 52 28 NULL NULL NULL 3 TR 31NULL 103 NULL NULL NULL 2 0p DT1 NULL NULL NULLNULL 104 NULL NULL NULL NULL NULL NULL 3 Rp VT2007-06-08 105 1 62 38 NULL NULL NULL NULL NULL NULL*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 22:42:15
|
| Great KH!!Your query works perfectly for BR1..But how to modify the query to satisfy the BR2..... Thanks for your help in advance!!!!! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-12 : 22:47:22
|
quote: But how to modify the query to satisfy the BR2.....
Can you explain more on BR2 ?quote: 2.After deduplicating the data loaded into table D ,again there is a possiblitiy of getting duplicates records in three tables(tablea,tableb,tablec).In these case the query should validate the tableD if the SID already exists in the TableD then the query should update the existing record with SID else it should insert.
The updating of existing records is on which table and from which table ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-07-12 : 23:45:48
|
I want to update the table D with table A,tableb,Table c values.Below is example.Please let me know if m not clear....Ex:After Transfering or loading the data in TableD with the query, I want to update the status with IMP (IMPORTED) for transfered data in the tableA,TableB,TableC.New status is records which are not being transfered to TableD..New data being stored in below tables.. TableA:Pdate SID type Er Ac status------- --- --- -- -- ----7-6-2007 100 1 52 26 IMP8-6-2007 100 1 52 47 IMP 7-6-2007 101 1 54 27 IMP8-6-2007 102 1 52 28 IMP8-6-2007 105 1 62 38 IMP9-6-2007 100 1 44 35 New9-6-2007 101 1 45 35 New9-6-2007 103 1 45 35 New9-6-2007 106 1 45 35 NewTableBPdate SID type nt1 nt2 status-------- --- -- --- --- ------ 7-6-2007 100 2 PR RP1 IMP8-6-2007 101 2 PR SP1 IMP10-6-207 103 2 0p DT1 IMP11-6-207 103 2 Pp DT2 New11-6-207 102 2 Pp DT3 New11-6-207 106 2 oo DT1 NewTableCPdate SID type FT1 FT2 Status------- --- -- --- --- ------7-6-2007 100 3 KR 23 IMP7-6-2007 101 3 1R 21 IMP8-6-2007 102 3 TR 31 IMP11-6-207 104 3 Rp VT IMP8-6-2007 103 3 TR 31 NEW11-6-207 104 3 op VT New11-6-207 106 3 op VT NewDesired Output:TableDPdate SID type Er Ac type nt1 nt2 type FT1 FT2-------- ---- ---- --- --- ---- --- ---- ---- --- ---8-6-2007 100 1 44 35 2 PR RP1 3 KR 23 7-6-2007 101 1 54 27 2 PR SP1 3 1R 218-6-2007 102 1 52 28 2 PP DT3 3 TR 319-6-2007 103 1 45 35 2 Pp DT2 3 TR 31Null 104 null null null nul nul null 3 Op VT8-6-2007 105 1 62 38 nul nul nul nul nul null 9-6-2007 106 1 45 35 2 oo DT1 3 0p VT |
 |
|
|
|
|
|