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 |
|
choto
Starting Member
1 Post |
Posted - 2008-12-03 : 22:45:18
|
| Hello EveryoneI am creating a temporary table for storing a matrix. The final step i do is to Insert the sum of all columns of that matrix as a row in the same table.Which usuall appears the first row of that table.While retrieveing the data from the temporary table the Sum row(Which contains column wise sum) appears usually as the first row when displayed in the data grid.How can i get it as the last row. Is there any way to insert as the last row of the table or i should retrieve it in such a way that it appears as the last Row? what is the best way and plzz let me know how to acheive that?Note: All creation of temp table and sum calculation and insertion of the calculated result is done using a procedure.The example below is just an analogy to my actual table.Eg:Name | a | b | c | dTotal | 13 | 14 | 15 | 12asdf | 3 | 4 | 5 | 6qwert | 5 | 6 | 8 | 4zxcvbv| 5 | 4 | 2 | 2In the above example the total of all rows is stored as the first row of the table.When i issue a command like SELECT * from Tablename the Total row appears as the first row in the Data Grid which ideally should be the last. I tried following:"select ... order by case when name='Total' 1 else 0 end, name"But got the following error"Incorrect syntax near '1'"THANKS |
|
|
cvraghu
Posting Yak Master
187 Posts |
Posted - 2008-12-03 : 23:38:15
|
| Its better not to store the sum of columns in table. Instead you can have two queries one with normal select and the other with the SUM of columns from the same table and union them. |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-12-04 : 01:23:23
|
| You always need to specify ORDER BY to get a specific order and this is no different. You will have to add or derrive a sequence column and order by that making sure your total row has something suitably large (or small and order by ... desc).e.g. select ... order by case when name='sum' 0 else 1, name |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-04 : 02:03:00
|
quote: Originally posted by LoztInSpace You always need to specify ORDER BY to get a specific order and this is no different. You will have to add or derrive a sequence column and order by that making sure your total row has something suitably large (or small and order by ... desc).e.g. select ... order by case when name='sum' 0 else 1, name
shouldnt it be other way around?select ... order by case when name='Total' 1 else 0 end, name |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-04 : 03:22:57
|
it should beselect ... order by case when name='Total' then 1 else 0 end, name |
 |
|
|
|
|
|