Hi all
I'm sorry if this question has been posted previously - I couldn't find evidence of such in six pages of searched posts; the nearest I came to was Results from Query as Headers
[blue]Background[/blue]
I have monthly sales information in a table which is stored as SalesQty1 through to SalesQty12 with 1 being the most recent previous month's data and so on.
Every month, as part of the month-end routine, the data is archived along a month - so SalesQty1's previous value is passed to SalesQty2, 2 to 3 and so on (SalesQty12's previous value is lost as this would then fall outside of the previous 12 months).
I know nothing off dynamic SQL, and can only currently use MSQuery (query engine associated with Microsoft Excel) to interrogate the database (SQL 2000). The month-names are not being extracted from any table data
[blue]Current output[/blue]
In March, I currently see
[tt][blue]StockCode Description CurrentUsage [highlight]SalesQty1[/highlight] [highlight]SalesQty2[/highlight] [highlight]SalesQty3[/highlight][/blue]
Item00001 A big shake 47 21 104 1[/tt]
[blue]Desired output[/blue]
I want my report to output the month name rather than SalesQty1, SalesQty2 etc - this will obviously change dependent upon the month in which the report is being refreshed:
In March, I'd like to see
[tt][blue]StockCode Description CurrentUsage [highlight]Feb09[/highlight] [highlight]Jan09[/highlight] [highlight]Dec08[/highlight][/blue]
Item00001 A big shake 47 21 104 1[/tt]
In April, I'd like to see
[tt][blue]StockCode Description CurrentUsage [highlight]Mar09[/highlight] [highlight]Feb09[/highlight] [highlight]Jan09[/highlight][/blue]
Item00001 A big shake 232 47 21 104[/tt]
[blue]Attempt[/blue]
I gather that in order to return the month name (e.g. Jan, Feb, Mar, etc) , I'd need something along the lines of the following in order to generate the abbreviated names of the previous month, the month before and the month before that:
I can return this in a normal [tt]SELECT [/tt] statement.
However, when I try to implement this to return the values as column labels, I get an error message
[red][tt]Didnt expect '(' after the SELECT column list[/tt][/red]
Mark, somewhere near Blackburn Lancs!
I'm sorry if this question has been posted previously - I couldn't find evidence of such in six pages of searched posts; the nearest I came to was Results from Query as Headers
[blue]Background[/blue]
I have monthly sales information in a table which is stored as SalesQty1 through to SalesQty12 with 1 being the most recent previous month's data and so on.
Every month, as part of the month-end routine, the data is archived along a month - so SalesQty1's previous value is passed to SalesQty2, 2 to 3 and so on (SalesQty12's previous value is lost as this would then fall outside of the previous 12 months).
I know nothing off dynamic SQL, and can only currently use MSQuery (query engine associated with Microsoft Excel) to interrogate the database (SQL 2000). The month-names are not being extracted from any table data
[blue]Current output[/blue]
In March, I currently see
[tt][blue]StockCode Description CurrentUsage [highlight]SalesQty1[/highlight] [highlight]SalesQty2[/highlight] [highlight]SalesQty3[/highlight][/blue]
Item00001 A big shake 47 21 104 1[/tt]
[blue]Desired output[/blue]
I want my report to output the month name rather than SalesQty1, SalesQty2 etc - this will obviously change dependent upon the month in which the report is being refreshed:
In March, I'd like to see
[tt][blue]StockCode Description CurrentUsage [highlight]Feb09[/highlight] [highlight]Jan09[/highlight] [highlight]Dec08[/highlight][/blue]
Item00001 A big shake 47 21 104 1[/tt]
In April, I'd like to see
[tt][blue]StockCode Description CurrentUsage [highlight]Mar09[/highlight] [highlight]Feb09[/highlight] [highlight]Jan09[/highlight][/blue]
Item00001 A big shake 232 47 21 104[/tt]
[blue]Attempt[/blue]
I gather that in order to return the month name (e.g. Jan, Feb, Mar, etc) , I'd need something along the lines of the following in order to generate the abbreviated names of the previous month, the month before and the month before that:
Code:
DATENAME(m, DateAdd("m", -1, GETDATE()))
DATENAME(m, DateAdd("m", -2, GETDATE()))
DATENAME(m, DateAdd("m", -3, GETDATE()))
I can return this in a normal [tt]SELECT [/tt] statement.
However, when I try to implement this to return the values as column labels, I get an error message
[red][tt]Didnt expect '(' after the SELECT column list[/tt][/red]
Code:
SELECT MyTable.SalesQty1 AS DATENAME(m, DateAdd("m", -1, GETDATE()))
FROM MyDatabase.dbo.MyTable
WHERE MyTable.Warehouse = 'BL'
Mark, somewhere near Blackburn Lancs!