我在 SQL Server MS 中创建了一个连接表,其中有几行重复。现在,我需要从该表中做出明智的选择,以便根据特定的选择标准为每个(Item, Recall_Date)对创建唯一的行:
这是我需要作为选择标准的视觉说明:
基本上,我的选择标准应该如下:
如果有 PingPong_FE = 1 & PingPong_Replen = 1 这样的行,那么选择这个,
否则,如果有 PingPong_FE = 0 & PingPong_Replen = 1 的行,则选择这个,
否则,如果有 PingPong_FE = 1 & PingPong_Replen = 0 这样的行,则选择这个,
否则,如果有 PingPong_FE = 0 & PingPong_Replen = 0 这样的行,则选择这个
进入输出表。
我的 SQL 查询应该是什么样的?
您可以合并 PingPong_Replen 和 PingPong_FE 列并获取具有最大值的行。
试试这个;
select * from
(
SELECT t.item, t.recall_date, t.fe_date , max(t.PingPong_Replen + t.PingPong_FE) AS maxPPval
FROM tableInput t
GROUP BY t.item, t.recall_date, fe_date) t1,
tableInput t2
where t2.item = t1.item
and t2.recall_date = t1.recall_date
and t1.fe_date = t2.fe_date
and t1.maxPPval = (t2.t.PingPong_Replen + t2.PingPong_FE)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句