| Author |
Topic |
|
duplicity
Starting Member
4 Posts |
Posted - 2006-09-08 : 15:17:25
|
I have a table having columns:ID | Phone | Date | Position | StatusOnly the ID field is identity. What i want to do is to have DISTINCT Phones with latest Date, and if dates are same, with the latest ID.Moreover, I like the data to be sorted by Position. I tried to write a lot of statements but couldn't succeed.Please help me with it. Below is an example, in which red lines show the data i am after. Thanks  1 , 1111111 , 20060917 , 5 , active2 , 1111111 , 20060918 , 3 , passive3 , 1111111 , 20060918 , 7 , active4 , 2222222 , 20060917 , 5 , passive5 , 1111111 , 20060917 , 3 , active6 , 3333333 , 20060917 , 5 , active7 , 3333333 , 20060915 , 6 , active8 , 5555555 , 20060912 , 4 , passive9 , 5555555 , 20060912 , 3 , passive10, 4444444 , 20060917 , 8 , active11, 6666666 , 20060913 , 2 , passive12, 4444444 , 20060917 , 1 , passive13, 5555555 , 20060911 , 1 , active14, 6666666 , 20060917 , 4 , active The sorted data should look like this:12, 4444444 , 20060917 , 1 , passive9 , 5555555 , 20060912 , 3 , passive14, 6666666 , 20060917 , 4 , active4 , 2222222 , 20060917 , 5 , passive6 , 3333333 , 20060917 , 5 , active3 , 1111111 , 20060918 , 7 , activeIs it hard ??  |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-09-08 : 16:00:30
|
assuming you want the attributes from the same rowcreate table #junk (ID int, Phone int, Date datetime, Position int, Status varchar(15))insert #junk select 1 , 1111111 , '20060917' , 5 , 'active' union allselect 2 , 1111111 , '20060918' , 3 , 'passive' union allselect 3 , 1111111 , '20060918' , 7 , 'active' union allselect 4 , 2222222 , '20060917' , 5 , 'passive' union allselect 5 , 1111111 , '20060917' , 3 , 'active' union allselect 6 , 3333333 , '20060917' , 5 , 'active' union allselect 7 , 3333333 , '20060915' , 6 , 'active' union allselect 8 , 5555555 , '20060912' , 4 , 'passive' union allselect 9 , 5555555 , '20060912' , 3 , 'passive' union allselect 10, 4444444 , '20060917' , 8 , 'active' union allselect 11, 6666666 , '20060913' , 2 , 'passive' union allselect 12, 4444444 , '20060917' , 1 , 'passive' union allselect 13, 5555555 , '20060911' , 1 , 'active' union allselect 14, 6666666 , '20060917' , 4 , 'active'select a.*from #junk ajoin ( select phone, max(convert(varchar, date,120)+str(id,12)) concatStr from #junk group by phone ) b on b.phone = a.phone and convert(datetime, substring(b.concatStr, 1, 19)) = a.date and convert(int, substring(b.concatStr, 20, 12)) = a.idorder by a.positionoutput:ID Phone Date Position Status ----------- ----------- ------------------------------------------------------ ----------- --------------- 12 4444444 2006-09-17 00:00:00.000 1 passive9 5555555 2006-09-12 00:00:00.000 3 passive14 6666666 2006-09-17 00:00:00.000 4 active4 2222222 2006-09-17 00:00:00.000 5 passive6 3333333 2006-09-17 00:00:00.000 5 active3 1111111 2006-09-18 00:00:00.000 7 active Be One with the OptimizerTG |
 |
|
|
duplicity
Starting Member
4 Posts |
Posted - 2006-09-08 : 16:01:56
|
quote: Originally posted by X002548 SELECT PHONE, MAX(DATE), MAX(ID) FROM myTable99 GROUP BY PHONEBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
Gives me a result like:11111111 20060918 522222222 20060917 433333333 20060917 744444444 20060917 1255555555 20060912 1366666666 20060917 14which as you see is not correct. I don't want the maximum values of the group. I want all the row selected, where DATE is max, and if date is same ID is max. |
 |
|
|
duplicity
Starting Member
4 Posts |
Posted - 2006-09-08 : 16:15:11
|
quote: Originally posted by TG assuming you want the attributes from the same rowcreate table #junk (ID int, Phone int, Date datetime, Position int, Status varchar(15))insert #junk select 1 , 1111111 , '20060917' , 5 , 'active' union allselect 2 , 1111111 , '20060918' , 3 , 'passive' union allselect 3 , 1111111 , '20060918' , 7 , 'active' union allselect 4 , 2222222 , '20060917' , 5 , 'passive' union allselect 5 , 1111111 , '20060917' , 3 , 'active' union allselect 6 , 3333333 , '20060917' , 5 , 'active' union allselect 7 , 3333333 , '20060915' , 6 , 'active' union allselect 8 , 5555555 , '20060912' , 4 , 'passive' union allselect 9 , 5555555 , '20060912' , 3 , 'passive' union allselect 10, 4444444 , '20060917' , 8 , 'active' union allselect 11, 6666666 , '20060913' , 2 , 'passive' union allselect 12, 4444444 , '20060917' , 1 , 'passive' union allselect 13, 5555555 , '20060911' , 1 , 'active' union allselect 14, 6666666 , '20060917' , 4 , 'active'select a.*from #junk ajoin ( select phone, max(convert(varchar, date,120)+str(id,12)) concatStr from #junk group by phone ) b on b.phone = a.phone and convert(datetime, substring(b.concatStr, 1, 19)) = a.date and convert(int, substring(b.concatStr, 20, 12)) = a.idorder by a.positionoutput:ID Phone Date Position Status ----------- ----------- ------------------------------------------------------ ----------- --------------- 12 4444444 2006-09-17 00:00:00.000 1 passive9 5555555 2006-09-12 00:00:00.000 3 passive14 6666666 2006-09-17 00:00:00.000 4 active4 2222222 2006-09-17 00:00:00.000 5 passive6 3333333 2006-09-17 00:00:00.000 5 active3 1111111 2006-09-18 00:00:00.000 7 active Be One with the OptimizerTG
GREAT TG       THAT SOLVED MY PROBLEM. I JUST NEEDED TO CHANGE LIKE THIS:SELECT a.ID, a.PHONE, a.date, a.Position, a.StatusFROM table99 AS a INNER JOIN (SELECT PHONE, MAX(date + STR(ID, 12)) AS concatStr FROM table99 GROUP BY PHONE) AS b ON b.PHONE = a.PHONE AND CONVERT(int, SUBSTRING(b.concatStr, 10, 12)) = a.IDORDER BY a.PositionCause I store dates as varchar already.Thanks a lot dude |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2006-09-10 : 19:12:16
|
quote: Originally posted by duplicityCause I store dates as varchar already.
Gah! Why do people do this? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-10 : 20:43:12
|
| >>Gah! Why do people do this?Most newbies use varchar to store dates to store them in the format they want which will cause problems when retreiving dataMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|