我有一个sql表,其中对于某列中某个值的每个更改都说C列,我想在其下插入一个新行来创建一个新事务。我不确定如何找到值更改并插入新行。我一直在通过csv文件上的VB代码执行此操作我导入到表中但无法在SQL中编写它。
Sub InsertRows()
Dim r As Long, mcol As String, i As Long
' find last used cell in Column A
r = Cells(Rows.Count, "A").End(xlUp).Row
' get value of last used cell in column A
mcol = Cells(r, 1).Value
' insert rows by looping from bottom
For i = r To 2 Step -1
If Cells(i, 1).Value <> mcol Then
mcol = Cells(i, 1).Value
Rows(i + 1).Insert
End If
Next i
End Sub
以下是示例数据
ID JOB FNAME LNAME ADDRESS1 ADDRESS2 DATE Concatenated
1234 A John Smith 4378 Anna St Seattle-WA-98040 12/24/2013 1234-A-41632
1234 A John Doe 3564 Lucie Ave Mercer Island-WA-98040 12/24/2013 1234-A-41632
1235 A Alex Smith 4554 Devon Ave Chicago-IL-60563 12/24/2013 1235-A-41632
1235 A Eli Manning 5555 Stranz Lane Dallas-TX-75213 12/24/2013 1235-A-41632
1233 B John Smith 4378 Anna St Seattle-WA-98040 12/24/2013 1233-B-41632
1233 C John Doe 3564 Lucie Ave Mercer Island-WA-98040 12/24/2013 1233-C-41632
1236 D Alex Smith 4554 Devon Ave Chicago-IL-60563 12/24/2013 1236-D-41632
1236 E Eli Manning 5555 Stranz Lane Dallas-TX-75213 12/24/2013 1236-E-41632
以下是所需的输出
ID JOB FNAME LNAME ADDRESS1 ADDRESS2 DATE Concatenated
1234 A John Smith 4378 Anna St Seattle-WA-98040 12/24/2013 1234-A-41632
1234 A John Doe 3564 Lucie Ave Mercer Island-WA-98040 12/24/2013 1234-A-41632
1235 A Alex Smith 4554 Devon Ave Chicago-IL-60563 12/24/2013 1235-A-41632
1235 A Eli Manning 5555 Stranz Lane Dallas-TX-75213 12/24/2013 1235-A-41632
1233 B John Smith 4378 Anna St Seattle-WA-98040 12/24/2013 1233-B-41632
1237 C John Doe 3564 Lucie Ave Mercer Island-WA-98040 12/24/2013 1237-C-41632
1236 D Alex Smith 4554 Devon Ave Chicago-IL-60563 12/24/2013 1236-D-41632
1236 E Eli Manning 5555 Stranz Lane Dallas-TX-75213 12/24/2013 1236-E-41632
“concatenated”列是我试图找到更改并在每次更改后插入一行的地方。 任何帮助将不胜感激。
答案 0 :(得分:0)
你真的想要编写一个存储过程,从你的数据源运行到你的数据库或者你的数据库中依赖的时间。
这是代码
BEGIN;
ALTER TABLE Test_table ADD COLUMN b1(10,2);
UPDATE test_table SET romney_pct = CAST(romney as DECIMAL(10,2))/ CAST(uspres_total AS DECIMAL(10,2);
COMMIT;