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
 General SQL Server Forums
 New to SQL Server Programming
 three dimensiional table

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-15 : 04:00:43
hi,

i'm having:

select
month(date) as month_order,
sum(case year(date) when 2006 then 1 else 0 end) as year_2006,
sum(case year(date) when 2007 then 1 else 0 end) as year_2007
from order
group by month(date)
order by month(date)


which produces two dimensional table on orders by date. Now i want to add additional dimension country. How should I modify it?

thank you

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-15 : 04:16:58
do you just mean ...


select country,
month(date) as month_order,
sum(case year(date) when 2006 then 1 else 0 end) as year_2006,
sum(case year(date) when 2007 then 1 else 0 end) as year_2007
from order
group by country, month(date)
order by country, month(date)


if not, post an example of how you wan the resultset to look

Em
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-15 : 04:27:08
elancaster, thank you for reply, but this is not how i want the table to look like.

it should look something like:
country
-----------------------------------------
date \ GB | USA | JAR
-----------------------------------------
year 2007
jan_
feb_
.
.
.
year 2006
jan_
feb_
.
.
.
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-15 : 04:33:23
more like...


select year(date) as year_order,
month(date) as month_order,
sum(case when country = 'GB' then 1 else 0 end) as GB,
sum(case when country = 'USA' then 1 else 0 end) as USA,
sum(case when country = 'JAR' then 1 else 0 end) as JAR
from order
group by year(date), month(date)
order by year(date), month(date)

or if you're using SQL 2005 maybe look at PIVOT in BOL

Em
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-15 : 04:44:20
PIVOT i can't use, neither can I use CROSS APPLY :-)

what if I want to add additional dimension? product. how to nest further down?

thank you.
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-15 : 04:48:02
show me another mock up of the data with another dimension

Em
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-15 : 04:52:06
----------------------------------------
product 1 | product 2
-----------------------------------------
date \ GB | USA | JAR || GB | USA | JAR
-----------------------------------------
year 2007
jan_
feb_
.
.
.
year 2006
jan_
feb_
.
.
.
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-15 : 04:52:53
i've written this a couple of ways round...

 declare @t table (mydate datetime, country varchar(10),product varchar(10))

insert into @t
select '20070101','GB','prod1'
union all select '20070102','GB','prod2'
union all select '20070103','GB','prod1'
union all select '20070101','USA','prod1'
union all select '20070102','USA','prod2'
union all select '20070103','USA','prod2'
union all select '20070104','USA','prod2'


select year(mydate) as year_order,
month(mydate) as month_order,
sum(case when country = 'GB' then 1 else 0 end) as GB,
sum(case when country = 'USA' then 1 else 0 end) as USA,
sum(case when product = 'prod1' then 1 else 0 end) as product1,
sum(case when product = 'prod2' then 1 else 0 end) as product2,
sum(case when product = 'prod1' and country = 'GB' then 1 else 0 end) as GB_product1,
sum(case when product = 'prod2' and country = 'GB' then 1 else 0 end) as GB_product2,
sum(case when product = 'prod1' and country = 'USA' then 1 else 0 end) as USA_product1,
sum(case when product = 'prod2' and country = 'USA' then 1 else 0 end) as USA_product2
from @t
group by year(mydate), month(mydate)
order by year(mydate), month(mydate)


Em
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-15 : 04:55:18
you sure you couldn't just do this in your front end app?

Em
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-15 : 05:27:54
I've done something similar. but it's painfull work since i can't use pivot :(

thank you very much elancaster for help.
Go to Top of Page
   

- Advertisement -