gpt4 book ai didi

sql - 从给定另一个输入的表中获取计数

转载 作者:行者123 更新时间:2023-11-29 13:56:18 25 4
gpt4 key购买 nike

我有 3 个表:filer_infofiler_persentpersent_email,通过以下方式连接:

filer_info.filer_info_id = filer_persent.filer_info_id
filer_persent.persent_info_id = persent_email.persent_info_id

我想在 persent_email 表(即 count > 1)中找到我有多个 PRIMARY 类型的所有行。我唯一想在查询中返回的是 filer_info.filer_ident 和计数。

这给了我每一行,但我只想要返回行中 filer_ident > 1 的数据。

select * from filer_info f
inner join filer_persent fp on f.filer_info_id=fp.filer_info_id
inner join persent_email p on fp.persent_info_id=p.persent_info_id
where fp.filer_persent_kind_cd = 'FILER' and p.persent_email_kind_cd='PRIMARY'
order by f.filer_ident

最佳答案

SELECT  filer_ident, cnt
FROM (
SELECT filer_info_id, COUNT(*) cnt
FROM filer_persent fp
JOIN persent_email p
USING (persent_info_id)
WHERE (fp.filer_persent_kind_cd, p.persent_email_kind_cd) = ('FILER', 'PRIMARY')
HAVING COUNT(*) > 1
) с
JOIN filer_info f
USING (filer_info_id)
WHERE filer_ident > 1
ORDER BY
f.filer_ident

关于sql - 从给定另一个输入的表中获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125938/

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