Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Cheluvaraj
Starting Member
1 Post |
Posted - 2008-10-01 : 22:01:48
|
Hi Freinds, I have 2 tablesTable WorkMessageWorkNo GUID Last_date101 X120…… 2008-09-28 14:02:00102 X121… 2008-09-29 14:02:00103 X123….. 2008-09-30 09:02:00104 X124… 2008-10-01 11:02:00105 X125… 2008-10-01 14:02:00104 X124 2008-10-01 15:02:00103 X123 2008-10-01 18:02:00Table PieTraceGUID ErrorNo ErrorMessage Last_Date_TimeX120…… 0 blahblah 2008-09-28 14:45:00X123… -23 blahblah 2008-09-30 10:02:00X124… -1 blahblah 2008-10-01 11:15:00X125 0 blahblha 2008-10-01 14:02:00X124 0 blahblah 2008-10-01 16:02:00X123 0 blahblah 2008-10-01 18:02:00Below query for comparing 2 tables and extract the uncommon like GUID X121 doesn’t exists in the 2nd table and the GUID which are unsuccessful (has –ve values).select isnull(pt.GUID,Wo.GUIS)as GUID,pt.[ErrorNo] as Errorfrom PieTreace ptfull outer join WorkMessage Wm on pt.GUID = Wm.GUIDwhere(pt.GUId is null) or (pt.[ErrorNo]<0)Along with above query I want to extract those,ErroNo and GUID which are unsuccessful for first time and next time(It may be second time or third time)it’s successful, with the lastest date.(I have marked those repeating GUID with red colour.) Thanks. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 03:38:50
|
[code]SELECT pt.GUID,MAX(pt.ErrorNo),MAX(pt.Last_Date_Time)FROM PieTerrace ptLEFT JOIN WorkMessage wmON wm.GUID=pt.GUIDLEFT JOIN (SELECT GUID FROM PieTerrace GROUP BY GUID HAVING SUM(CASE WHEN ErrorNo<0 THEN 1 ELSE 0 END) >0 AND SUM(CASE WHEN ErrorNo>0 THEN 1 ELSE 0 END) >0 AND MAX(CASE WHEN ErrorNo<0 THEN Last_Date_Time ELSE NULL END) < MAX(CASE WHEN ErrorNo>0 THEN Last_Date_Time ELSE NULL END))tmpON tmp.GUID = pt.GUIDWHERE (wm.GUID IS NULLOR tmp.GUID IS NOT NULL)GROUP BY pt.GUID[/code] |
|
|
|
|
|