1

我有一个加入客户及其订单的视图,列如下:

CustomerId   Gender    OrderNo    OrderDate   OrderYear   OrderMonth   OrderQuarter
====================================================================================

当给定日期时,报告应该提取日期部分(年、季度、月)并查询此视图以给出如下所示的矩阵:

                          NoOfCustomersForTheMonth    NoOfCustomersForTheQuarter    NoOfCustomerForTheYear
===========================================================================================================
New Customers    MALE         20                              100                         400
                 FEMALE       30                              200                         500
TOTAL                         50                              300                         900

在不改变视图的情况下这可能吗?我希望我的解释足够清楚。如果其他人回答了这个问题,我也很抱歉,请指出正确的方向,我将欠你一条命 :)

4

1 回答 1

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您将替换 , 的值OrderMonthOrderQuarterOrderYear使用您需要的值

于 2013-01-30T21:27:24.443 回答