I have a sql table, in which for every change in a value in a certain column say Column C, I want to insert a new row under it to create a new transaction. I am not sure how to find that value change and insert a new row. I have been doing this through VB code on the csv file I Import into the table but unable to write it in 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
Here's the sample data
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
Below is the desired output
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
The column "concatenated" is where i'm trying to find a change and insert a row after every change. Any help would be appreciated.
you really want to write a stored procedure run either from you data source into you database or time depended in your database.
here is the code
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.