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 |
|
callahan77
Starting Member
2 Posts |
Posted - 2006-02-02 : 10:57:36
|
| Hi all!I hope anybody can help meI try to make a query that groups and calculate lots of datasimple example:SELECT name,A1 = SUM(CASE WHEN TEMP IN (1,2,3,4,5) AND qtime < 5 THEN 1 ELSE 0 END)A2 = SUM(CASE WHEN TEMP IN (1,2,3,4,5) AND qtime > 10 THEN 1 ELSE 0 END)A_All = SUM(CASE WHEN TEMP IN (1,2,3,4,5) AND qtime < 5 THEN 1 ELSE 0 END) + SUM(CASE WHEN TEMP IN (1,2,3,4,5) AND qtime > 10 THEN 1 ELSE 0 END)FROM tabelleGROUP BY name;So you can see that I need always the same part of the case statement.??? Is there any possibility to make this more comfortable???Thanks a lot for your answers!!! |
|
|
Norwich
Posting Yak Master
158 Posts |
Posted - 2006-02-02 : 11:58:29
|
Is the query working?If I understand you correctly, you just want to make your query shorter.If it's working then you could go the dynamic query route ie.:Declare @case varchar(100), @Sql Varchar (400)Set @case = 'SUM(CASE WHEN TEMP IN (1,2,3,4,5) AND qtime'Set @Sql = 'SELECT name, A1 = ' + @case + '< 5 THEN 1 ELSE 0 END) , A2 = ' + @case + '> 10 THEN 1 ELSE 0 END) , A_All = ' + @case + ' < 5 THEN 1 ELSE 0 END) + ' + @case + ' > 10 THEN 1 ELSE 0 END) FROM tabelle GROUP BY name'Print @Sql--Execute (@SQL) but that would be overkill It Doesn't even make the query shorter!NThe revolution won't be televised! |
 |
|
|
callahan77
Starting Member
2 Posts |
Posted - 2006-02-03 : 02:02:27
|
| Thanks for your hint!Yes - The query works!i only want to make the query shorter and more confortable to modify, because like in the example above, my whole query has 13.000 characters and this is very hard to modify.So i try your way, maybe it is shorter. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|