列“ Transaction_Master.Tranaction_Slno”无效

时间:2019-02-21 05:50:22

标签: sql sql-server sql-server-2008

我遇到错误:

  

select中的“ Transaction_Master.Tranaction_Slno”列无效   列出,因为它既不包含在聚合函数中也不包含   GROUP BY子句。

select TM.Tranaction_Slno,Container_Number,count(TM.CFS_Id), Container_Size,CFM.CFS_Name,
TD.Container_Size as Size,
Read_Time,gate,Location from Transaction_Master TM
inner join Transaction_Data TD on TD.Tranaction_Slno = TM.Tranaction_Slno
inner join Transaction_Track TT on TT.Transaction_Slno = TM.Tranaction_Slno
inner join CFSMaster CFM on TM.CFS_ID = CFM.CFS_Id
where CFM.CreatedOn='2015-12-02 02:06:30.000'
group by CFM.CFS_Name

2 个答案:

答案 0 :(得分:0)

在这种情况下,当您在查询中使用聚合函数时,必须将所有选择列添加到分组依据中,但聚合列除外

select TM.Tranaction_Slno,Container_Number,count(TM.CFS_Id), Container_Size,CFM.CFS_Name,
TD.Container_Size as Size,
Read_Time,gate,Location from Transaction_Master TM
inner join Transaction_Data TD on TD.Tranaction_Slno = TM.Tranaction_Slno
inner join Transaction_Track TT on TT.Transaction_Slno = TM.Tranaction_Slno
inner join CFSMaster CFM on TM.CFS_ID = CFM.CFS_Id
where CFM.CreatedOn='2015-12-02 02:06:30.000'
group by CFM.CFS_Name,TM.Tranaction_Slno,Container_Number,Container_Size,
TD.Container_Size,Read_Time,gate,Location

答案 1 :(得分:0)

您要选择的所有列应位于group by子句中或聚合函数的一部分中。您收到的错误消息也说了同样的话。您可以在查询下面运行。

  select TM.Tranaction_Slno,Container_Number,count(TM.CFS_Id), Container_Size,CFM.CFS_Name,
        TD.Container_Size as Size,
        Read_Time,gate,Location from Transaction_Master TM
        inner join Transaction_Data TD on TD.Tranaction_Slno = TM.Tranaction_Slno
        inner join Transaction_Track TT on TT.Transaction_Slno = TM.Tranaction_Slno
        inner join CFSMaster CFM on TM.CFS_ID = CFM.CFS_Id
        where CFM.CreatedOn='2015-12-02 02:06:30.000'
 group by CFM.CFS_Name,TM.Tranaction_Slno,Container_Number,Container_Size