【问题标题】:Joining 3 tables with subquery使用子查询连接 3 个表
【发布时间】:2018-01-18 10:44:50
【问题描述】:

我有这个问题:

select  IDPersonne, NumeroClientAXA, COUNT(1) as 'NbrDoublons'
from PortefeuillePersonne
group by IDPersonne, NumeroClientAXA
having COUNT(1)>1

我想用这个子查询加入 3 个表 我尝试了类似的方法:

select prs.NumeroSocietaire, 
pp.NumeroClientAXA, pp.IDPortefeuille,  pf.Code, pf.Intitule, count(1)
 from PortefeuillePersonne pp
    Join Portefeuille pf
        ON pf.IDPortefeuille = pp.IDPortefeuille
    Join Personne prs
        ON prs.IDPersonne IN (select  IDPersonne
                             from PortefeuillePersonne
                             group by IDPersonne
                             having  COUNT(IDPersonne)>1)

它一直告诉我: 列 'Personne.NumeroSocietaire' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

你能帮帮我吗?

【问题讨论】:

  • 您不能将聚合与非聚合混合,所以要么GROUP BY 查询中的所有列,要么将COUNT(1) 更改为1
  • 第二次查询,您正在使用聚合函数 COUNT 而不使用 Group By
  • 问题是我希望 tp 检索具有相同 IDPersonne 和 NumClientAxxa 的记录

标签: sql sql-server join subquery


【解决方案1】:
Select    prs.NumeroSocietaire
        , pp.NumeroClientAXA
        , pp.IDPortefeuille
        , pf.Code
        , pf.Intitule
        , count(1)
from        PortefeuillePersonne pp
Inner Join (    select      IDPersonne
                from        PortefeuillePersonne
                group by    IDPersonne
                having      COUNT(IDPersonne)>1
            ) p1            ON pp.IDPersonne = P1.IDPersonne
Inner Join Portefeuille pf  ON pf.IDPortefeuille = pp.IDPortefeuille
Inner Join Personne prs     ON prs.IDPersonne = pp.IDPersonne
GROUP BY  prs.NumeroSocietaire
        , pp.NumeroClientAXA
        , pp.IDPortefeuille
        , pf.Code
        , pf.Intitule

【讨论】:

  • 这可以工作,但我需要检索具有相同 IDPersonne 和 NumClientAxa 的记录
【解决方案2】:

这不是一个有效的join 尝试;

ON prs.IDPersonne IN (select  IDPersonne
                     from PortefeuillePersonne
                     group by IDPersonne
                     having  COUNT(IDPersonne)>1)

您应该将子选择作为一个集合加入,所以完整的查询应该是这样的;

select prs.NumeroSocietaire, pp.NumeroClientAXA, pp.IDPortefeuille,  pf.Code, pf.Intitule, count(1) 
from PortefeuillePersonne pp
join Portefeuille pf ON pf.IDPortefeuille = pp.IDPortefeuille
join (select IDPersonne
             from PortefeuillePersonne
             group by IDPersonne
             having  COUNT(IDPersonne)>1) A ON A.IDPersonne = prs.IDPersonne
group by prs.NumeroSocietaire, pp.NumeroClientAXA, pp.IDPortefeuille,  pf.Code, pf.Intitule

【讨论】:

    【解决方案3】:

    假设您想要具有多个 PortefeuillePersonne 记录的记录,您应该将其更改为如下内容:

    select prs.NumeroSocietaire, pp.NumeroClientAXA, pp.IDPortefeuille,  pf.Code,
        pf.Intitule, count(pp.IDPersonne)
    from PortefeuillePersonne pp
        Join Portefeuille pf
            ON pf.IDPortefeuille = pp.IDPortefeuille
        Join Personne prs
            ON prs.IDPersonne = pp.IDPersonne
    group by prs.NumeroSocietaire, pp.NumeroClientAXA, pp.IDPortefeuille,  pf.Code,
        pf.Intitule
    having  COUNT(pp.IDPersonne)>1
    

    select 列表中的任何字段都必须聚合或包含在 SQL Server 中的 group by 中。

    【讨论】:

    • 我要检索IDPersonne和NumClientAxa相同的记录
    【解决方案4】:
    SELECT 
        first_name, last_name, salary
    FROM
        employees join salaries on employees.emp_no = salaries.emp_no
    WHERE
        employees.emp_no IN (SELECT 
                emp_no
            FROM
                dept_manager)
                group by employees.emp_no
                order by employees.emp_no;
    

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 2015-06-09
      相关资源
      最近更新 更多