Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

different columns

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
I don't know how to approach this problem.

I have a query

SELECT SUM(Sold_Sum) Sold, Sale_Dt
FROM Sales
WHERE
Dimen = 'D1' AND SaleMonth = 2
GROUP BY Sale_Dt

This query is fine, gives me the Total sales for February

Now I need to get another column, to show the Total sales for March

(I need to display on a graph, to compare)

SELECT SUM(A.Sold_Sum) ASold, SUM(B.Sold_Sum) BSold, Sale_Dt
FROM Sales A, Sales B

as far as I got, not sure how to write the WHERE clause

Can anyone help?

Marika
 
You may try something like this:
SELECT Sale_Dt,
SUM(CASE WHEN SaleMonth=2 THEN Sold_Sum ELSE 0 END) SoldFeb
SUM(CASE WHEN SaleMonth=3 THEN Sold_Sum ELSE 0 END) SoldMar
FROM Sales
WHERE Dimen = 'D1' AND SaleMonth BETWEEN 2 And 3
GROUP BY Sale_Dt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks, I run your code and its almost what I need. I realised that to compare I cannot display the Sale_Dt, rather I need to display Day(Sale_Dt)

Now all I need to do is to display FebSale for day 1 and MarSale for day 1 next to each other FebSale for day2 and MArSale for day 2 next to each other and so on

like
Day FebSale MarSale
1 23444 23333
2 78890 90000

Do you know how I can do that?
 
SELECT Day(Sale_Dt),
SUM(CASE WHEN SaleMonth=2 THEN Sold_Sum ELSE 0 END) SoldFeb
SUM(CASE WHEN SaleMonth=3 THEN Sold_Sum ELSE 0 END) SoldMar
FROM Sales
WHERE Dimen = 'D1' AND SaleMonth BETWEEN 2 And 3
GROUP BY Day(Sale_Dt)
ORDER BY 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top