【问题标题】:Select from duplicated data into a reduced table with unique lines从重复数据中选择到具有唯一行的简化表中
【发布时间】:2018-09-22 08:21:25
【问题描述】:

我在 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 查询应该是什么样子?

【问题讨论】:

    标签: sql sql-server select duplicates unique


    【解决方案1】:

    您可以合并 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)
    

    【讨论】:

    • 好主意佩林!谢谢!
    • 基本上,由于 PingPong_Replen 比 PingPong_FE 更早,所以我将 maxPPval 计算为 (2*t2.PingPong_Replen + t2.PingPong_FE),效果很好。
    • 好主意!我不知何故认为这是一个字符串字段,我想做字符串连接。
    【解决方案2】:

    可能是这样的:

     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;
    

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多