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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 date - sum

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, Date

There are many records with the same or different IDS and Dates
I 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 code

but 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
Go to Top of Page

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 code
SELECT Code, MIN(Date) AS MinDate, MAX(Date) AS MaxDate
INTO #tmp
FROM tblMain
GROUP BY Code

-- Select the amount sums for the min date of each code
SELECT m.Code, t.MinDate, SUM(m.Amount)
FROM tblMain m
INNER JOIN #tmp t
ON m.Code = t.Code
AND m.Date = t.MinDate
GROUP BY m.Code, t.MinDate

-- Select the amount sums for the max date of each code
SELECT m.Code, t.MaxDate, SUM(m.Amount)
FROM tblMain m
INNER JOIN #tmp t
ON m.Code = t.Code
AND m.Date = t.MaxDate
GROUP BY m.Code, t.MaxDate

-- Drop the temp table
DROP TABLE #tmp
GO




There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-03-27 : 14:58:11
Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-27 : 15:08:33
another way


select code,
sum(case when date=mindate then Amount else 0 end) as mindatetotal,
sum(case when date=maxdate then Amount else 0 end) as maxdatetotal
from
(
select code,Amount,date, min(date) over (partition by code) as mindate,max(date) over (partition by code) as maxdate
from tblMain
)t
group by code


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 maxdatetotal
from
(
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 fseq
from tblMain
)t
where bseq=1
or fseq=1
group by code


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-03-27 : 15:54:14
Also asked here
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/6f6bde94-0c40-4de1-a508-06559a399608



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -