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


how to convert the below subquery to joins


how to convert the below subquery to joins

Author
Message
surendiran.balasubramanian
surendiran.balasubramanian
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 19
how to convert the below subquery into join ..

SELECT CtryId, WrkflwId, Dt
FROM stepHistory H
WHERE EXISTS
(SELECT * FROM
(SELECT WrkflwId, CtryId , max(DtTm) DtTm
FROM stepHistory
GROUP BY WrkflwId, ctryId
) S
WHERE S.WrkflwId = H.WrkflwId AND S.DtTm = H.DtTm AND H.CtryId = S.CtryId)
AND SteTyId = 2

----I have use the above query as inner join because i have to run the above query into an datawarehous applicance which wont support subquery

please help


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6080 Visits: 5280
Something like this

SELECT   H.CtryId, H.WrkflwId, H.Dt
FROM stepHistory H
INNER JOIN (
SELECT WrkflwId, ctryId, MAX( DtTm ) AS DtTm
FROM stepHistory
GROUP BY WrkflwId, ctryId
) AS SH ON H.WrkflwId = SH.WrkflwId AND H.CtryId = SH.CtryId AND H.DtTm = SH.DtTm
WHERE H.SteTyId = 2




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42115 Visits: 20008
surendiran.balasubramanian (4/19/2013)
how to convert the below subquery into join ..

SELECT CtryId, WrkflwId, Dt
FROM stepHistory H
WHERE EXISTS
(SELECT * FROM
(SELECT WrkflwId, CtryId , max(DtTm) DtTm
FROM stepHistory
GROUP BY WrkflwId, ctryId
) S
WHERE S.WrkflwId = H.WrkflwId AND S.DtTm = H.DtTm AND H.CtryId = S.CtryId)
AND SteTyId = 2

----I have use the above query as inner join because i have to run the above query into an datawarehous applicance which wont support subquery

please help


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


If the row in the inner query having the maximum date per CtryId and WrkflwId has a different SteTyId to the outer query, the outer query will drop the row unless the row with SteTyId = 2 has the same value for DtTm. Are you absolutely sure this query does what you are expecting it to?

“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
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