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)
 Sub Query Help?

Author  Topic 

sheridanbman
Starting Member

10 Posts

Posted - 2010-06-24 : 00:58:16
Hey Guys,
I'm not sure how to go about this. I have 2 tables and I'm trying to graph the data. I'm looking for the desired result below. I'm not sure if i can even produce a result like this. Is this doable? If so, I could please use some help.

Here is an example of the table structure and desired result.

tblReturns
----------------------------------------------
Return Date Building Return Type
1/4/2010 Store 1 Missing Item
1/4/2010 Store 1 Damage
1/4/2010 Store 2 Refused
1/4/2010 Store 1 Damage
1/4/2010 Store 2 Damage
1/4/2010 Store 2 Damage
1/11/2010 Store 1 Damage
1/11/2010 Store 2 Damage
1/11/2010 Store 1 Missing Item
1/11/2010 Store 2 Missing Item
1/11/2010 Store 1 Refused
1/18/2010 Store 1 Damage
1/18/2010 Store 2 Damage
1/18/2010 Store 2 Missing Item


tblOrders
----------------------------------------------
Date Building Order Totals
1/1/2010 Store 1 34
1/1/2010 Store 2 23
1/2/2010 Store 1 15
1/2/2010 Store 2 34
1/3/2010 Store 1 43
1/3/2010 Store 2 24
1/4/2010 Store 1 45
1/4/2010 Store 2 34
1/5/2010 Store 1 26
1/5/2010 Store 2 27
1/6/2010 Store 1 34
1/6/2010 Store 2 44
1/7/2010 Store 1 23
1/7/2010 Store 2 56
1/8/2010 Store 1 45
1/8/2010 Store 2 33
1/9/2010 Store 1 31
1/9/2010 Store 2 54
1/10/2010 Store 1 43
1/10/2010 Store 2 42
1/11/2010 Store 1 25
1/11/2010 Store 2 43
1/12/2010 Store 1 27
1/12/2010 Store 2 51
1/13/2010 Store 1 43
1/13/2010 Store 2 22
1/14/2010 Store 1 43
1/14/2010 Store 2 25
1/15/2010 Store 1 26
1/15/2010 Store 2 45
1/16/2010 Store 1 65
1/16/2010 Store 2 45
1/17/2010 Store 1 54
1/17/2010 Store 2 23
1/18/2010 Store 1 45
1/18/2010 Store 2 22


RESULT NEEDED
----------------------------------------------
Week Number Building Returns Orders
Week 1 Store 1 0 49
Week 1 Store 2 0 57
Week 2 Store 1 3 247
Week 2 Store 2 3 272
Week 3 Store 1 3 272
Week 3 Store 2 2 273
Week 4 Store 1 1 99
Week 4 Store 2 2 45







Sachin.Nand

2937 Posts

Posted - 2010-06-24 : 02:20:39
How do you define your week?Is it the week of the year or the month?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

sheridanbman
Starting Member

10 Posts

Posted - 2010-06-24 : 02:33:08
Hello Idera,
Thanks for asking. We are using week of the year.


quote:
Originally posted by Idera

How do you define your week?Is it the week of the year or the month?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH

Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-24 : 02:43:04
I havent used the all the parts of the data you posted.But the below query will give you an general idea.

declare @tblReturns as table(dates date,store varchar(20),totals varchar(2))

insert into @tblReturns

select '1/1/2010','Store1','34'union all
select'1/1/2010','Store2','23'union all
select'1/2/2010','Store1','15'union all
select'1/2/2010','Store2','34'union all
select'1/3/2010','Store1','43'union all
select'1/3/2010','Store2','24'union all
select'1/4/2010','Store1','45'union all
select'1/4/2010','Store2','34'union all
select'1/5/2010','Store1','26'union all
select'1/5/2010','Store2','27'union all
select'1/6/2010','Store1','34'union all
select'1/6/2010','Store2','44'union all
select'1/7/2010','Store1','23'union all
select'1/7/2010','Store2','56'union all
select'1/8/2010','Store1','45'union all
select'1/8/2010','Store2','33'union all
select'1/9/2010','Store1','31'union all
select'1/9/2010','Store2','54'union all
select'1/10/2010','Store1','43'union all
select'1/10/2010','Store2','42'union all
select'1/11/2010','Store1','25'union all
select'1/11/2010','Store2','43'union all
select'1/12/2010','Store1','27'union all
select'1/12/2010','Store2','51'union all
select'1/13/2010','Store1','43'

select * from @tblReturns

select
'WeekNo' + '' + convert(varchar,datepart(week,dates))as week,
store,
SUM(convert(int,totals))Orders
from @tblReturns
group by datepart(week,dates),store



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page
   

- Advertisement -