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 |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2010-03-26 : 07:29:57
|
| Hi,following on from my previous issue regarding dates, I have a similar issue as follows:tblMain has Code, Amount, DateThere are many records with the same or different IDS and DatesI would like to sum up the Amount for the min date in one query and then sum up the Amount in the Max dates for each Code.At present I have:select code, sum(Amount), min(date) from tblMain group by codebut this seems to be summing up everything and not just the codes in the min date.Any thoughtst please?Thanks |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2010-03-26 : 07:54:58
|
| I wonder if row_number should be used here somehow?Thanks |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-03-26 : 07:55:33
|
Here's one way to do it:-- Populate a tempory table with the min and max dates for each codeSELECT Code, MIN(Date) AS MinDate, MAX(Date) AS MaxDateINTO #tmpFROM tblMainGROUP BY Code-- Select the amount sums for the min date of each codeSELECT m.Code, t.MinDate, SUM(m.Amount)FROM tblMain mINNER JOIN #tmp t ON m.Code = t.Code AND m.Date = t.MinDateGROUP BY m.Code, t.MinDate-- Select the amount sums for the max date of each codeSELECT m.Code, t.MaxDate, SUM(m.Amount)FROM tblMain mINNER JOIN #tmp t ON m.Code = t.Code AND m.Date = t.MaxDateGROUP BY m.Code, t.MaxDate-- Drop the temp tableDROP TABLE #tmpGO There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2010-03-27 : 14:58:11
|
| Thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-27 : 15:08:33
|
another wayselect code,sum(case when date=mindate then Amount else 0 end) as mindatetotal,sum(case when date=maxdate then Amount else 0 end) as maxdatetotalfrom(select code,Amount,date, min(date) over (partition by code) as mindate,max(date) over (partition by code) as maxdate from tblMain )tgroup by code ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-27 : 15:11:27
|
quote: Originally posted by arkiboys I wonder if row_number should be used here somehow?Thanks
why not  select code,sum(case when fseq=1 then Amount else 0 end) as mindatetotal,sum(case when bseq=1 then Amount else 0 end) as maxdatetotalfrom(select code,Amount,date, row_number() over (partition by code order by date desc) as bseq, row_number() over (partition by code order by date asc) as fseqfrom tblMain )twhere bseq=1or fseq=1group by code ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|