SQL Server Update,来自同一个表的连接

时间:2017-02-14 14:30:43

标签: sql tsql

我已经设法从平面txt文件创建我的表,但由于这个布局,时间戳不在正确的位置。该文件如下所示:

13-Feb-17 01:14

N1 SUNDRY 331/6203 ASTOCK 000000  PICK   111111    0     3   0   0 268          

13-Feb-17 01:15

N1 SUNDRY 397/5747 ASTOCK 000000  PICK   111111    0     2   0   0 268          

13-Feb-17 06:09

MP GRENDZ 606/0484        000000         060472    0     0   0   0 193
A5 GRENDZ 606/0484        000000         060472    1    26   5   0   3
MP GRENDZ 346/9789        000000         060704    1     0   5   0 304
A5 GRENDZ 346/9789        000000         060704    1     8   5   0   3

所以当我用:

导入到新表时
    -- SELECT DATABASE
    USE [CHDS_Sandbox]
    GO
    -- TRUNCATE TABLE
    TRUNCATE TABLE [dbo].[LogScan]
    GO
-- INSERT NEW DATA
INSERT INTO [dbo].[LogScan]
            (move_type, program, cat, from_loc, pallet_no, to_loc, grds, pallets, items, tme, clk_num, id_num, movement_key, move_stamp, t_stamp)
SELECT
-- 0 AS id_als,
CASE WHEN SUBSTRING(Field1,1,2) BETWEEN '0' AND '99' THEN '' ELSE SUBSTRING(Field1,1,2) END AS move_type,
CASE WHEN SUBSTRING(Field1,4,6) LIKE '%-%' THEN '' ELSE SUBSTRING(Field1,4,6) END AS program,
CASE WHEN SUBSTRING(Field1,11,8) LIKE '%:%' THEN '' ELSE SUBSTRING(Field1,11,8) END AS cat,
SUBSTRING(Field1,20,6) AS from_loc,
0+SUBSTRING(Field1,27,6) AS pallet_no,
SUBSTRING(Field1,35,6) AS to_loc,
0+SUBSTRING(Field1,42,6) AS grds,
0+SUBSTRING(Field1,49,4) AS pallets,
0+SUBSTRING(Field1,54,5) AS items,
0+SUBSTRING(Field1,60,3) AS tme,
0+SUBSTRING(Field1,64,3) AS clk_num,
0+SUBSTRING(Field1,68,4) AS id_num,
SUBSTRING(Field1,72,9) AS movement_key,
CASE
    WHEN SUBSTRING(Field1,4,3)='JAN' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='FEB' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='MAR' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='APR' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='MAY' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='JUN' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='JUL' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='AUG' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='SEP' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='OCT' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='NOV' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
    WHEN SUBSTRING(Field1,4,3)='DEC' THEN CONVERT(DATETIME2(0),(SUBSTRING(Field1,1,15)),101)
END AS move_stamp,
GETDATE() AS t_stamp
FROM [CHDS_Staging].[dbo].[csv_ImportedData]
WHERE SUBSTRING(Field1,11,8) LIKE '%/%' OR SUBSTRING(Field1,11,5) LIKE '%:%'   AND SUBSTRING(Field1,27,6) NOT LIKE '%:%'
GO

但是这给了我:enter image description here

我需要在它下面的每个单元格中移动标记,直到它发生变化。

对此的任何帮助都会很棒。

这在MySQL中有用:

   UPDATE dbo.LogScan t1
   JOIN (
   SELECT id_als, @s:=IF(move_stamp IS NULL, @s, move_stamp) move_stamp
   FROM (SELECT * 
   FROM dbo.LogScan 
   ORDER BY id_als) r,
   (SELECT @s:='') t
   ) t2
   ON t1.id_als = t2.id_als
   SET @t1.move_stamp = t2.move_stamp;

1 个答案:

答案 0 :(得分:0)

我写了下面的查询,我希望我没有任何语法错误。 请检查并查看这是否适合您。 我假设你的is-als列是一个int列。 如果你想删除行1,3,5,10等空行,那么请包含查询中当前注释的where条件 您可以使用此查询来更新行

   WITH StampCTE AS (
      SELECT ([id-als]+1) AS id-als,move_stamp
      FROM [dbo].[LogScan]
      WHERE move_stamp IS NOT NULL
    )  
    SELECT
        T1.id_als,
        T1.move_type, 
        T1.program, 
        T1.cat, 
        T1.from_loc, 
        T1.pallet_no, 
        T1.to_loc, 
        T1.grds, 
        T1.pallets, 
        T1.items, 
        T1.tme, 
        T1.clk_num, 
        T1.id_num, 
        T1.movement_key, 
        T1.move_stamp ,
        T2.move_stamp AS [NEW COLUMN MOVED] ,  
        T1.t_stamp
    FROM [dbo].[LogScan] T1
    LEFT JOIN StampCTE T2 ON T1.id_als = T2.id-als
    --WHERE RTRIM(LTRIM(T1.move_type)) <> '' 

更新声明:

I would first run the select and if you see that thats what you like then use the below:



    WITH StampCTE AS ( SELECT ([id_als]+1) AS id_als,move_stamp 
    FROM [dbo].[LogScan] WHERE move_stamp IS NOT NULL 
    ) 
    UPDATE T1 
    SET T1.move_stamp = T2.move_stamp 
    FROM [dbo].[LogScan] T1
    LEFT JOIN StampCTE T2 ON T1.id_als = T2.id_als

以下更新语句会更新后面一行的每一行,如果你有很多数据,它可能会很慢:

DECLARE @ID INT;

WHILE (SELECT COUNT(*) FROM [dbo].[LogScan] WHERE move_stamp IS NULL) > 0
BEGIN

    SELECT TOP(1) @ID = id_als from [dbo].[LogScan]
    WHERE move_stamp IS NULL
    ORDER BY id_als; 

    WITH StampCTE AS ( 
    SELECT DISTINCT move_stamp 
    FROM [dbo].[LogScan] WHERE id_als = @ID-1
    ) 
    UPDATE [dbo].[LogScan] T1
    SET T1.move_stamp = StampCTE.move_stamp 

END


SELECT * FROM [dbo].[LogScan]