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 2000 Forums
 SQL Server Development (2000)
 Get value from latest date in period?

Author  Topic 

de9625
Starting Member

17 Posts

Posted - 2002-12-12 : 06:55:17
I need a query that gets the value from the latest date in a period, in this case a week. The actual period is between about nine weeks.

The table contains a number of transactions for each day, so I want to do a sum(transaction_value) for the last day in the week.

Recordset with the result:

Field 1(sum of value for the last day), Field 2 (year + week number)
----------------------------------------------------------------
1293243, 200249
324234243,200250
324234324,200251
243098324,200252
45354345,200301
etc....

Hmm, I dont know where to start. I know I have do a group by for the year and week but not how to get them sum of transactions for the last day in every week. Cursors are not an option in this case :-(

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-12-12 : 07:18:11
Have you tried the sum() aggregate function?

(how bout you post some ddl and sample data so we can have a clue what you are talking about)

Jay White
{0}
Go to Top of Page

de9625
Starting Member

17 Posts

Posted - 2002-12-12 : 07:41:40
I think I found a solution:
The datepart function has probably not correct syntax it should work something like that.

SELECT (SUM(trans_value)/1000) AS value_SUM, Datepart(transDate,YEAR)+ Datepart(transDate,WEEK) AS PERIOD, transDate
FROM table1
WHERE transDate IN
( SELECT MAX(transDate) AS transDate
FROM table1
WHERE transDate between 20021206 and 20030131
GROUP BY Datepart(transDate,YEAR)+ Datepart(transDate,WEEK) AS PERIOD
)

GROUP BY Datepart(transDate,YEAR)+ Datepart(transDate,WEEK) AS PERIOD,transDate
ORDER BY transDate

Go to Top of Page
   

- Advertisement -