I have a table in production with 12 columns for each month. I need to create a SP where I pass in a productID, Customer and Month parameter and retrieve the sum of that month.
currently my logic is
if month = 1 then
select sum(JAN) from table where productID = @id and customer = @cust
if month = 2 then
select SUM(FEB) from table where productID = @id and customer = @cust
....
The query is a bit more involved but this is the core of it. Is there any way around these “IF” statements?
Edit – This is an SQL Server 2000 database but will be migrated to SQL Server 2005, the PIVOT and UNPIVOT will come in handy when I move to SQL Server 2005.
Assuming you declare and inialize the variables, this should work. It’s more complicated of course if you wan to do multiple months, that’s one reason why the design is not the best for querying.