SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Order one column alphabetically while keeping a Total Line Footer at Bottom of Table


Order one column alphabetically while keeping a Total Line Footer at Bottom of Table

Author
Message
OP_HELP
OP_HELP
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 354
Hello,

I've written this SQL , running it in Access 2003, which produces the information I need, including a footer showing the Total.

However, I would like to Sort Col1 alphabetically (From A-Z), but so far, has been unsuccessful. Below is the main pseudcode for this:


SELECT “Total“ AS Col1, “ “ AS Col2, “ “ AS Col3, Count([TableA.Column1]) AS [Count_TableA_Column1]
FROM…TableA INNER JOIN TableB INNER JOIN TableC
UNION
SELECT TableB.Col1, TableB.Col2, TableB.Col3, Count([TCPIP Table].[Term #]) AS [CountOfTerm #]
GROUP BY TableB.Col1, TableB.Col2, TableB.Col3
ORDER BY Col1, [Count_TableA_Column1]

Col1 Col2 Col3 CountOfTerm#
A - - 5
D - - 4
Total 11
Z - - 2



When I remove the "Col1" from the "Order by" in the second SELECT, I get the "Total" row pushed to the bottom, but then the Col1 is all unordered like this, but the Total appears at the bottom, as I want it.

Z 2
D 4
A 5
Total 11

Is there a way to order by Column 1 while still getting the Total Row at the bottom? Thanks for any help
OP_HELP
OP_HELP
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 354
I've reached a temporary solution. I added a space ( " "+ [TableB.Col1] ) to the second Select statement in the Union query, inserting a space before each cell of data. So, when it sorts, it sorts everything with the Space first, and then the "Total" which has no space before it in the first Select.
Dwayne Dibley
Dwayne Dibley
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 3171
SELECT “Total“ AS Col1, “ “ AS Col2, “ “ AS Col3, Count([TableA.Column1]) AS [Count_TableA_Column1], 2 as roworder
FROM…TableA INNER JOIN TableB INNER JOIN TableC
UNION
SELECT TableB.Col1, TableB.Col2, TableB.Col3, Count([TCPIP Table].[Term #]) AS [CountOfTerm #],1 as roworder
GROUP BY TableB.Col1, TableB.Col2, TableB.Col3
ORDER BY roworder,Col1, [Count_TableA_Column1]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search