查询以显示不同的组合,然后显示每个不同组合的计数
Query to display distinct combination and then display count for each distinct combination
此 table 中存在多行的 transid 字段,其中包含配置值列表。对于所有 transid,我想按计数
检索存在于 table 组中的所有 transid、config_name 和值字段的不同组合
我的连接查询没有按预期返回结果。以下是 table 结构、使用的查询、结果和预期结果
Table
transid config_name value
1 payment_fee instant
2 eligible_account true
1 Block_intl_trans false
5 payment_fee provider_charge
1 eligible_account false
1 KycEligible 0
2 KycEligible 1
5 KycEligible 1
5 Block_intl_trans true
2 Block_intl_trans false
2 payment_fee provider_charge
5 eligible_account true
上面的table结构意味着下面是每个用户的配置值组合。
transid KycEligible payment_fee eligible_account Block_intl_trans
1 0 instant false false
2 1 provider_charge true false
5 1 provider_charge true false
下面是我用来将行转换为列然后按 config_name 对它们进行分组的查询(对于每个 config_name,而不是每个配置键和值组合的多行)。然后 select table 中存在的 KycEligible、configname 和值组合的所有不同组合以及每个不同组合的计数。
select
distinct
max(case when b.config_name = 'KycEligible' then b.config_value end) KycEligible,
max(case when b.config_name = 'payment_fee' then b.config_value end) payment_fee,
max(case when b.config_name = 'eligible_account' then b.config_value end) eligible_account,
max(case when b.config_name = 'Block_intl_trans' then b.config_value end) Block_intl_trans,
count(*) AS COUNT
from tableA b
where b.config_name in ('KycEligible', 'payment_fee', 'eligible_account', 'Block_intl_trans')
group by b.config_name
having count(*) > 1
预期结果:
KycEligible payment_fee eligible_account Block_intl_trans Count
0 instant false false 1
1 provider_charge true false 2
我的查询没有返回预期的结果。有人可以帮忙解决这个问题吗?
我想你需要这样的东西:
SELECT
KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) CNT
FROM (
SELECT
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'KycEligible' AND t0.transid = t.transid) as KycEligible,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'payment_fee' AND t0.transid = t.transid) as payment_fee,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'eligible_account' AND t0.transid = t.transid) as eligible_account,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'Block_intl_trans' AND t0.transid = t.transid) as Block_intl_trans
FROM
test t
GROUP BY t.transid
) dt
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans
;
这将给出以下结果:
ycEligible payment_fee eligible_account Block_intl_trans CNT
0 instant○ false false 1
1 provider_charge true false 1
1 provider_charge true true 1
结果与问题中的预期不同:
2 and 5 transids have different Block_intl_trans
1 and 5 transids have different payment_fee
1 and 2 transids have different eligible_account
我给出了两个 fiddle 示例,因为您提供的示例数据不一致。
- 此 fiddle 适用于 transid=5 且
Block_intl_trans
数据为 true
- 与您的 table 数据示例一致:https://www.db-fiddle.com/f/r1imsYP8dQxkLSo5SkYcVK/3
- 这个fiddle是for transid=5 with
Block_intl_trans
数据是false
- 和你说明的配置组合一致:https://www.db-fiddle.com/f/r1imsYP8dQxkLSo5SkYcVK/4
我猜测唯一组合将来自所有 config_name
值。这是示例查询:
SELECT KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) FROM
(SELECT transid,
GROUP_CONCAT(CASE WHEN config_name="KycEligible" THEN config_value END) AS "KycEligible",
GROUP_CONCAT(CASE WHEN config_name="payment_fee" THEN config_value END) AS "payment_fee",
GROUP_CONCAT(CASE WHEN config_name="eligible_account" THEN config_value END) AS "eligible_account",
GROUP_CONCAT(CASE WHEN config_name="Block_intl_trans" THEN config_value END) AS "Block_intl_trans"
FROM TableA
GROUP BY transid) V
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans;
使用 Max 进行简单查询,
结果计数可以基于您的输入数据 (transid=5, config=Block_intl_trans, value=false or value=true)
SELECT KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) FROM
(
SELECT
transid,
max(CASE WHEN config_name="KycEligible" THEN value END) AS "KycEligible",
max(CASE WHEN config_name="payment_fee" THEN value END) AS "payment_fee",
max(CASE WHEN config_name="eligible_account" THEN value END) AS "eligible_account",
max(CASE WHEN config_name="Block_intl_trans" THEN value END) AS "Block_intl_trans"
FROM <YOUR_TABLE_NAME>
GROUP BY transid
) tmp
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans;
此 table 中存在多行的 transid 字段,其中包含配置值列表。对于所有 transid,我想按计数
检索存在于 table 组中的所有 transid、config_name 和值字段的不同组合我的连接查询没有按预期返回结果。以下是 table 结构、使用的查询、结果和预期结果
Table
transid config_name value
1 payment_fee instant
2 eligible_account true
1 Block_intl_trans false
5 payment_fee provider_charge
1 eligible_account false
1 KycEligible 0
2 KycEligible 1
5 KycEligible 1
5 Block_intl_trans true
2 Block_intl_trans false
2 payment_fee provider_charge
5 eligible_account true
上面的table结构意味着下面是每个用户的配置值组合。
transid KycEligible payment_fee eligible_account Block_intl_trans
1 0 instant false false
2 1 provider_charge true false
5 1 provider_charge true false
下面是我用来将行转换为列然后按 config_name 对它们进行分组的查询(对于每个 config_name,而不是每个配置键和值组合的多行)。然后 select table 中存在的 KycEligible、configname 和值组合的所有不同组合以及每个不同组合的计数。
select
distinct
max(case when b.config_name = 'KycEligible' then b.config_value end) KycEligible,
max(case when b.config_name = 'payment_fee' then b.config_value end) payment_fee,
max(case when b.config_name = 'eligible_account' then b.config_value end) eligible_account,
max(case when b.config_name = 'Block_intl_trans' then b.config_value end) Block_intl_trans,
count(*) AS COUNT
from tableA b
where b.config_name in ('KycEligible', 'payment_fee', 'eligible_account', 'Block_intl_trans')
group by b.config_name
having count(*) > 1
预期结果:
KycEligible payment_fee eligible_account Block_intl_trans Count
0 instant false false 1
1 provider_charge true false 2
我的查询没有返回预期的结果。有人可以帮忙解决这个问题吗?
我想你需要这样的东西:
SELECT
KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) CNT
FROM (
SELECT
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'KycEligible' AND t0.transid = t.transid) as KycEligible,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'payment_fee' AND t0.transid = t.transid) as payment_fee,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'eligible_account' AND t0.transid = t.transid) as eligible_account,
(SELECT MAX(t0.config_value) FROM test t0 WHERE t0.config_name = 'Block_intl_trans' AND t0.transid = t.transid) as Block_intl_trans
FROM
test t
GROUP BY t.transid
) dt
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans
;
这将给出以下结果:
ycEligible payment_fee eligible_account Block_intl_trans CNT
0 instant○ false false 1
1 provider_charge true false 1
1 provider_charge true true 1
结果与问题中的预期不同:
2 and 5 transids have different Block_intl_trans
1 and 5 transids have different payment_fee
1 and 2 transids have different eligible_account
我给出了两个 fiddle 示例,因为您提供的示例数据不一致。
- 此 fiddle 适用于 transid=5 且
Block_intl_trans
数据为true
- 与您的 table 数据示例一致:https://www.db-fiddle.com/f/r1imsYP8dQxkLSo5SkYcVK/3 - 这个fiddle是for transid=5 with
Block_intl_trans
数据是false
- 和你说明的配置组合一致:https://www.db-fiddle.com/f/r1imsYP8dQxkLSo5SkYcVK/4
我猜测唯一组合将来自所有 config_name
值。这是示例查询:
SELECT KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) FROM
(SELECT transid,
GROUP_CONCAT(CASE WHEN config_name="KycEligible" THEN config_value END) AS "KycEligible",
GROUP_CONCAT(CASE WHEN config_name="payment_fee" THEN config_value END) AS "payment_fee",
GROUP_CONCAT(CASE WHEN config_name="eligible_account" THEN config_value END) AS "eligible_account",
GROUP_CONCAT(CASE WHEN config_name="Block_intl_trans" THEN config_value END) AS "Block_intl_trans"
FROM TableA
GROUP BY transid) V
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans;
使用 Max 进行简单查询,
结果计数可以基于您的输入数据 (transid=5, config=Block_intl_trans, value=false or value=true)
SELECT KycEligible, payment_fee, eligible_account, Block_intl_trans, COUNT(*) FROM
(
SELECT
transid,
max(CASE WHEN config_name="KycEligible" THEN value END) AS "KycEligible",
max(CASE WHEN config_name="payment_fee" THEN value END) AS "payment_fee",
max(CASE WHEN config_name="eligible_account" THEN value END) AS "eligible_account",
max(CASE WHEN config_name="Block_intl_trans" THEN value END) AS "Block_intl_trans"
FROM <YOUR_TABLE_NAME>
GROUP BY transid
) tmp
GROUP BY KycEligible, payment_fee, eligible_account, Block_intl_trans;