从重复数据中选择具有唯一线条的简化表格

时间:2018-04-12 08:54:38

标签: sql sql-server select duplicates unique

我在SQL Server MS中创建了一个连接表,其中有几个重复的行。现在,我需要从这个表中做出明智的选择,以便根据特定的选择标准为每个(Item,Recall_Date)对设置唯一的行

以下是我作为选择标准所需要的视觉说明:

Selection logic from Input table into Output Table

基本上,我的选择标准应如下:

  

如果行有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查询应该如何?

3 个答案:

答案 0 :(得分:0)

你可以试试这个

SELECT
    Item, Recall_Date, PingPong_FE , PingPong_Replen 
FROM inpute i
WHERE Replen_Date = (SELECT MAX(Replen_Date) FROM inpute WHERE i.Item = Item)
AND (
        (PingPong_FE = 1 AND PingPong_Replen = 1)
        OR (PingPong_FE = 0 AND PingPong_Replen = 1)
        OR (PingPong_FE = 1 AND PingPong_Replen = 0)
        OR (PingPong_FE = 0 AND PingPong_Replen = 0)
    )

答案 1 :(得分:0)

您可以合并PingPong_Replen和PingPong_FE列,并获取具有最大值的行。

试试这个;

--queue

答案 2 :(得分:0)

可能是这样的:

 WITH CTE AS (
 SELECT Item, Recall_Date, PingPongFE, PinPongReplen , Row_number() over
 (PARTITION BY Item, Recall_Date ORDER BY Item, Recall_Date) ROW
 FROM Yourtable)
 SELECT * FROM CTE WHERE ROW=1;