Can you create a cross-tab report in my sql Server!
How can I get the report about sale quality for each store and each quarter and the total sale quality for each quarter at year 1993?
You can use the table sales and stores in datatabase pubs.
Table Sales record all sale detail item for each store. Column store_id is the id of each store,ord_date is the order date of each sale item,and column qty is the sale qulity. Table stores record all store @R_322_4045@ion.
I want to get the result look like as below:
Output:
stor_name Total Qtr1 Qtr2 Qtr3 Qtr4
------------------------------------------------ ----------- ----------- ----------- ----------- -----------
Barnum's 50 0 50 0 0
Bookbeat 55 25 30 0 0
Doc-U-Mat: Quality Laundry and Books 85 0 85 0 0
Fricative Bookshop 60 35 0 0 25
Total 250 60 165 0 25
答案:
select
b.stor_name,
total=sum(a.qty),
Q1=sum(case when datepart(mm,ord_date)<4 then a.qty else 0 end),
Q2=sum(case when datepart(mm,ord_date)>3 and datepart(mm,ord_date)<7 then a.qty else 0 end),
Q3=sum(case when datepart(mm,ord_date)>6 and datepart(mm,ord_date)<10 then a.qty else 0 end),
Q4=sum(case when datepart(mm,ord_date)>9 then a.qty else 0 end)
from sales as a
inner join stores as b on b.stor_id = a.stor_id
where a.ord_date BETWEEN convert(datetime,'1992-12-31 23:59:59') and convert(datetime,'1994-1-1')
group by b.stor_name
union
select 'total',
sum(a.qty),
sum(case when datepart(mm,'1994-1-1')
没有pubs数据库的可以到这里找到创建pubs数据库的sql脚本.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。