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 «««12345»»»

Correlated Joins Using "Apply" Expand / Collapse
Author
Message
Posted Friday, February 27, 2009 7:57 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, November 21, 2014 6:34 AM
Points: 6,259, Visits: 2,031
OOps! I didn't realized this was and old post.


* Noel
Post #665745
Posted Friday, February 27, 2009 8:02 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 12:33 PM
Points: 392, Visits: 820
We've been using the ROW_NUMBER() method for a while now and it's great for reporting purposes. Not to mention, its speed is great compared to using UDFs or inserting into a temp table within a loop.

To get the top X records per group

SELECT * FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY ChowID ORDER BY ChowID, EndDate) as RowNumber, ChowID, EndDate, Name FROM StatLog) as a
WHERE a.RowNumber <= @TopCount ORDER BY a.Name, a.EndDate


/* Anything is possible but is it worth it? */
Post #665754
Posted Friday, February 27, 2009 8:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 6, 2010 1:30 PM
Points: 34, Visits: 82
If you are going to write a piece of code then you have an obligation to the user. Why? This is a service business. The user experience is the most important part of the service business.

If your code runs slower than another piece of code and both return the same result, you should use the other code. An article explaining the new "feature" runs poorer than the old "feature" may be providing a public service. But, I would really like to see code samples that help me provide the user with a better experience instead of a worse one.
Post #665767
Posted Friday, February 27, 2009 8:14 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 24, 2014 2:38 PM
Points: 1,386, Visits: 825
my first thought was to set up a CTE for the transactions, then join that to the Call data and use TOP 3. If the data can be returned on multiple rows i'd think this would be pretty fast (haven't tested it).
Post #665768
Posted Friday, February 27, 2009 8:15 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 24, 2014 2:38 PM
Points: 1,386, Visits: 825
P.S. great article!
Post #665769
Posted Friday, February 27, 2009 8:22 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 22, 2011 4:01 PM
Points: 7, Visits: 26
Does a regular correlated sub-query perform the same thing?

SELECT
lvt.ID,
lvt.PhoneNumber,
lvt.CreateDate,
lvc.CallWindowStart,
lvc.CallWindowEnd,
lvc.LVCallDispositionID
FROM
LVTransaction lvt
OUTER APPLY --<<<<<<<<<<<<<< Replace OUTER APPLY with WHERE.
(
SELECT top 3 *
FROM LVCall
WHERE lvtransactionID = lvt.ID
Order By CreateDate DESC
) as lvc

Post #665778
Posted Friday, February 27, 2009 8:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 24, 2014 2:38 PM
Points: 1,386, Visits: 825
nope. you'd get this error as stated in the article:

Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "lvt.ID" could not be bound.

Hence the use of apply (or row_number).
Post #665786
Posted Friday, February 27, 2009 8:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 22, 2011 4:01 PM
Points: 7, Visits: 26
Andy, I didn't do the same thing noted in the article:

--Correlated INNER JOIN Attempt
SELECT
lvt.ID,
lvt.PhoneNumber,
lvt.CreateDate,
lvc.CallWindowStart,
lvc.CallWindowEnd,
lvc.LVCallDispositionID
FROM
LVTransaction lvt
INNER JOIN
(SELECT TOP 3 * FROM LVCall WHERE LVTransactionID = lvt.ID ORDER BY CreateDate DESC) lvc
on lvc.LVTransactionID = lvt.ID -- <<<<<<<<<<<<<<<< mine DOESN'T have this.

Mine isn't a correlated inner join, it's a simple correlated subquery. Note the <<<<<<<<<<<'s. In mine, the inner query will reference the outside query for each row in the outside query. There is no "JOIN.... ON... "

Post #665794
Posted Friday, February 27, 2009 8:47 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 24, 2014 2:38 PM
Points: 1,386, Visits: 825
you're still looking for lvt.ID in a query that only has LVCalls in the from statement
Post #665801
Posted Friday, February 27, 2009 8:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 22, 2011 4:01 PM
Points: 7, Visits: 26
Ok. Sorry, I had a minor error in the WHERE Clause. I meant:

SELECT
lvt.ID,
lvt.PhoneNumber,
lvt.CreateDate,
lvc.CallWindowStart,
lvc.CallWindowEnd,
lvc.LVCallDispositionID
FROM
LVTransaction lvt
WHERE lvt.ID in ( --<<<<<<<<<<<<<<< THIS IS WHAT I MEANT
SELECT top 3 *
FROM LVCall
WHERE lvtransactionID = lvt.ID
Order By CreateDate DESC
) as lvc

Try this. It is essentially the same thing.

create table #T1 (pk int identity(1,1), C1 int)
create table #T2 (pk int identity(1,1), C2 int)

insert #T1 (C1) values (1)
insert #T1 (C1) values (2)

insert #T2 (C2) values (1)
insert #T2 (C2) values (2)

select *
from #T1 T1
where pk in (select pk from #T2 T2 where T1.pk = T2.pk)

drop table #T1
drop table #T2

Post #665811
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse