Ich habe es geschafft, meine Tabelle aus der Flat-TXT-Datei zu erstellen, aber aufgrund des Layouts ist der Zeitstempel nicht an der richtigen Stelle. Die Datei liest sich wie folgt:SQL Server Update mit Join aus der gleichen Tabelle
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
Also, wenn ich in der neuen Tabelle importieren mit:
-- 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
aber das gibt mir: enter image description here
ich den Umzug Stempel in jedem der Notwendigkeit Zellen darunter, bis es sich ändert.
Jede Hilfe dazu wäre großartig.
Dies ist, was in MySQL gearbeitet:
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;
Wo würde dies gehen i die Update-Anweisung? Oder füge ich es einfach zum Einsatz hinzu? –
Just updated the post – BSAMEI
Hallo BSAMEI es ist in der Nähe, aber es füllt nicht alles in ADN Ich würde diese Funktion brauchen, um auf der INSERT oder lassen Sie es die UPDATE direkt danach und es muss in der move_stamp bleiben. –