Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Query improvement Expand / Collapse
Author
Message
Posted Thursday, June 27, 2013 7:34 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 6:08 AM
Points: 2,482, Visits: 3,028
Krishna1 (6/27/2013)
But if you see my query it has got differnt where clasues for the 2 query hence can not use roll up.

I overlooked that the WHERE clause is different. In that case I don't think there is another way to write the query. But I agree with Sean Lange to replace your UNION with the UNION ALL statement if your resultset doesn't have to be GROUPED BY.


** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1468148
Posted Thursday, June 27, 2013 7:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
Why read the table twice?
SELECT 
sumyy = sum(yy),
xx = ???('summary line') -- where ??? is an aggregate function
FROM tablea
CROSS APPLY (
SELECT SurrogateGroup = CASE
WHEN somedate_column < @startdate_parameter THEN <<constant value>>
WHEN somedate_column between @startdate_parameter and @enddate_parameter THEN <<tablea.PK>>
ELSE NULL end
) x
WHERE SurrogateGroup IS NOT NULL -- or use somedate_column <= @enddate_parameter
GROUP BY SurrogateGroup



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1468160
Posted Thursday, June 27, 2013 8:02 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 8:26 AM
Points: 2,386, Visits: 7,623
ChrisM@Work (6/27/2013)
Why read the table twice?
SELECT 
sumyy = sum(yy),
xx = ???('summary line') -- where ??? is an aggregate function
FROM tablea
CROSS APPLY (
SELECT SurrogateGroup = CASE
WHEN somedate_column < @startdate_parameter THEN <<constant value>>
WHEN somedate_column between @startdate_parameter and @enddate_parameter THEN <<tablea.PK>>
ELSE NULL end
) x
WHERE SurrogateGroup IS NOT NULL -- or use somedate_column <= @enddate_parameter
GROUP BY SurrogateGroup



Careful with this approach Chris, the constant scan generated is often worse than just hitting the table twice depending on indexes, data size etc. I do always like to test that approach when doing something that needs a "working" column, but it often loses out in my environment.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1468166
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse