SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Max or Top 1 more efficient


Max or Top 1 more efficient

Author
Message
tshad
tshad
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1012 Visits: 454
I have a table with a month value and year value both ints.

I need two things, the latest date and the date 24 months before.

I came up with these to get the latest date and trying to find the most efficient.

SELECT MAX(CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar))) AS ValueDate
FROM dbo.myTable


This gets me a plan of

Stream Aggregate (Aggregate) -Cost 7%
Compute Scalar - Cost 1%
Clustered Index Scan - Cost 91%

The other option is:

SELECT TOP 1
CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar)) AS ValueDate
FROM dbo.myTable
ORDER BY ValueDate DESC


Sort (top N Sort) -Cost 92%
Compute Scalar - Cost 0%
Clustered Index Scan - Cost 8%

Which one is the most efficient? The costs seem equivelant.

The whole query (depending on which select I use) would be:


DECLARE @ValueDate smalldatetime

SELECT @ValueDate =
(SELECT TOP 1
CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar)) AS ValueDate
FROM dbo.myTable
ORDER BY ValueDate DESC)

SELECT DATEADD(month, -23, @ValueDate)



Thanks,

Tom
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93571 Visits: 38955
Or this:



with MaxDate as (
select
max(dateadd(month, MonthValue - 1, dateadd(year, YearValue - 1900, 0))) as MaxDate
from
dbo.DealerForecastSegmentValue
)
select
md.MaxDate,
dateadd(month, -23, md.MaxDate) as MinDate
from
MaxDate;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5122 Visits: 10605
How long do they take to run?

How long does a potentially SARGable
SELECT  MAX(CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar))) AS ValueDate
FROM (
SELECT TOP 1 YearValue, MonthValue
FROM dbo.myTable
ORDER BY YearValue DESC, MonthValue DESC
) d

query take?


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)

Group: General Forum Members
Points: 212093 Visits: 41977
tshad (4/24/2013)
I have a table with a month value and year value both ints.

I need two things, the latest date and the date 24 months before.

I came up with these to get the latest date and trying to find the most efficient.

SELECT MAX(CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar))) AS ValueDate
FROM dbo.myTable


This gets me a plan of

Stream Aggregate (Aggregate) -Cost 7%
Compute Scalar - Cost 1%
Clustered Index Scan - Cost 91%

The other option is:

SELECT TOP 1
CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar)) AS ValueDate
FROM dbo.myTable
ORDER BY ValueDate DESC


Sort (top N Sort) -Cost 92%
Compute Scalar - Cost 0%
Clustered Index Scan - Cost 8%

Which one is the most efficient? The costs seem equivelant.

The whole query (depending on which select I use) would be:


DECLARE @ValueDate smalldatetime

SELECT @ValueDate =
(SELECT TOP 1
CONVERT(smalldatetime, CAST(MonthValue AS varchar) + '/01/' +
CAST(YearValue AS varchar)) AS ValueDate
FROM dbo.myTable
ORDER BY ValueDate DESC)

SELECT DATEADD(month, -23, @ValueDate)



Thanks,

Tom


A better thing to do would be to add a persisted calculated column to do the datetime conversion and then index that. Things will be much faster that way.

As a bit of a sidebar, a more efficient method for doing the calculation would be to get rid of all the VARCHAR conversions and stick to integer math. It's pretty simple, too.

DateTimeValue = DATEADD(mm,YearValue*12-22801+MonthValue,0)




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ScottPletcher
ScottPletcher
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19365 Visits: 7410
Just in case, IF you already have an index on ( YearValue, MonthValue ), then this should be extremely fast:


SELECT
MAX(YearValue) AS YearValue, MAX(MonthValue) AS MonthValue --or some computation to turn this into a datetime
FROM dbo.myTable
WHERE
YearValue = ( SELECT MAX(YearValue) FROM dbo.myTable )



SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
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