【发布时间】:2023-03-08 22:05:01
【问题描述】:
我有一张表,每个月都会列出带有 active_indicator 的客户。对于每个客户,我只想将活动指标拉出两个月(2014 年 12 月和 2015 年 12 月),但是当我编写以下代码时,我得到一个表格,其中每个客户都列出了两次。我知道我可以执行另一个步骤来使用 max 将表汇总到客户级别,但无论如何可以在一个简单的 SQL 查询中执行此操作吗?
select distinct
customer
,case when date='2015-12-01' then active_indicator else 0 end as Dec2015_active_ind
,case when date='2014-12-01' then active_indicator else 0 end as Dec2014_active_ind
from monthly_account_cust
where date in ('2015-12-01', '2014-12-01')
order by customer
【问题讨论】:
-
标记使用的 dbms(MySQL 还是 MS SQL Server?)。列
date的数据类型? -
除非你聚合它,否则假设有两行符合日期条件,你将得到两行。
标签: sql sql-server