Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-07-20 : 04:09:12
|
Hi, I have data in a table wid 3 columns.Invoice_no, item, price....example:-Invoice_no item price102 my item #1 $6.21102 my item #2 $3.56102 my item #3 $4.28105 my item #4 $1.90105 my item #5 $3.66107 my item #6 $2.01I wanted to convert this data into something like this :--( adding 2 mroe columns)Invoice_no item price invoice_line invoice_line#102 my item #1 $6.21 3 1102 my item #2 $3.56 3 2102 my item #3 $4.28 3 3105 my item #4 $1.90 2 1105 my item #5 $3.66 2 2107 my item #6 $2.01 1 1Let me know how to add those 2 columns Regards,SushantDBAVirgin Islands(U.K) |
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2012-07-20 : 04:59:32
|
ALTER TABLE YourTableName ADD [invoice_line] INT, [invoice_line#] INTSQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-07-20 : 05:05:13
|
quote: Originally posted by sql-programmers ALTER TABLE YourTableName ADD [invoice_line] INT, [invoice_line#] INTSQL Server Programmers and Consultantshttp://www.sql-programmers.com/
Ok...But how to populate those 2 columns with the data as intended.?Regards,SushantDBAVirgin Islands(U.K) |
 |
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2012-07-20 : 05:25:44
|
You need create update query for each row using primary key.May i know , Did you stored that values in any table or have any logic to update that values in that column?SQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2012-07-20 : 05:46:18
|
hiassuming that invoiceLine is number of row per invoice and invoiceLine# is current row in invoicehere is with Aas (select 102 as Invoice_no, 'my item #1' as item, $6.21 as priceunion allselect 102,'my item #2', $3.56union allselect 102, 'my item #3', $4.28union allselect 105,'my item #4', $1.90union allselect 105 ,'my item #5', $3.66union allselect 107 ,'my item #6', $2.01)select A.*,B.invoiceLine ,row_number() over (partition by A.invoice_no order by A.item asc ) as invoice_line#from A inner join (select count (invoice_no)as invoiceLine , invoice_no from A group by invoice_no)B on A.invoice_no=B.Invoice_noS |
 |
|
|
|
|
|
|