如何使此查询不返回具有相同单元ID的行?

时间:2015-09-03 14:45:23

标签: sql sql-server

我有一个使用CTE的查询并提取这样的数据。

我的结果:

Mach    ProdDate    ActionCode  UnitID     RejectID    Wgt  
RW01    2015-08-28  Gross     215H0712AD              508.0000
RW01    2015-08-28  Gross     215H0712AD              1016.0000
RW01    2015-08-28  Gross     215H0712AE              508.0000
RW01    2015-08-28  Gross     215H0712AE              1016.0000
RW01    2015-08-28  Gross     215H0712AF              603.0000
RW01    2015-08-28  Gross     215H0712AF              1206.0000

期望的结果:

Mach    ProdDate    ActionCode  UnitID     RejectID    Wgt  
RW01    2015-08-28  Gross     215H0712AD              508.0000
RW01    2015-08-28  Gross     215H0712AE              508.0000
RW01    2015-08-28  Gross     215H0712AF              603.0000

现在这是我的问题。我需要找到一种方法来使我的查询不返回相同单位ID的倍数。我不知道该怎么做。正如您在下面看到的,我尝试使用distinct,但还有其他一些我没有提到的不同的列。这导致distinct再次提取相同的ID。当我总结时,不止一次使用相同的ID会增加总重量。

cteGROSS (UnitID, Time, Mach, Cte, ActionCode, RejectID, Wgt, ProdDate) AS
(
  SELECT
     distinct unit_id                 AS UnitID,
     sq_timestamp                     AS Time,
     (machine_type + machine_id)      AS Mach,
     'cteGross'                       AS Cte,
     'Gross'                          AS ActionCode,
     reject_id                        AS RejectID,
     SUM(wgt_scaled)                  AS Wgt,
     @ProdDate

  FROM MillProdDb.millproddb.dbo.invent_audit_cbs1

  WHERE
     CAST(sq_timestamp AS DATE) = @ProdDate         AND
     machine_type + machine_id in ('RW01','RW02','RW31','RW32') AND
     mx_station in ('RW','WINDER','REWINDER','CHECKER') AND
     wgt_scaled <> '0'

  GROUP BY
     unit_id,
     sq_timestamp,
     machine_type,
     machine_id,
     reject_id
),

我对SQL很新,对CTE的概念很陌生,但任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

当你正在进行更正时,你的错误就越清楚了。

您有GROUP BY sq_timestampWHERE CAST(sq_timestamp AS DATE) = @ProdDate

我敢打赌,您的表格中有三条记录显示每个UnitID,两条记录具有相同的sq_timestamp,另一条记录具有不同的sq_timestamp

你似乎有两个选择:
1.不要GROUP BY sq_timestamp
2.而是GROUP BY CAST(sq_timestamp AS DATE)

然后,您每UnitID可能只会获得一行。


但即便如此,SUM(wgt)也无法提供您想要的结果。也许你想要所有记录的AVG(wgt),或MAX(wgt)或perhp你应该GROUP BY wgt?你的问题在这一点上并不清楚。


最后,你有潜在的问题来源;
- SELECT (machine_type + machine_id)
- GROUP BY machine_type, machine_id

当您拥有'RW0' + '1''RW' + '01'等值时,这可能会出现问题。您的GROUP BY会将它们分开,但是当它们在您的SELECT中连接后,它们看起来是一样的。

答案 1 :(得分:0)

假设您需要min(wgt)

      with tbl(Mach    ,ProdDate,    ActionCode,  UnitID  ,   RejectID,    Wgt  ) as
      ( select 'RW01','2015-08-28','Gross','215H0712AD','',508.0000 from dual union
      select 'RW01','2015-08-28','Gross','215H0712AD','',1016.0000 from dual union
      select 'RW01','2015-08-28','Gross','215H0712AE','',508.0000 from dual union
      select 'RW01','2015-08-28','Gross','215H0712AE','',1016.0000 from dual union
      select 'RW01','2015-08-28','Gross','215H0712AF','',508.0000 from dual union
      select 'RW01','2015-08-28','Gross','215H0712AF','',1206.0000 from dual),
      dist as
      (select distinct(unitID) as dist_id,min(WGT) as min_wgt from tbl
      group by unitID)
      select tbl.* from dist,tbl
      where dist.dist_id = tbl.UnitID
      and dist.min_wgt=tbl.Wgt

输出

      MACH, PRODDATE, ACTIONCODE, UNITID,   REJECTID, WGT
      RW01  2015-08-28  Gross   215H0712AD           508
      RW01  2015-08-28  Gross   215H0712AE           508
      RW01  2015-08-28  Gross   215H0712AF           508