| Author |
Topic |
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-06-10 : 11:24:31
|
| I need to have greatest date in a view :SELECT DISTINCT PERSONNUM, MAX(EFFECTIVEDATE) AS EDate, PERSONFULLNAMEFROM tkcsdb2.dbo.VP_ACCRUALWHERE (ACCRUALCODENAME = N'Personal')GROUP BY PERSONNUM, ACCRUALTRANAMOUNT, PERSONFULLNAMEHAVING (PERSONNUM = N'10002')but it returns:1002 4/1/2007 k1002 10/7/2007 k1002 1/26/2008 kI just need the last row.What shoudl I do?Sep |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-10 : 11:26:52
|
SELECT DISTINCT PERSONNUM, MAX(EFFECTIVEDATE) AS EDate, PERSONFULLNAMEFROM tkcsdb2.dbo.VP_ACCRUALWHERE (ACCRUALCODENAME = N'Personal')and HAVING (PERSONNUM = N'10002')GROUP BY PERSONNUM, ACCRUALTRANAMOUNT, PERSONFULLNAME |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-10 : 11:28:44
|
oops! I copied and pasted the HAVINGSELECT DISTINCT PERSONNUM, MAX(EFFECTIVEDATE) AS EDate, PERSONFULLNAMEFROM tkcsdb2.dbo.VP_ACCRUALWHERE (ACCRUALCODENAME = N'Personal')and (PERSONNUM = N'10002')GROUP BY PERSONNUM, ACCRUALTRANAMOUNT, PERSONFULLNAME |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-06-10 : 11:34:03
|
| Thank you so much.Sep |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-06-10 : 11:37:05
|
| Now the problem is when I add AccrualTransAmount to the reporting fields again it shows me 3 date but I only need that filed for that date.SELECT PERSONNUM, MAX(EFFECTIVEDATE) AS EDate, PERSONFULLNAME, ACCRUALTRANAMOUNTFROM tkcsdb2.dbo.VP_ACCRUALWHERE (ACCRUALCODENAME = N'Personal')GROUP BY PERSONNUM, PERSONFULLNAME, ACCRUALTRANAMOUNTHAVING (PERSONNUM = N'10002')this is what I get 10002 1/26/2008 k 0 10002 10/7/2007 k 144000 10002 4/1/2007 k -144000And I only need the first row.Sep |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-10 : 12:01:56
|
quote: Originally posted by Sep410 Now the problem is when I add AccrualTransAmount to the reporting fields again it shows me 3 date but I only need that filed for that date.SELECT PERSONNUM, MAX(EFFECTIVEDATE) AS EDate, PERSONFULLNAME, ACCRUALTRANAMOUNTFROM tkcsdb2.dbo.VP_ACCRUALWHERE (ACCRUALCODENAME = N'Personal')GROUP BY PERSONNUM, PERSONFULLNAME, ACCRUALTRANAMOUNTHAVING (PERSONNUM = N'10002')this is what I get 10002 1/26/2008 k 0 10002 10/7/2007 k 144000 10002 4/1/2007 k -144000And I only need the first row.Sep
remove ACCRUALTRANAMOUNT from group by and use MIN or MAX. B/w are you concerned about the value to be retrieved for ACCRUALTRANAMOUNT field or a random value is enough? |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-06-10 : 12:05:32
|
| No the ACCRUALTRANAMOUNT should be the one which is in the max record.I mean it should be 0 in the this example10002 1/26/2008 k 0<----------- I need this amount which is for the greatest date10002 10/7/2007 k 14400010002 4/1/2007 k -144000Sep |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-10 : 12:37:10
|
| [code]SELECT t.PERSONNUM, t.EFFECTIVEDATE AS EDate, t.PERSONFULLNAME, t.ACCRUALTRANAMOUNTFROM tkcsdb2.dbo.VP_ACCRUAL tINNER JOIN (SELECT PERSONNUM, MAX(EFFECTIVEDATE) AS MaxDate FROM tkcsdb2.dbo.VP_ACCRUAL GROUP BY PERSONNUM)mON m.PERSONNUM=t.PERSONNUMAND m.MaxDate=t.EFFECTIVEDATEWHERE (ACCRUALCODENAME = N'Personal')AND (PERSONNUM = N'10002')[/code] |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-06-10 : 13:19:39
|
| it is not reyurning right value for ACCRUALTRANAMOUNT!!!Sep |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-10 : 13:22:36
|
quote: Originally posted by Sep410 it is not reyurning right value for ACCRUALTRANAMOUNT!!!Sep
Only reason i suspect you didnt get the correct value is datatype of EFFECTIVEDATE. Are you sure its datetime? |
 |
|
|
|