我想加入两个具有公共列 stationcode(TABLE vfdefects & tempsubstation)的表。问题是 tempsubstation 具有重复的列值,而我只想要每个唯一值的计数。在我目前的尝试中,使用LEFT JOIN
和GROUP BY
TABLE A
ID|stationID|CODE
TABLE B
A|B|C|D|stationcode
查询如下:
SELECT DISTINCT TS.substationid,'20130924',ts.employeeid,ts.substationcode,ts.manufacturingproductid,ts.assemblylineid,ts.teamid,ts.subofficecode,
TA.AllowID,ta.OtherAllowanceID,ta.allowname,ta.minbenchmark,ta.maxbenchmark,COUNT(vfd.DefectsID) Achieved
FROM tempsubstation ts
LEFT JOIN TempAllowances ta ON ts.manufacturingproductid=ta.manufacturingproductid AND ts.subofficecode=ta.subofficecode AND ta.teamid=ts.teamid
LEFT JOIN wms_assemblyqcmapping aqc ON ts.subofficeid=aqc.subofficeid AND ts.manufacturingproductid=aqc.manufacturingproductid AND ts.assemblylineid=aqc.assemblylineid
LEFT JOIN wms_vfdefects vfd ON vfd.QCStationCode=aqc.QCStationCode AND ts.SubstationCode =vfd.MFGStationNum AND DATE(vfd.CreationDate)='2013-09-24'
WHERE ta.AllowID=42
GROUP BY ta.minbenchmark,ta.maxbenchmark,ts.substationid
HAVING COUNT(vfd.DefectsID)>=ta.minbenchmark AND COUNT(vfd.DefectsID)<=ta.maxbenchmark