0

我有一个像这样的表结构:

  • 月(日期时间)
  • 帐户(整数)
  • 产品(整数)
  • 支付金额(整数)

还有一些示例数据:

month     account product amountPaid 
------------------------------------
1-1-2012  1       1       50 
2-1-2012  1       1       50 
2-1-2012  2       1       150 
2-1-2012  2       2       100 

我想要的是一个可以告诉我的查询,

  1. 对于每个月,仅支付产品 1 的帐户数量。
  2. 仅支付产品 2 的账户数量。
  3. 以及同时支付产品 1 和 2 的账户数量。

此外,每个帐户支付的产品可以逐月更改。例如,一个帐户可能在一个月内只为产品 1 付款,下个月为产品 1 和 2 付款,下个月只为产品 2 付款。

这可以在 SQL 查询中完成吗?

结果集可能类似于:

month     product  count 
------------------------
1-1-2012  1        10 
1-1-2012  2        5   
1-1-2012  1+2      3   
2-1-2012  1        8   
2-1-2012  2        4   
2-1-2012  1+2      2   
4

3 回答 3

4

您可以使用单个 SQL 查询来完成,但我的解决方案不是动态的。如果您的产品发生变化,您必须更新查询以添加/删除要显示的产品。有关 SQL Server 2008 中的工作示例,请参阅SQL Fiddle 。

with AccPay as (
 select 
  pay_month, 
  account,
  sum(case when product=1 then 1 else 0 end) as prod1_pay,
  sum(case when product=2 then 1 else 0 end) as prod2_pay
 from payments p
 group by
  pay_month,
  account
)
select
 pay_month,
 sum(case when prod1_pay>=1 and prod2_pay=0 then 1 else 0 end) as prod1_only,
 sum(case when prod2_pay>=1 and prod1_pay=0 then 1 else 0 end) as prod2_only,
 sum(case when prod1_pay>=1 and prod2_pay>=1 then 1 else 0 end) as prod1_and_prod2
from AccPay
group by
 pay_month
;

或者

with AccPay as (
 select 
  pay_month, 
  account,
  sum(case when product=1 then 1 else 0 end) as prod1_pay,
  sum(case when product=2 then 1 else 0 end) as prod2_pay
 from payments p
 group by
  pay_month,
  account
)
select
 pay_month,
 '1' as product,
 count(*)  as count
from AccPay
where prod1_pay>=1 and prod2_pay=0
group by 
 pay_month
union all
select
 pay_month,
 '2' as product,
 count(*) as count
from AccPay
where prod2_pay>=1 and prod1_pay=0
group by 
 pay_month
union all
select
 pay_month,
 '1+2' as product,
 count(*)  as count
from AccPay
where prod1_pay>=1 and prod2_pay>=1
group by 
 pay_month

;
于 2013-08-21T04:53:11.747 回答
0

由于只有两种产品,因此有一个简单的解决方案。在这个查询中,我假设您的表的名称是数据,数字 3 代表产品 1+2。

select month, sum ( product ) as products, count ( number ) as number
from (
    select month, product, count ( account ) as number
    from data
    group by month, product
) as t
group by month

在 mysql 中有一个函数 group_concat 就是这样做的(它返回 '1+2' )。在 MS Sql 中,它早在 2000 年就不存在了,但也许从那以后它就被引入了。

于 2013-08-21T04:53:40.270 回答
0

试试这个。其他人可以随意编辑,但这就是我所拥有的。

with x as (select [month], case when HasProduct1 = 1 and HasProduct2 = 1 then '1+2'
                    when HasProduct1 = 1 then '1'
                    when HasProduct2 = 1 then '2' else null end as "Product"
    from
    (select s1.[month], 
        case when exists(select s2.product 
                     from sales s2 
                      where s2.[month] = s1.[month] and 
                            s2.account = s1.account and 
                            s2.product = 1) then 1 else 0 end as HasProduct1,
        case when exists(select s2.product 
                     from sales s2 
                      where s2.[month] = s1.[month] and 
                            s2.account = s1.account and 
                            s2.product = 2) then 1 else 0 end as HasProduct2
        from sales s1) w
)
select [month], 
       Product, 
       (select count(*) from x x2 where x2.Product = x.Product) "Count"
from x
 group by [month], Product
于 2013-08-21T04:56:19.550 回答