Question

So I have a table where it dense_ranks my rows. Here is the table:

COL1  | COL2  | COL3  | DENSE_RANK |

  a   |   b   |   c   |       1    |
  a   |   s   |   r   |       1    |
  a   |   w   |   f   |       1    |
  b   |   b   |   c   |       2    |
  c   |   f   |   r   |       3    |
  c   |   q   |   d   |       3    |

So now I want to select any rows where the rank was only represented once, so the 2 is all alone, but not the 1 or 3. I want to select all the rows where this occurs, but how do I do that?

Some ideas:

  -COUNT DISTINCT (RANK())
  -COUNT RANK()

but neither of those are working, any ideas? please and thank you!

happy hacking

actual code:

SELECT events.event_type AS "event", 
       DENSE_RANK() OVER (ORDER BY bw_user_event.pad_id) as rank 
FROM user_event 
WHERE (software_events.software_id = '8' OR software_events.software_id = '14') 
  AND (software_events.event_type = 'install')
Was it helpful?

Solution

WITH Dense_ranked_table as (
 -- Your select query that generates the table with dense ranks
)
SELECT DENSE_RANK
FROM Dense_ranked_table
GROUP BY DENSE_RANK
HAVING COUNT(DENSE_RANK) = 1;

I don't have SQL Server to test this. So please let me know whether this works or not.

OTHER TIPS

I would think you can add a COUNT(*) OVER (PARTITION BY XXXXX) where XXXXX is what you include in your dense rank.

Then wrap this in a Common Table Expression and select where your new Count is = 1.

Something like this fiddler: http://sqlfiddle.com/#!6/ae774/1

Code included here as well:

CREATE TABLE T
(
COL1 CHAR,
COL2 CHAR,
COL3 CHAR
);

INSERT INTO T
VALUES 
('a','b','c'),
('a','s','r'),
('a','w','f'),
('b','b','c'),
('c','f','r'),
('c','q','d');

WITH CTE AS (
  SELECT  COL1 ,
        COL2 ,
        COL3, 
        DENSE_RANK() OVER (ORDER BY COL1) AS DR,
        COUNT(*) OVER (PARTITION BY COL1) AS C
FROM dbo.T AS t

 )
SELECT COL1, COL2, COL3, DR
FROM CTE 
WHERE C = 1

Would return just the

b, b, c, 2 

row from your test data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top