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
 Transact-SQL (2000)
 Weeks

Author  Topic 

Adria
Starting Member

2 Posts

Posted - 2003-05-02 : 08:08:21
Hi.

so let me explain what i have:
structure:
date,product code, group, sales(quantity)....
would like to get
how many sales were made for each product grouped by weeks..

so perfect would be if i could get something like this
group 1
January
week1
product code sum(sales)
...........
week2
............

any ideas?



SamC
White Water Yakist

3467 Posts

Posted - 2003-05-02 : 09:01:51
SELECT date, productcode, sum(sales)

FROM MyTable

GROUP BY DATEPART(wk, date)

------

Look at the CONVERT function in BOL to pull the month and week out of date.

Sam

Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-05-02 : 09:06:20
Cross Post

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25890

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-05-02 : 09:26:56
Sam -- that actually won't work. you are selecting date but not grouping by it:

SELECT date, productcode, sum(sales)

FROM MyTable

GROUP BY DATEPART(wk, date)


Based on your full specs (including grouping by the Group column) what you want to do is something like this:

select [Group], Year(date), Month(date), datepart(wk,date) as Week, productcode, sum(sales)
from MyTable
group by [Group], Year(date), Month(date), datepart(wk,date), productCode
order by code, year(date), month(date), datepart(wk,date), productcode


- Jeff
Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-05-02 : 09:34:43
Jeff

That will return week of year I think he wants week of month see my post above.

Go to Top of Page
   

- Advertisement -