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 |
tyekhan
Starting Member
29 Posts |
Posted - 2013-03-19 : 20:26:46
|
Hi,I have one field in the database that have 5 different types of outcome , I need a query that will do the following,Area saletype Total1 , A45 , 42 , A007 , 52 , A005 , 23 , A775 , 53 , A001 , 24 , A775 , 45 , 4007 , 2what I would like to do is if the below saletype are used then to times the amount as below,A007 *2A005 *3 A775 *4all the other saletype will be just a count apart from the above they will need times the figures, so the above table will look like this,Area saletype Total1 , A45 , 42 , A007 , 102 , A005 , 63 , A775 , 203 , A001 , 24 , A775 , 205 , 4007 , 2I do have a query that will do 1 of the figures not all three. SELECT Area, SUM(CASE [Saletype] WHEN 'A007' THEN Total * 2 ELSE Total END) AS TotalFROM overallGROUP BY Areaoverall finish's table would look like this Area Total1 , 42 , 163 , 224 , 205 , 2 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2013-03-19 : 23:13:05
|
[code]SELECT area, SUM (CASE saletype WHEN 'A007' THEN total * 2 WHEN 'A005' THEN total * 3 WHEN 'A775' THEN total * 4 ELSE total END )FROM yourtableGROUP BY area[/code] |
|
|
tyekhan
Starting Member
29 Posts |
Posted - 2013-03-20 : 09:02:58
|
thanks, that worked fine but i'm added other table but it does not work for that,i have the below but it does not work,SELECT Overall.Place, SUM(CASE saletype WHEN 'EE002' THEN TotalSales * 2 WHEN 'EE003' THEN TotalSales * 3 WHEN 'EE004' THEN TotalSales * 4 ELSE TotalSales END) AS totalsales FROM Area INNER JOIN Overall ON Area.AreaID = Overall.AreaID INNER JOIN saletypeall ON Area.Salecode = saletypeall.SalecodeWHERE (Overall.ImportDate > CONVERT(DATETIME, '2013-02-24 00:00:00', 102))GROUP BY Overall.Place This is coming up with an error, |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-20 : 11:17:43
|
is TotalSales a calculated column?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
tyekhan
Starting Member
29 Posts |
Posted - 2013-03-20 : 11:59:14
|
Not in this query thats what i'll need to do but not sure how to do itquote: Originally posted by visakh16 is TotalSales a calculated column?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-20 : 13:06:16
|
can you post error message you got?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
tyekhan
Starting Member
29 Posts |
Posted - 2013-03-20 : 13:36:29
|
Its the below,SQL Execution Error.Exection SQL Statement: SELECT Overall.Place, SUM(CASE saletype WHEN 'EE002' THEN TotalSales * 2 WHEN 'EE003' THEN TotalSales * 3 WHEN 'EE004' THEN TotalSales * 4 ELSE TotalSales END) AS totalsales FROM Area INNER JOIN Overall ON Area.AreaID = Overall.AreaID INNER JOIN saletypeall ON Area.Salecode = saletypeall.SalecodeWHERE (Overall.ImportDate > CONVERT(DATETIME, '2013-02-24 00:00:00', 102))GROUP BY Overall.PlaceError Message: Invalid column name 'TotalSales'.Error Invalid column name 'TotalSales'.Error Invalid column name 'TotalSales'.Error Invalid column name 'TotalSales'.quote: Originally posted by visakh16 can you post error message you got?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-20 : 13:44:46
|
the error is obvious. you dont have the TotalSales column in any of the included tables.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|