代码之家  ›  专栏  ›  技术社区  ›  Glory Raj

select查询中带有count的内部联接

  •  0
  • Glory Raj  · 技术社区  · 7 年前

    我正在尝试使用下面的查询获取有关详细信息的查询以及状态(如果该报表的特定列存在)。

    select rh.Rec_ID 
       ,rh.Report_ID 
       ,rh.Report_Name 
       ,rh.Source_Type_Display 
       ,rh.Description 
       ,rh.IndID 
       ,rh.Name 
       ,rh.Time_Updated 
       ,count(*) OVER() as TotalCount
       ,case when count(rd.demo) > 0 THEN 'Completed' ELSE 'incomplete' END
      FROM v_Report_Header_OV rh 
      inner join v_Table_NI_Report_Demo rd
      ON rh.Report_ID = rd.Report_ID
      WHERE rh.Client_ID = 12324
    

    我的错误低于

    Column 'v_Report_Header_OV.Rec_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    我不知道我为什么会出错,有人能帮我吗

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Zaynul Abadin Tuhin    7 年前

    你错过了 group by

    select rh.Rec_ID 
       ,rh.Report_ID 
       ,rh.Report_Name 
       ,rh.Source_Type_Display 
       ,rh.Description 
       ,rh.IndID 
       ,rh.Name 
       ,rh.Time_Updated 
       ,count(*) OVER() as TotalCount
       ,case when count(rd.demo) > 0 THEN 'Completed' ELSE 'incomplete' END
      FROM v_Report_Header_OV rh 
      inner join v_Table_NI_Report_Demo rd
      ON rh.Report_ID = rd.Report_ID
      WHERE rh.Client_ID = 12324
     group by rh.Rec_ID 
       ,rh.Report_ID 
       ,rh.Report_Name 
       ,rh.Source_Type_Display 
       ,rh.Description 
       ,rh.IndID 
       ,rh.Name 
       ,rh.Time_Updated