【发布时间】:2017-06-28 08:03:28
【问题描述】:
我目前有两个查询如下。
我有两个字段 CUSTOM6 和 CUSTOM8。
它们都存储了可以分配给客户的不同计划 ID(客户始终在 custom6 和 custom8 中都有一个 planID)
我想从本质上计算每个计划 ID 上不同客户的总数。所以,我真的希望能够将下面的两个查询结果合并成一个输出。
我查看了其他堆栈溢出主题并看到建议使用 Union。但是,这对我不起作用。
你能给我建议吗?
谢谢
查询 1
SELECT custom8 as plan, custom1 as billingplatform, count(distinct MSISDN)as msisdn,
CASE
WHEN custom1 = 'DISE' then 'Business'
WHEN custom1 = 'CUK' then 'Consumer'
end as BusinessConsumer,
CASE
WHEN instr(custom8, 'ROW') = True then 'ROW'
end as Region
from kkudrfeed2
Group by custom8, custom1
AND 查询 2
SELECT custom6 as plan, custom1 as billingplatform, count(distinct MSISDN)as msisdn,
CASE
WHEN custom1 = 'DISE' then 'Business'
WHEN custom1 = 'CUK' then 'Consumer'
end as BusinessConsumer,
CASE
WHEN instr(custom6, 'DOM') = True then 'DOM'
end as Region
from kkudrfeed2
Group by custom6, custom1;
两个查询的输出如下。我基本上需要查询 2(带有字段 custom6)作为单个表直接位于查询 1(带有字段 custom8)下。
【问题讨论】:
-
你的表的结构是什么。特别是custom8和custom6的数据类型?您能否将其添加到您的查询中。你知道 CREATE TABLE ...
-
您还应该按这些案例陈述进行分组以进行适当的聚合
-
UNION 到底有什么不适合您的?您的预期输出是什么?
-
您能解释一下为什么
UNION不适合您吗?你也试过UNION ALL吗? -
您能否将您希望获得的输出添加到这些示例中?
标签: sql