Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query improvement


Query improvement

Author
Message
Krishna1
Krishna1
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
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)
jkassay
jkassay
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 115
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
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)
Krishna1
Krishna1
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
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
Neeraj Prasad Sharma
Neeraj Prasad Sharma
Old Hand
Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)

Group: General Forum Members
Points: 313 Visits: 675
Are you using Index well on the table?
please specify

Neeraj Prasad Sharma
Sql Server Tutorials
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3048 Visits: 3625
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’! **
Krishna1
Krishna1
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
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)
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10215 Visits: 9549
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
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