如何与group by一起使用case语句?

时间:2019-08-05 03:11:23

标签: sqlite

假设我们公司针对一种特定产品进行了多次营销活动,这可能会以某种方式促进其销售。广告系列的结果显示在以下数据表中:

Date         CampaignID  QtySold
2017-01-05   1           20   
2017-01-18   2           35
2017-01-23   1           15
…

出于建模目的,所需的输出表如下所示:

CampaignID JanQtySold FebQtySold … DecQtySold
1          55         30
2          45         20
…
N

我尝试获取每个广告系列每次交易的月份,然后按campaignIDmonth分组。

select
    campaignid,
    strftime('%m',date) as Month,
    sum(qtysold) as Sum_Qty
from campaign
group by campaignid, month
;

返回应包含唯一的CampaignID,但我不知道如何进行。

2 个答案:

答案 0 :(得分:0)

我相信您想要类似的东西:-

int a

引入更多组并不是您想要更多派生列的真正原因(引入更多分组组件将引入更多行,因此您只需要1个分组组件,即广告系列ID)。而是要使用子查询为派生的列生成数据。

  • 注意,为简洁起见,仅显示了3个月(1月至3月),其他月份只是复制其中一个月份,然后分别修改测试值和列名。

示例:-

使用:-

struct A

结果:-

enter image description here

答案 1 :(得分:0)

您需要在CASE内使用SUM()语句进行条件聚合:

select
  CampaignID,
  sum(case strftime('%m',Date) when '01' then QtySold else 0 end) as JanQtySold,
  sum(case strftime('%m',Date) when '02' then QtySold else 0 end) as FebQtySold,
  sum(case strftime('%m',Date) when '03' then QtySold else 0 end) as MarQtySold,
  sum(case strftime('%m',Date) when '04' then QtySold else 0 end) as AprQtySold,
  sum(case strftime('%m',Date) when '05' then QtySold else 0 end) as MayQtySold,
  sum(case strftime('%m',Date) when '06' then QtySold else 0 end) as JunQtySold,
  sum(case strftime('%m',Date) when '07' then QtySold else 0 end) as JulQtySold,
  sum(case strftime('%m',Date) when '08' then QtySold else 0 end) as AugQtySold,
  sum(case strftime('%m',Date) when '09' then QtySold else 0 end) as SepQtySold,
  sum(case strftime('%m',Date) when '10' then QtySold else 0 end) as OctQtySold,
  sum(case strftime('%m',Date) when '11' then QtySold else 0 end) as NovQtySold,
  sum(case strftime('%m',Date) when '12' then QtySold else 0 end) as DecQtySold
from campaign
where strftime('%Y',Date) = '2017'
group by CampaignID

我添加了条件:

where strftime('%Y',Date) = '2017'

以防万一表中存在多年的行。
如果您不希望结果中为零,只需从else 0语句中删除所有case
请参见demo
为了提高效率,您可以使用CTE:

with cte as (
  select 
    CampaignID, 
    strftime('%Y',Date) as year,
    strftime('%m',Date) as month,
    QtySold
  from campaign
)  
select
  CampaignID,
  sum(case month when '01' then QtySold else 0 end) as JanQtySold,
  sum(case month when '02' then QtySold else 0 end) as FebQtySold,
  sum(case month when '03' then QtySold else 0 end) as MarQtySold,
  sum(case month when '04' then QtySold else 0 end) as AprQtySold,
  sum(case month when '05' then QtySold else 0 end) as MayQtySold,
  sum(case month when '06' then QtySold else 0 end) as JunQtySold,
  sum(case month when '07' then QtySold else 0 end) as JulQtySold,
  sum(case month when '08' then QtySold else 0 end) as AugQtySold,
  sum(case month when '09' then QtySold else 0 end) as SepQtySold,
  sum(case month when '10' then QtySold else 0 end) as OctQtySold,
  sum(case month when '11' then QtySold else 0 end) as NovQtySold,
  sum(case month when '12' then QtySold else 0 end) as DecQtySold
from cte
where year = '2017'
group by CampaignID

请参见demo

结果:

| CampaignID | JanQtySold | FebQtySold | MarQtySold | AprQtySold | MayQtySold | JunQtySold | JulQtySold | AugQtySold | SepQtySold | OctQtySold | NovQtySold | DecQtySold |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 1          | 35         | 65         | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          |
| 2          | 35         | 75         | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          | 0          |