| Author |
Topic |
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-05-20 : 09:42:50
|
| Okay, so i have a table like belowtblScoresemail, distance, playDatewith example data as belowtest@test.com , 25, 12-01-2009test@test.com , 23, 13-01-2009test@test.com , 45, 12-03-2009test@test.com , 51, 11-01-2009test2@test.com , 65, 12-01-2009test2@test.com , 24, 13-03-2009test2@test.com , 45, 12-03-2009test2@test.com , 52, 11-01-2009what i want to do is select all the highest distances for each email address within a date range and also add a total row count. so the above table would come out as follows if i searched for januarytest@test.com , 51, 11-01-2009, 3test2@test.com , 65, 12-01-2009, 2I can do the date range bit, what I'm having trouble with is creating the last column (the total number of occurrences of an email address during a date range) and also selecting the highest distanceany help is appreciated |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-20 : 09:53:06
|
[code]DECLARE @tblScores TABLE( email varchar(20), distance int, playDate datetime)INSERT INTO @tblScoresSELECT 'test@test.com' , 25, '12-01-2009' UNION ALLSELECT 'test@test.com' , 23, '13-01-2009' UNION ALLSELECT 'test@test.com' , 45, '12-03-2009' UNION ALLSELECT 'test@test.com' , 51, '11-01-2009' UNION ALLSELECT 'test2@test.com' , 65, '12-01-2009' UNION ALLSELECT 'test2@test.com' , 24, '13-03-2009' UNION ALLSELECT 'test2@test.com' , 45, '12-03-2009' UNION ALLSELECT 'test2@test.com' , 52, '11-01-2009'SELECT s.*, m.cntFROM @tblScores s INNER JOIN ( SELECT email, distance = MAX(distance), cnt = COUNT(*) FROM @tblScores WHERE playDate >= '2009-01-01' AND playDate <= '2009-01-31' GROUP BY email ) m ON s.email = m.email AND s.distance = m.distanceWHERE playDate >= '2009-01-01'AND playDate <= '2009-01-31'ORDER BY email DESC/*email distance playDate cnt -------------------- ----------- ------------------------------------------------------ ----------- test@test.com 51 2009-01-11 00:00:00.000 3test2@test.com 65 2009-01-12 00:00:00.000 2(2 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|