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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any way to use this SQL statement without grouping by Type?

Status
Not open for further replies.

joshs1100

IS-IT--Management
Jan 24, 2002
23
US
SELECT Table1.Item,
CASE (Table2.Type)
WHEN 'Variable' THEN SUM(Table1.Cost)
END AS V_Cost,
CASE (Table2.Type)
WHEN 'Fixed' THEN SUM(Table1.Cost)
END AS F_Cost
FROM Table1 INNER JOIN
Table2 ON
Table1.TYPE = Table2.Type
GROUP BY Table1.Item,
Table2.Type
 
The SUM function should be outside the CASE function rather than inside.

SELECT Table1.Item,
SUM(CASE Table2.Type
WHEN 'Variable'
THEN Table1.Cost
ELSE 0 END) AS V_Cost,
SUM(CASE Table2.Type
WHEN 'Fixed'
THEN Table1.Cost
ELSE 0 END) AS F_Cost
FROM Table1 INNER JOIN
Table2 ON
Table1.TYPE = Table2.Type
GROUP BY Table1.Item Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top