gpt4 book ai didi

mysql - MySQL 中的相同值未分组

转载 作者:行者123 更新时间:2023-11-30 00:22:14 25 4
gpt4 key购买 nike

我在两个单独的表(均为 varchar(45))中有两列,并且它们都有诸如 0123456 之类的值。

我有一个联合查询,它基本上堆叠两个表中的行。

select 
'ISSUED' AS action,
card_match_key AS card_match_key,
count(0) AS CNT
from
dim_consumer_cards
group by 1 , 2

union

select
'REDEEMED' AS action,
card_match_key AS card_match_key,
count(0) AS CNT
from
fct_card_redemptions
group by 1 , 2

然后是第二个查询,它在堆叠数据上形成交叉表。

Select
card_match_key as cardkey,
sum(case when action = 'ISSUED' then CNT else 0 end) as ISSUED,
sum(case when action = 'REDEEMED' then CNT else 0 end) as REDEEMED
from vw_rpt_matchkey1
group by
card_match_key

我的问题是我的值没有按相似的值分组(例如 0123456)。每个值应该只出现在一行上,但我得到了两行。

最佳答案

尝试更改您的第一个查询以使用通用子表达式,如下所示:

with (
select
'ISSUED' AS action,
card_match_key AS card_match_key,
from
dim_consumer_cards
UNION ALL
select
'REDEEMED' AS action,
card_match_key AS card_match_key,
from
fct_card_redemptions) AS combined
select action, card_match_key, count(1) as CNT
from combined
group by 1,2;

在进行分组之前,需要先进行并集操作。

关于mysql - MySQL 中的相同值未分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com