我有一个加入客户及其订单的视图,列如下:
CustomerId Gender OrderNo OrderDate OrderYear OrderMonth OrderQuarter
====================================================================================
当给定日期时,报告应该提取日期部分(年,季,月)并查询此视图以提供如下矩阵:
NoOfCustomersForTheMonth NoOfCustomersForTheQuarter NoOfCustomerForTheYear
===========================================================================================================
New Customers MALE 20 100 400
FEMALE 30 200 500
TOTAL 50 300 900
这是否可以在不更改视图的情况下实现?我希望我的解释足够清楚。我还道歉,如果有人回答了这个问题,请指出我正确的方向,我将欠你一生:))
答案 0 :(得分:0)
虽然我看不到您的数据是什么样的,但您应该能够使用聚合函数和CASE
表达式生成此报告:
select gender,
count(case when OrderMonth = 1 then CustomerId end) NoCustomersForMonth,
count(case when OrderQuarter = 1 then CustomerId end) NoCustomersForQuarter,
count(case when OrderYear = 2013 then CustomerId end) NoCustomersForYear
from yourview
group by gender
在CASE
内,您将使用您需要的值替换OrderMonth
,OrderQuarter
和OrderYear
的值