I have multiple columns with some amount in a table and I want to show the total of all those amounts in the last Total column. I have a table in sql which looks somewhat like this,
A_Amt B_Amt C_Amt D_Amt E_Amt F_Amt ... Total
------------------------------------------------
15 20 25 30 35 40
I do not want to do sum(col1 + col2 + ..)
like this because there are many columns. Is there another way to get the sum using a where clause like **where columnname.name like '%Amt%'**
?
I am trying to use UNPIVOT
and I came up with the following code but it is not working,
select Product_ID, Amount
FROM Products
unpivot
(Amount for Product_ID in
(select c.name + '' from syscolumns c(nolock) where (id =
(select id from dbo.sysobjects where name = 'Products'))
and (c.name like '%Amt%')))
Any ideas will be helpful.