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 Tuesday, June 25, 2013 10:02 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
Dear All

I have folloing query

select sum(yy) sumyy, 'summary line' xx
from tablea
where somedate_column < @startdate_parameter
union
select yy, xx
from tablea
where somedate_column between @startdate_parameter and @enddate_parameter

Is there a better way to achive this or this is the ok

Regards
Krishana

Post #1467436
Posted Wednesday, June 26, 2013 8:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,062, Visits: 11,891
Krishna1 (6/25/2013)
Dear All

I have folloing query

select sum(yy) sumyy, 'summary line' xx
from tablea
where somedate_column < @startdate_parameter
union
select yy, xx
from tablea
where somedate_column between @startdate_parameter and @enddate_parameter

Is there a better way to achive this or this is the ok

Regards
Krishana



Pretty hard to tell if that is the best approach or not. We can't see your tables, don't know the data or the requirements for the project. In addition, what you posted is only a portion of what appears to be a stored proc.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1467704
Posted Wednesday, June 26, 2013 2:58 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 2:05 PM
Points: 23, Visits: 92
Krishna1 (6/25/2013)
Dear All

I have folloing query

select sum(yy) sumyy, 'summary line' xx
from tablea
where somedate_column < @startdate_parameter
union
select yy, xx
from tablea
where somedate_column between @startdate_parameter and @enddate_parameter

Is there a better way to achive this or this is the ok

Regards
Krishana



I agree with what Sean Lange said. The only thing that you can do, which isn't much, is to change the BETWEEN condition in the second part of the UNION to be:

WHERE somedate_column >= @startdate_parameter and somedate_column <= @enddate_parameter

This is what the optimizer does in the background anyway, so save it the need to do so.
Post #1467880
Posted Wednesday, June 26, 2013 3:10 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,062, Visits: 11,891
Depending on the requirements you might be able to use UNION ALL instead of UNION which is certainly a performance improvement.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1467886
Posted Wednesday, June 26, 2013 10:13 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
Dear All

Table structure is as follows
Create table tablea(
Yy numeric(13,3),
Xx varchar(100),
somedate_column datetime)

What i wanted was like cube or rollup is there a way to make single query rather than union
Post #1467947
Posted Thursday, June 27, 2013 1:15 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, September 27, 2013 5:37 AM
Points: 306, Visits: 524
Are you using Index well on the table?
please specify





Neeraj Prasad Sharma
Sql Server Tutorials
Post #1467979
Posted Thursday, June 27, 2013 2:50 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:33 PM
Points: 2,222, Visits: 2,651
Krishna1 (6/26/2013)
What i wanted was like cube or rollup is there a way to make single query rather than union

These functions are available in SQL Server (2008+). Take a look at http://msdn.microsoft.com/en-us/library/bb522495(v=sql.105).aspx

So it would be something like:
SELECT
xx
, sum(Yy) as total
FROM tablea
GROUP BY ROLLUP (Xx)



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1468026
Posted Thursday, June 27, 2013 3:27 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
Dear Hanshi

Thanks for your reply. But if you see my query it has got differnt where clasues for the 2 query hence can not use roll up.

There is no performace issue as idexex are defined.

I only wnated to see if the same query could be written in any other better way.
Post #1468048
Posted Thursday, June 27, 2013 7:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 13,062, Visits: 11,891
I would change your UNION to a UNION ALL. It is better for performance and in your case you probably want to get both result sets even if they are the same.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1468140
Posted Thursday, June 27, 2013 7:27 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, July 18, 2014 2:05 PM
Points: 3,943, Visits: 2,957
Is the somedate_column indexed? What indexes do you have?


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1468141
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse