【问题标题】:Joining the results of two queries with different field names连接两个具有不同字段名称的查询的结果
【发布时间】: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


【解决方案1】:

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 进行实际测试,所以如果有任何问题,请告诉我。

【讨论】:

  • 非常感谢您的回复。我认为这几乎是我需要的,但它仍然产生 2 个计划字段。我已经更新了我的描述,希望对您有所帮助。
  • @Kieran 欢迎您。因此,您需要例如两条 C 记录,第一条的 msisdn 为 119,另一条的值为 111?如果是这样,我们需要知道对于同一个计费平台,custom6 的值是否与 custom8 的值相同?
  • 是的,这是正确的。计费平台可能会重复,但是 custom6 和 custom8 值永远不应该匹配。只是为了确认,msisdn 是满足该条件的用户数
  • 对不起,上面的措辞很糟糕 - 计费平台不会是唯一值,custom6 和 custom8 很可能具有相同的计费平台。但是,custom6 值不应与 custom8 值相同。确认一下,msisdn 是满足该条件的用户数
  • 啊,它需要联合所有,然后它工作:) - 再次感谢!
【解决方案2】:

两个查询都会为每个“计划”和计费平台生成一行。我猜你的问题是每个“计划”都有不同的域,所以你只能加入 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2012-12-07
    • 2013-03-26
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多