gpt4 book ai didi

MySQL 对 select 中的相似行进行计数

转载 作者:行者123 更新时间:2023-11-30 01:03:20 24 4
gpt4 key购买 nike

我有这个查询:

SELECT organisation.organisation as org, kund_nr as kundnrnr
FROM anv_data
INNER JOIN organisation ON organisation.ID = anv_data.organisation_id
INNER JOIN anvandare ON anv_data.anvandare_id = anvandare.ID
WHERE anv_data.indatum<='2013-10-31' AND anv_data.indatum>='2013-10-01' AND anv_data.rp = '0'
GROUP BY MONTH(anv_data.indatum), anv_data.anvandare_id ORDER BY anvandare.anstallningsnr

生成此响应:

organisation    kundnr
---------------------------
Företag1 123456
Företag1 123456
Företag1 123456
Företag2 789123
Företag2 789123
Företag2 789123
Företag2 789123

我想修改查询,以便它总结具有类似“组织”的行,使其看起来像:

organisation    kundnr  sum
---------------------------
Företag1 123456 3
Företag2 789123 4

这可能吗?

最佳答案

SELECT A.org,A.kundnrnr,count(1) as sum
FROM(
SELECT organisation.organisation as org, kund_nr as kundnrnr
FROM anv_data
INNER JOIN organisation ON organisation.ID = anv_data.organisation_id
INNER JOIN anvandare ON anv_data.anvandare_id = anvandare.ID
WHERE anv_data.indatum<='2013-10-31' AND anv_data.indatum>='2013-10-01' AND anv_data.rp = '0'
GROUP BY MONTH(anv_data.indatum), anv_data.anvandare_id ORDER BY anvandare.anstallningsnr
) A
GROUP BY A.org,A.kundnrnr

关于MySQL 对 select 中的相似行进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19901198/

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