473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL - query improvement MAX(date), IN

Hi,
I wonder if you could shed some light into this.

I have the following table.
Id, ContentId, VersionDate, ContentXml

There are several ContentIds in the table.

SELECT *
FROM tblVersions
WHERE (VersionDate =
(SELECT MAX(tblVersions2.VersionDate)
FROM tblContentVersion AS
tblVersions2
WHERE tblVersions2.ContentiD =
tblVersions.ContentiD))
ORDER BY ContentId
This query works to select the latest versions (MAX) of every content,
but I do not like it, any other way to do this properly?

I also want to do this knowing a set of ids (probably using IN )

SELECT *
FROM tblVersions
WHERE (VersionDate =
(SELECT MAX(tblVersions2.VersionDate)
FROM tblContentVersion AS
tblVersions2
WHERE tblVersions2.ContentiD =
tblVersions.ContentiD AND tblVersions.ContentiD IN (1, 2, 3, 6, 7, 8)
))
ORDER BY ContentId
Any ideas for improvements on this query?

ContentXml is of ntext type

Thanks,
/ jorge
Jul 23 '05 #1
4 3706
Hiya Jorge,

The corelated subquery is a good solution - although depending on
tablesizes it might not be optimal (subquery is executed once for
everyrow returned in the parent query). I'd suggest to check the
indexes and stats first. Secondaly you could try the following..

SELECT
*
FROM tblVersions a join
(select
contentid,
MAX(versionDate) as max_versiondate
from
tblContentVersion
group by
contentid)dt
where a.contentid=dt.contentid and
a.versiondate=dt.max_versiondate

Jul 23 '05 #2
Sorry type'o on the query..

SELECT
*
FROM tblVersions a join
(select
contentid,
MAX(versionDate) as max_versiondate
from
tblContentVersion
group by
contentid)dt
ON a.contentid=dt.contentid and
a.versiondate=dt.max_versiondate

Jul 23 '05 #3
Jorge (jo***************@gmail.com) writes:
I also want to do this knowing a set of ids (probably using IN )

SELECT *
FROM tblVersions
WHERE (VersionDate =
(SELECT MAX(tblVersions2.VersionDate)
FROM tblContentVersion AS
tblVersions2
WHERE tblVersions2.ContentiD =
tblVersions.ContentiD AND tblVersions.ContentiD IN (1, 2, 3, 6, 7, 8)
))
ORDER BY ContentId
Any ideas for improvements on this query?


A good alternative to the subquery is to use derived table as Greg
suggested. Then you can easily add a filer on the id:s

SELECT *
FROM tblVersions a
join (select contentid,
MAX(versionDate) as max_versiondate
from tblContentVersion
group by contentid) dt
ON a.contentid=dt.contentid and
a.versiondate=dt.max_versiondate
WHERE a.ContentID IN (1, 2, 3, 6, 7, 8)

If you want to pass the list of ids as a parameter, check out
http://www.sommarskog.se/arrays-in-s...st-of-integers.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Thank you for your answers.

The ids are actually Guids.

/ jorge delgado lopez

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: 400NEWBIE | last post by:
Hi everyone, I'm having trouble figuring out how to select the max date in MS ACCESS from an AS400 table with a composite key. Here's my data: Type Name Select-Rule Error-Rule Effec-Date...
2
by: zdk | last post by:
I have table name "actionlog",and one field in there is "date_time" date_time (Type:datetime) example value : 11/1/2006 11:05:07 if I'd like to query date between 24/07/2006 to 26/07/2006(I...
1
by: Freddie | last post by:
Hello everyone: i am trying to compare a MAX(DATE) from one table that would be greater than a date in another. first_table compname MYDATE abc comp 2006-09-26 09:19:43.250
9
by: robtyketto | last post by:
Is there anyway to change the max date to datenow + 2 years I tried this in the properties -> Format -> MaxDate = dateadd("yyyy",2,date()) without success :( Can anyone help. Thanks, a...
1
by: Anne150585 | last post by:
Hi, I was wandering if anyone could give me some help... The idea is that I want to create a stored procedure that will check a bunch of tables and give me the latest 'LastUpdated' field ...
1
by: abetancur | last post by:
I have the following code: SELECT MAX(INVENTORY_TRANSACTION_HIST.DATE_APPLIED) AS LAST_TRANS, INVENTORY_TRANSACTION_HIST.PART_NO FROM INVENTORY_TRANSACTION_HIST INNER JOIN ...
5
by: HoganGroup | last post by:
Hi Experts and Fellow Duffers: I am trying to create a query which will identify the most recent EndDate in tblSchedules associated with tblKids.KidID where a Null value (indicating a current...
3
Inbaraj
by: Inbaraj | last post by:
Hi... I have a Table in that i have inserted the DATE as VARCHAR now in that i Want to get the Max Date. I am new to MySQL Plz help me how to find the max date. with regard Inbaraj.D
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.