筛选重复的记录并添加值(1和2)的其他列

时间:2019-12-17 05:06:35

标签: mysql sql

我能够获得重复的记录,并且我想根据createdate (oldest)确定哪个项目是最旧的,并在其中输入1和2值。

获取重复记录的示例脚本:

select pl5.refcode, pl5.barcode, pl5.name, pl5.status, pl5.createDate
from pList6 pl6
inner join plist5 pl5 on pl6.barcode = pl5.barcode
order by pl5.barcode, pl5.createDate;

输出:

refcode        barcode          ItemDescription       status        createDate
0075345        480000234512     Shampoo A             A             2017-09-22 10:12:29
0002345        480000234512     Shampoo A             A             2019-09-22 13:07:29
0010388        790000345217     Shampoo B             A             2018-05-13 08:34:01
0023754        790000345217     Shampoo B             A             2019-10-17 09:45:11

最终输出应如下所示:(添加具有值1和2的附加列SORT)

refcode      barcode          ItemDescription       status        createDate               sort
0075345        480000234512     Shampoo A             A             2017-09-22 10:12:29        1
0002345        480000234512     Shampoo A             A             2019-09-22 13:07:29        2
0010388        790000345217     Shampoo B             A             2018-05-13 08:34:01        1
0023754        790000345217     Shampoo B             A             2019-10-17 09:45:11        2

注释:

1和2可以是任何值---我要实现的是基于createdate

确定重复项

3 个答案:

答案 0 :(得分:0)

ROW_NUMBER应该在这里工作:

SELECT
    pl5.refcode,
    pl5.barcode,
    pl5.name,
    pl5.status,
    pl5.createDate,
    ROW_NUMBER() OVER (PARTITION BY pl5.barcode ORDER BY pl5.createDate) [sort]
FROM pList6 pl6
INNER JOIN plist5 pl5
    ON pl6.barcode = pl5.barcode
ORDER BY
    pl5.barcode,
    pl5.createDate;

ROW_NUMBER在上方被使用,它将为属于给定条形码的记录集生成一个序列,该序列按创建日期从1开始排序。

答案 1 :(得分:0)

尝试一下:

对于 SQL Server

select pl5.refcode, pl5.barcode, pl5.name, pl5.status, pl5.createDate
    ,ROW_NUMBER() OVER(PARTITION BY pl5.barcode ORDER BY pl5.createDate)[Sort]
from pList6 pl6
inner join plist5 pl5 on pl6.barcode = pl5.barcode
order by pl5.barcode, pl5.createDate;

对于 MySQL ,请尝试以下操作:

创建一个临时表并将结果数据插入到临时表中。

CREATE TEMPORARY TABLE Temp1(..........)
INSERT INTO Temp1 SELECT ..........

然后尝试一下。

SELECT 
  A.*, 
  @cur:= IF(barcode=@id, @cur+1, 1) AS RowNumber, 
  @id := barcode
FROM 
  Temp1 A CROSS JOIN 
    (SELECT @id:=(SELECT MIN(barcode) FROM Temp1), @cur:=0) AS init 
ORDER BY 
  A.barcode

答案 2 :(得分:0)

信贷转到DineshDB;

-通过该查询,我创建了2个临时表pList7和pList8,它们在下面得到了最终结果

选择pl5.refcode,pl5.barcode,pl5.name,pl5.status,pl5.createDate 来自pList6 pl6 pl6.barcode上的内部联接plist5 pl5 = pl5.barcode 按pl5.barcode,pl5.createDate排序;

如果不存在,则创建临时表pList7为 (从pList6 pl6中选择pl5.refcode,pl5.barcode,pl5.name,pl5.status,pl5.createDate pl6.barcode上的内部联接plist5 pl5 = pl5.barcode 按pl5.barcode,pl5.createDate排序);

创建临时表(如果不存在)pList8 as (从pList6 pl6中选择pl5.refcode,pl5.barcode,pl5.name,pl5.status,pl5.createDate pl6.barcode上的内部联接plist5 pl5 = pl5.barcode 按pl5.barcode,pl5.createDate排序);

选择    一种。*,    @cur:= IF(条形码= @ id,@ cur + 1,1)AS行号,    @id:=条码     从       pList7交叉加入      (SELECT @id:=(从pList8选择MIN(条形码),@cur:= 0)AS初始化           订购         A.条形码;