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
 General SQL Server Forums
 New to SQL Server Programming
 Rank most popular/most used term?

Author  Topic 

Chloe_19
Starting Member

44 Posts

Posted - 2012-03-25 : 23:24:32
I have this code which simply gives me the count of term.

 SELECT 
OUA_ID

,COALESCE (MAX( DECODE( TERM, 0603, TERM_COUNT, NULL ) ), 0 ) "TERM_0603_CNT"
,COALESCE (MAX( DECODE( TERM, 0702, TERM_COUNT, NULL) ), 0 ) "TERM_0702_CNT"
,COALESCE (MAX( DECODE( TERM, 0705, TERM_COUNT, NULL ) ), 0 ) "TERM_0705_CNT"
,COALESCE (MAX( DECODE( TERM, 0707, TERM_COUNT, NULL ) ), 0 ) "TERM_0707_CNT"


FROM(

SELECT DISTINCT
OUA_ID, TERM_COUNT, TERM
FROM(
SELECT OUA_ID, TERM ,PROVIDER_CDE, COUNT(*) TERM_COUNT
FROM TABLE
WHERE PROVIDER_CDE = 'BILL'
GROUP BY OUA_ID, TERM, PROVIDER_CDE)) GROUP BY OUA_ID


Gives:

OUA_ID	TERM_0603_CNT	TERM_0702_CNT	TERM_0705_CNT	TERM_0707_CNT
32710 0 3 7 0
45726 2 0 1 0




Is there a way i could used the rank function (with decode) to only display the 1ST (First) and 2nd(Second) most used term per OUA_ID?


So i get this:
OUA_ID	1st MOST USED	2nd MOST USED 
3271 705 702
45726 603 705

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-26 : 13:03:46
seems like Oracle code. This is MS SQL Server forum. Please post it in Oracle forum like www.orafaq.com

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Chloe_19
Starting Member

44 Posts

Posted - 2012-03-27 : 00:03:21
Thannks i relise that.
But i find the people in this forum most helpful.
Even if it is MS SQL it can be easily interpreted.
(but i guess this question is a little more difficult)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-27 : 12:04:42
in SQL Server, your requirement can be achieved using DENSE_RANK window function

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -