|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:39 PM
Points: 6,260,
Visits: 1,977
|
|
OOps! I didn't realized this was and old post.
* Noel
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:29 PM
Points: 387,
Visits: 681
|
|
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? */
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, April 06, 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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 6:59 AM
Points: 1,386,
Visits: 823
|
|
| 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).
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 6:59 AM
Points: 1,386,
Visits: 823
|
|
|
|
|
|
Forum 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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 6:59 AM
Points: 1,386,
Visits: 823
|
|
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).
|
|
|
|
|
Forum 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... "
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 6:59 AM
Points: 1,386,
Visits: 823
|
|
| you're still looking for lvt.ID in a query that only has LVCalls in the from statement
|
|
|
|
|
Forum 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
|
|
|
|