SQL Group乱码错误(SQL Server)

时间:2017-04-04 13:32:23

标签: sql sql-server

我有SQL代码会抛出错误

  

错误:SQLCODE = -119,SQLSTATE = 42803,SQLERRMC = WONUM

代码正常工作,直到我添加组:

  select *
  from workorder 
  left join labtrans on  labtrans.refwo=workorder.wonum and labtrans.siteid=workorder.siteid
  left join matusetrans on workorder.wonum=matusetrans.refwo and workorder.siteid=matusetrans.tositeid  and linetype not in (select value from synonymdomain where domainid='LINETYPE' and maxvalue='TOOL')
  left join  locations  on locations.location = workorder.location and locations.siteid=workorder.siteid
  left join person on personid in (select personid from labor where laborcode = labtrans.laborcode)
  left join po on workorder.wonum=po.hflwonum and workorder.siteid=po.siteid and workorder.orgid=po.orgid
  left join companies on companies.company = po.vendor and companies.orgid=po.orgid
  left join pluspcustomer on pluspcustomer.customer=workorder.pluspcustomer
  where workorder.wonum='10192'
  group by personid
 ;

2 个答案:

答案 0 :(得分:2)

如果您只GROUP BY personid,则无法选择personid以外的所有内容,或SUMMAX等聚合函数使用的字段

<强>更新

如果您只想查看重复的personid,您可以使用:

 select personid 
 from table 
 group by personid 

但请注意:如果您编写这样的查询,那么确定重复记录的唯一字段是persionid,如果您需要从不同的persionid唯一标识每个CompanyId ,您需要group by persionid, CompanyId,否则,来自不同公司的同一personId将被视为重复记录。

但是如果要删除这些重复记录,则应使用ROW_NUMBER()OVER (Partition by persionid Order by your_criteria)删除重复记录。尝试进行一些搜索,看看它是如何工作的,通常我更喜欢将该函数与CTE table expression一起使用。

答案 1 :(得分:1)

如果您只需要删除重复项,请将DISTINCT与您的查询一起使用,如下所示:

您的查询:

SELECT * FROM  .....

修改它:

SELECT DISTINCT * FROM .....

希望它有所帮助。