Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Another way to do this

Author  Topic 

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2011-06-02 : 03:15:57
Hi everyone,

Is there any other way to do this.

SELECT TOP 10 C.ENCOUNTER_KEY, CA.ACTIVITY_KEY
FROM
(
SELECT ENCOUNTER_KEY, CLAIM_KEY,
ROW_NUMBER() OVER( PARTITION BY ENCOUNTER_KEY ORDER BY ISNULL(CLAIM_CREATION_DT, 0) DESC) Claim_RowNo
FROM CLAIM WITH(NOLOCK)
) C
INNER JOIN
(
SELECT CLAIM_KEY, ACTIVITY_KEY,
ROW_NUMBER() OVER( PARTITION BY CLAIM_KEY ORDER BY CLAIM_AMT_KEY DESC ) Claim_Amt_RowNo
FROM CLAIM_AMT WITH(NOLOCK)
) CA
ON C.CLAIM_KEY = CA.CLAIM_KEY AND Claim_RowNo = 1 AND Claim_Amt_RowNo = 1



In the above query
I am fetching encounter_key order by claim_creation_dt Desc
(first sub Query)
and joining with
latest Claim_key order by claim_amt_key desc from claim_amt table (second sub query)
from claim_amt table i want activity_key also.

Please help me out...

Vaibhav T

If I cant go back, I want to go fast...

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2011-06-02 : 03:22:49
Moreover I will get one row for one encounter_key.

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-06-02 : 05:51:30
maybe:


SELECT TOP 10 ENCOUNTER_KEY, ACTIVITY_KEY
FROM
(
SELECT
C.ENCOUNTER_KEY,
C.CLAIM_KEY,
CA.ACTIVITY_KEY
ROW_NUMBER() OVER( PARTITION BY ENCOUNTER_KEY, CLAIM_Key ORDER BY ISNULL(CLAIM_CREATION_DT, 0) DESC, CLAIM_AMT_KEY DESC) RowNo
FROM CLAIM C WITH(NOLOCK)
INNER JOIN CLAIM_AMT CA WITH(NOLOCK)
On C.CLAIM_KEY = CA.CLAIM_KEY
) A
Where RowNo = 1


Corey

I Has Returned!!
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2011-06-02 : 06:06:27
Query is perfect but its again taking very much time as before
I want some optimized way to do this.

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-06-02 : 06:11:50
How many rows in each table?

What indexes do you have on the tables?

Have you tried without the 'With(NoLock)'?

Are there ALOT of CLAIM_KEYs per ENCOUNTER_KEY? Like how many per ENCOUNTER_KEY?



Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -