|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, January 19, 2013 8:28 AM
Points: 1,038,
Visits: 255
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 4:57 AM
Points: 1,
Visits: 81
|
|
I noticed an interesting feature when I do "Grouping by Two Columns" on table with PrimaryKey (Clustered Index Unique):
select ProductID,Shelf,sum(Quantity) from Production.ProductInventory group by ProductID,Shelf
In this case, the result is automatically sorted by the second column from GroupBy clause “Shelf”. Is not that strange? In certain situations, the ExecutionPlan shows that SQL Engne executes the illogical/invert sort. Why executes any sort?
This is not the case when we do "Grouping by Tree Columns or More".
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 10:40 PM
Points: 20,
Visits: 28
|
|
Good article... One comment though...
I would not filter on a row value in a having statement, the code is wasting CPU cycles and too much memory swapping.
The last example would be an illustration of that. SELECT StoreName ,SUM(TotalSalesAmount) AS StoreSalesAmount FROM dbo.SalesTransaction GROUP BY StoreName HAVING StoreName LIKE '%Outlet%' <==== NOT GOOD OR StoreName LIKE '%Books%';
This should be better written like: SELECT StoreName ,SUM(TotalSalesAmount) AS StoreSalesAmount FROM dbo.SalesTransaction WHERE StoreName LIKE '%Outlet%' <=== This way the row is not even loaded in to memory to be aggregated. OR StoreName LIKE '%Books%'; GROUP BY StoreName
|
|
|
|