我在下面提到的sql,主表中的问题下陷入困境
|PLACE| DATE_1 |ID | FQ | PLAN|
---------------------------------
Alpha |01-03-16 | A | 100| YES |
Alpha |01-03-16 | A | 50 | YES |
Alpha |02-03-16 | A | 10 | YES |
BETA |02-03-16 | C | 80 | YES |
BETA |02-03-16 | D | 9 | YES |
BETA |03-03-16 | D | 11 | YES |
BETA |03-03-16 | E | 13 | YES |
--------------------------------
但需要以下格式输出
|Sum of FQ | Plan |Sum of FQ | Plan |
| Place |ID |01-03-16 | 01-03-16 | 02-03-16 | 02-03-16 |
| Alaph |A |100 | YES | 10 | NO |
| Alaph |B |50 | YES | | |
| Beta |C | | | 80 | NO |
答案 0 :(得分:0)
根据输入数据,您的输出没有意义。我猜你想要条件聚合:
select place, id,
max(case when date_1 = '2016-03-01' then fq end) as fq_20160301,
max(case when date_1 = '2016-03-01' then plan end) as plan_20160301,
max(case when date_1 = '2016-03-02' then fq end) as fq_20160302,
max(case when date_1 = '2016-03-02' then plan end) as plan_20160302
from t
group by place, id;
请注意,这使用ISO标准日期常量。如果您将日期存储为字符串(这是一个非常糟糕的主意),那么您需要调整数据的逻辑。