目前我有两个问题如下。
我有两个字段CUSTOM6和CUSTOM8。
它们都存储了可以分配给客户的不同计划ID(客户在custom6和custom8中始终都有planID)
我想基本上对每个计划ID的不同客户总数进行计数。所以,我真的希望能够将以下两个查询结果加入到单个输出中。
我已经了解了其他堆栈溢出主题&已经看过联盟了。不过,这对我没用。
你可以告诉我吗?由于
查询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;
答案 0 :(得分:0)
Union
会为您提供相同billingplatform
的单独行,但是,这显然不是您的意图。这就是你在理论上如何实现它的目的:
select c1, c2
from (query1) t1
join (query2) t2
on t1.something = t2.something
让我们在实践中应用这个:
select t1.billingplatform, t1.plan as t1plan, t2.plan as t2plan, t1.msisdn + t2.msisdn as msisdn
from
(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) t1
(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) t2
on t1.billingplatform = t2.billingplatform
请注意,此查询将忽略t1
或t2
没有其他对应对的记录,因此您可能希望使用外连接,将msisdn
默认为0在主select
子句中。
我很可能误解了这个问题。在这种情况下,我想请求进一步澄清。
修改
由于提问者提供了进一步的信息,我意识到在这种情况下我们不需要使用联接,而是在这种情况下需要结合。
(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)
union all
(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)
order by billingplatform
我现在没时间用MySQL测试这个,所以如果有任何问题,请告诉我。
答案 1 :(得分:0)
两个查询都会为每个'plan'和billingplatform生成一行。我猜你的问题是每个'计划'都有不同的域名,所以你只能加入计费平台。只在计费平台上加入将无法满足您的需求,因为您将获得计划的跨产品。 在这种情况下,您需要做的是将计划转换为同一个域。 假设计划代码由区域前缀和计划大小组成,并且您希望加入计划大小,则可以尝试修改您的查询:
SELECT substr(custom6,4) as plan_size, 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;