Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using Dense Rank


Using Dense Rank

Author
Message
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Or:


SELECT
rb.OFFICE_ID,
rb.BINDER_NO,
rb.BINDER_ID
FROM dbo.REV_BINDERS AS rb
WHERE
rb.BINDER_NO IN
(
SELECT
rb.BINDER_NO
FROM dbo.REV_BINDERS AS rb
GROUP BY
rb.BINDER_NO
HAVING
COUNT_BIG(*) = 1
);




+-----------+-----------+-----------+
| OFFICE_ID | BINDER_NO | BINDER_ID |
+-----------+-----------+-----------+
| 2105110 | 108 | 5 |
| 2101110 | 106 | 265 |
+-----------+-----------+-----------+




Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Or:


SELECT
OFFICE_ID = MAX(t.OFFICE_ID),
BINDER_ID = MAX(t.BINDER_ID),
t.BINDER_NO
FROM
(
SELECT
rb.*,
rn = ROW_NUMBER() OVER (
PARTITION BY rb.BINDER_NO
ORDER BY rb.BINDER_NO)
FROM dbo.REV_BINDERS AS rb
) AS t
GROUP BY
t.BINDER_NO
HAVING
MAX(t.rn) = 1;




+-----------+-----------+-----------+
| OFFICE_ID | BINDER_NO | BINDER_ID |
+-----------+-----------+-----------+
| 2101110 | 106 | 265 |
| 2105110 | 108 | 5 |
+-----------+-----------+-----------+




Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
I still like mine, just had to modify the partition by clause. I believe this is also one that Paul White post.



CREATE TABLE [REV_BINDERS](
[OFFICE_ID] [int] NOT NULL,
[BINDER_ID] [int] NOT NULL,
[BINDER_NO] [varchar](7) NULL,
);
go

INSERT INTO [REV_BINDERS]
([OFFICE_ID],[BINDER_ID],[BINDER_NO])
select 2105110 , 1 ,0101 union all
select 2105110 ,2 ,0102 union all
select 2105110 ,3 ,0103 union all
select 2105110 ,4 ,0104 union all
select 2105110 ,5 ,0108 union all
select 2105110 ,6 ,0105 union all
select 2101110 ,261 ,0101 union all
select 2101110 ,262 ,0102 union all
select 2101110 ,263 ,0103 union all
select 2101110 ,264 ,0104 union all
select 2101110 ,265 ,0106 union all
select 2101110 ,266 ,0105;

go

with emp as (
select
OFFICE_ID,
BINDER_ID,
BINDER_NO,
count(*) over (PARTITION BY BINDER_NO) as BinderCnt
from
REV_BINDERS )
select * from emp where BinderCnt = 1;

go

drop table [REV_BINDERS];
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search