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 |
|
cbrinson
Starting Member
36 Posts |
Posted - 2001-11-29 : 18:06:00
|
| The problem is that I wish to do a select and order by a user selected column.The following query works fine and orders by 'total' descending:SELECT COUNT(*) AS 'total' ,computerFROM TBL_Symantec_Log_EntriesWHERE event = 5 AND EventTime > '11/18/01'GROUP BY computerORDER BY 'total' descHowever, the following query does not work correctly and always orders by computer ascending:DECLARE @col AS varchar(15)SELECT @col = 'total'SELECT COUNT(*) AS 'total' ,computerFROM TBL_Symantec_Log_EntriesWHERE event = 5 AND EventTime > '11/18/01'GROUP BY computerORDER BY CASE @col WHEN 'total' THEN 'total' ELSE computer END DESCIt seems as though this should be possible. The problem seems to be related to referencing the count(*) column as 'total'. If I use this same query with two "real" table columns it works fine. Any ideas?Thanks,Chris |
|
|
LarsG
Constraint Violating Yak Guru
284 Posts |
Posted - 2001-11-29 : 18:25:59
|
| SELECT @col = 'total'SELECT COUNT(*) AS total,computerFROM TBL_Symantec_Log_EntriesWHERE event = 5 ANDEventTime > '11/18/01'GROUP BY computerORDER BY CASE when @col <> 'total' THEN computer end desc, total descIn your query, the first criteria will always be constant ('total') and will not have any implication on the ordering |
 |
|
|
mono
Starting Member
36 Posts |
Posted - 2001-11-30 : 08:03:37
|
cbrinson is right when he states:quote: It seems as though this should be possible. The problem seems to be related to referencing the count(*) column as 'total'. If I use this same query with two "real" table columns it works fine. Any ideas?
There is possible confusion between 'total' the string and 'total' the column. I'd use a different token e.g. a named constant for the @col variable. So you might have:DECLARE @TOTAL _COLDECLARE @COMPUTER_COL...CASE WHEN @COL = @TOTAL THEN [TOTAL] ELSE [COMPUTER]...The reason it is failing (I *think*) if is that...WHEN 'total' THEN 'total'...tells it to use the string 'total' not the column 'total'. Write it:...WHEN 'total' THEN total...or even better:...WHEN 'total' THEN [total]...to make it clear to us humans as well as to the parser that we are referring to a column not a string. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2001-12-01 : 00:32:52
|
| >> cbrinson is right when he states: It seems as though this should be possible. He is right it is possible and the server does it correctlyconsider rows123order these by 'total' then each row has the same literal 'total' so this has no effect on the order.There may be a bit of confusion caused by including the output column name in single quotes which is incorrect as it is an identifier not a literal. ==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|