Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
stevelalor
Starting Member
4 Posts |
Posted - 2007-07-27 : 04:55:13
|
Hi all, I'm trying to product a total of two columns but everything I try seems to notwork for me.Here is a outline to my SQL so far.SELECT mo.ModelId, NewSales = (Select COUNT(1) FROM tblDiaryEntries WHERE ModelId = mo.ModelId AND New = 1 AND RID = 1 AND SPID = 3565), UsedSales = (Select COUNT(1) FROM tblDiaryEntries WHERE ModelId = mo.ModelId AND New = 0 AND RID = 1 AND SPID = 3565), SUM(NewSales + UsedSales) AS TotalFROM tblModels moEverything works ok apart from my Total column, I have even tried to replacethe NewSales + UsedSales with the selects I used to get those two columnsorginally (Effectively performing the SELECT TWICE.This produced a "Cannot perform an aggregate function on an expressioncontaining an aggregate or a subquery."Please could someone point me in the right direction? I basically need to beable to work out the total of the new and used sales columns and put this inthe third "Total" column.Many thanksSteve |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-07-27 : 05:46:45
|
You want the same total returned with all rows? or you want sum of NewSales and UsedSales of each row? |
 |
|
stevelalor
Starting Member
4 Posts |
Posted - 2007-07-27 : 06:01:57
|
HiI would like the total per row.ThanksSteve |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-27 : 07:38:20
|
[code]SELECT m.ModelID, SUM(CASE WHEN de.New = 1 THEN 1 ELSE 0 END) AS NewSales, SUM(CASE WHEN de.New = 0 THEN 1 ELSE 0 END) AS UsedSales COUNT(de.New) AS TotalFROM tblModels AS mLEFT JOIN tblDiaryEntries AS de ON de.ModelID = m.ModelID AND de.RID = 1 AND de.SPID = 3565GROUP BY m.ModelIDORDER BY m.ModelID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|