last day (15 days later) » 

1:35 PM
4
A: SQL Server Query - Sorting data using Order By in Union All

Salman AYou can add a faux column in all queries that identify the type of row (per year, total, monthly average and daily average2). Then sort the combined result by tenant, row type and month number, in that order: SELECT 1 AS [row type], tenantcode, [month], year1, year2, year3, year4, year5 FROM #Sa...

 
i need to put rowtype?
 
rowtype is a helper column to get the order of rows right. Its name does not matter, you can name it anything.
 
Month is not numeric based on the query set of #SalesPerYear. it is actually based on the month name and the code goes like this Order by DATEPART(MM,datename(month,date)+' 01 2011')
 
Anything that changes January to 1 and February to 2 (etc) will work. Remember that the order of unioned result could be different from the order of individual queries.
 
the Month name is not in right order sir... as you have said because in my current code, it is not in number
 
1:35 PM
@rickyProgrammer: most simple solution is to revise your first query to return the month number and sort by that column. You can return the month name as well but use it for displaying instead of sorting.
@rickyProgrammer see revised answer. I would still suggest that you modify #SalesPerYear to return month number to sort easily.
 
this is the error: ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
Considering your suggestion to revise the first query, but I really want my sorting in sql (backend) be in Name ordering , because it will be used as source in my Crystal Report
 
@rickyProgrammer Try changing 1st query to SELECT 1 AS [row type], DATEPART(MONTH, [month] + ' 1, 2000') AS [month code], [month], ..., 2nd...4th query to SELECT 2, NULL, #TotalSales.* ... and the order by clause to ORDER BY tenantcode, [row type], [month code]
 
It worked, only thing I did not like is that it created two additional columns instantly, I am really considering your first suggestion. Is it possible to make my month into number for easy sorting and display in in month in the final query to be displayed?
 
@rickyProgrammer it is simple: January for displaying, 1 for sorting. You can probably configure your report to display specific columns. If extra columns are an issue I think I have one other solution. See revised answer in a few minutes.
 
ok thanks.... please.
thanks so much for the effort! for you what is the most efficient approach to use since both is working, performance wise?
 
1:35 PM
I can't tell. An easy method to check which query is less expensive is to use SSMS: paste both queries in one window and select Query > View execution plan then use the query that has smaller query cost relative to batch.
 
this is really nice and interesting
 
if you see the execution plan tab, use the query which has smaller percentage
 
starting to locate it.. sorry, so in the Query menu.. i cant find the view execution plan
saw it...
 
which version of sql server are you using?
 
2008 sir
 
1:44 PM
it should be there, may be in some other menu
try pressing CTRL+SHIFT+M then F5
 
there's an icon Display Estimated Execution Plan..
Maybe that's it
?
 
yes, please click that, then run the qurey, you shuld then see a new tab next to results tab
 
it has... :)
 
click on it, it will show you two charts, you are interested in Query cost: nnn%
for each query
 
one approach has a total of 42 query to reach the final output, actually tried to make the example in my question shorter for easy understanding..
some of the queries in the first approach has 0% query cost and the highest has 15%
is that acceptable?
 
1:56 PM
i tought there were two queries?
 
still coding for the second query.. but at least you have me tought how to do it.. i can do it later and give you feedback
thank you so much for your generosity
 
you're welcome
 
i hope i could still count on you on future question! you are a blessing
 
i'll now delete the comments from the main question, it would be nice if you do the same
 

  last day (15 days later) »