| Author |
Topic |
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2012-01-27 : 11:08:55
|
| I have a table that I need to find the top 500 used CPT Codes, Which I know how to do. Which is this...SELECT TOP (500)clms_proc, COUNT(clms_proc) AS 'cptcount'From dbo.ConsiliumClaims c Join impact.dbo.clms on clms_id = clm_id1Where CLM_DOUT between '1/1/2010' and '12/31/2011' and negdup is null and iserror <> 1GROUP BY clms_procORDER BY 'cptcount' DESCNow I want to take those 500 and add them details (total charges, client, etc....This is what I have for that...Select* from (Select Distinctclm_id1,clm_65a as Client, TIN, clm_tchg as TotalCharges, clm_sppo as TotalSavings, accessfeefinal,[Closed Status],clm_net as Network,clmsID,CPTCODE,claimLine, medicarefrom dbo.ConsiliumClaims Left Join (Select clms_id as clmsID, clms_proc as CPTCODE, clms_line as claimLine, clms_uac as medicare from impact.dbo.clms )cpt on cpt.clmsID = clm_id1Where CLM_DOUT between '1/1/2010' and '12/31/2011' and negdup is null and iserror <> 1) Table1How can I link the first query with this or am I going about doing this the wrong way.My main goal is to find the top utilized CPTCODES and them later sum the total charges with the client name aside them.Thanks for you help. |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2012-01-27 : 11:28:39
|
Use it as a derived table and inner join to it on the clms_proc field.  select...FROM(...) Table1INNER JOIN(SELECT TOP (500)clms_proc, COUNT(clms_proc) AS 'cptcount'From dbo.ConsiliumClaims c Join impact.dbo.clms on clms_id = clm_id1Where CLM_DOUT between '1/1/2010' and '12/31/2011' and negdup is null and iserror <> 1GROUP BY clms_procORDER BY 'cptcount' DESC) top500onTable1.clms_proc = top500.clms_proc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-27 : 11:36:08
|
| what if count values are same for lot of codes? do you need to accomodate all of them or only stop at first 500?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2012-01-30 : 09:31:30
|
I have to make sure that I am pulling the most used Codes. It's okay that they are the same codes. I hope that anwers your question.quote: Originally posted by visakh16 what if count values are same for lot of codes? do you need to accomodate all of them or only stop at first 500?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2012-01-30 : 09:33:08
|
Thank you rick. This worked perfectly!quote: Originally posted by RickD Use it as a derived table and inner join to it on the clms_proc field.  select...FROM(...) Table1INNER JOIN(SELECT TOP (500)clms_proc, COUNT(clms_proc) AS 'cptcount'From dbo.ConsiliumClaims c Join impact.dbo.clms on clms_id = clm_id1Where CLM_DOUT between '1/1/2010' and '12/31/2011' and negdup is null and iserror <> 1GROUP BY clms_procORDER BY 'cptcount' DESC) top500onTable1.clms_proc = top500.clms_proc
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
|
|
|