Search code examples
sqlsql-serversql-server-2000

SQL Query Grouping Two Columns Into One


Sorry for the vague Title.

I have a SQL Query

SELECT SaleDate, Location, SUM(DollarSales)
FROM [TableName]
WHERE (SaleDate= '2012-11-19' OR SaleDate= '2012-11-12')
GROUP BY SaleDate, Location
ORDER BY Location, SaleDate

I get the results as follows

SaleDate                    Location    (No column name)
2012-11-12 00:00:00.000     002         21500.38
2012-11-19 00:00:00.000     002         24166.81
2012-11-12 00:00:00.000     016         14723.26
2012-11-19 00:00:00.000     016         12801.00

I want the results to look like

Location    (Sale on 11/12)    (Sale on 11/19)
002         21500.38           24166.81
016         14723.26           12801.00

I don't want to use inner selects. Is there any way I can do this. And this is on one of our legacy systems that uses SQL Server 2000. Thank you in advance.


Solution

  • You could try:

    SELECT
    Location,
    SUM(CASE WHEN SaleDate = '2012-11-12' THEN DollarSales ELSE 0 END)'Sale on 11/12'
    SUM(CASE WHEN SaleDate = '2012-11-19' THEN DollarSales ELSE 0 END)'Sale on 11/19'
    FROM [TableName]
    GROUP BY Location
    ORDER BY Location
    

    This would only work if columns were static, you would have to change this if the dates change.