SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Get value from latest date in period?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

de9625
Starting Member

17 Posts

Posted - 12/12/2002 :  06:55:17  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2878 Posts

Posted - 12/12/2002 :  07:18:11  Show Profile  Reply with Quote
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 - 12/12/2002 :  07:41:40  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000