DROP TABLE dbo.[cust_rel_sample]
CREATE TABLE dbo.[cust_rel_sample]
(
[customer_id] [int] NULL,
[cnt_name] [int] NULL,
[id_name] [int] NULL,
[cnt_bank] [int] NULL,
[id_bank] [bigint] NULL,
[cnt_mobile] [int] NULL,
[id_mobile] [int] NULL,
[cnt_home] [int] NULL,
[id_home] [int] NULL,
[cnt_email] [int] NULL,
[id_email] [int] NULL,
[cnt_address] [int] NULL,
[id_address] [int] NULL
)
GO
;
INSERT INTO dbo.[cust_rel_sample]
VALUES
(1035608,1,NULL,1,NULL,1,NULL,1,NULL,1,NULL,7,635),
(1285215,1,NULL,1,NULL,1,NULL,1,NULL,1,NULL,7,635),
(1618173,2,1618173,4,1618173,1,NULL,4,1618173,1,NULL,1,NULL),
(2091361,1,NULL,1,NULL,1,NULL,1,NULL,1,NULL,7,635),
(2543284,4,2543284,1,NULL,1,NULL,4,1618173,2,2543284,1,NULL),
(1000875447,2,1000847636,1,NULL,1,NULL,2,1000847636,10,1000847636,1,NULL),
(1000941758,1,NULL,1,NULL,1,NULL,1,NULL,10,1000847636,1,NULL),
(1000941737,1,NULL,1,NULL,1,NULL,1,NULL,10,1000847636,1,NULL),
(1000962860,2,1000932977,1,NULL,1,NULL,3,1000932977,10,1000847636,1,NULL),
(1000242402,2,1707868,1,NULL,1,NULL,3,1000242383,4,1707868,1,NULL),
(1006384494,4,2543284,4,1618173,1,NULL,2,1006327204,4,1005964327,1,NULL),
(2543285,4,2543284,1,NULL,1,NULL,4,1618173,2,2543284,1,NULL),
(1000852813,1,NULL,1,NULL,1,NULL,1,NULL,2,1000852813,1,NULL),
(1000242383,2,1000242383,1,NULL,1,NULL,3,1000242383,4,1707868,1,NULL),
(1000847636,2,1000847636,1,NULL,1,NULL,2,1000847636,10,1000847636,1,NULL),
(1000932977,2,1000932977,1,NULL,1,NULL,3,1000932977,10,1000847636,7,635),
(1000978168,1,NULL,1,NULL,1,NULL,3,1000932977,10,1000847636,7,635),
(1000307069,2,1000242383,1,NULL,1,NULL,3,1000242383,4,1707868,1,NULL),
(1217037,1,NULL,1,NULL,1,NULL,1,NULL,1,NULL,7,635),
(1313183,1,NULL,1,NULL,1,NULL,1,NULL,1,NULL,7,635),
(1005964327,4,2543284,4,1618173,1,NULL,4,1618173,4,1005964327,1,NULL),
(1005977057,1,NULL,1,NULL,1,NULL,1,NULL,4,1005964327,1,NULL),
(1707868,2,1707868,1,NULL,1,NULL,1,NULL,4,1707868,1,NULL),
(1006327204,2,1618173,4,1618173,1,NULL,2,1006327204,4,1005964327,1,NULL),
(1000863810,3,1000863810,2,1000863810,1,NULL,3,1000863810,1,NULL,1,NULL),
(1000929465,3,1000863810,1,NULL,1,NULL,3,1000863810,10,1000847636,1,NULL),
(1000933009,3,1000863810,2,1000863810,1,NULL,3,1000863810,2,1000852813,1,NULL),
(1000941560,1,NULL,1,NULL,1,NULL,1,NULL,10,1000847636,1,NULL),
(1000977540,1,NULL,1,NULL,1,NULL,1,NULL,10,1000847636,1,NULL)
;with cte as(
select customer_id,id_name as id,DENSE_RANK() over(order by id_name) as cnt from dbo.[cust_rel_sample]
where id_name is not null
union all
select customer_id,id_email as id,DENSE_RANK() over(order by id_email) as cnt from dbo.[cust_rel_sample]
where id_email is not null
union all
select
customer_id,id_address as id,DENSE_RANK() over(order by id_address) as cnt from dbo.[cust_rel_sample]
where id_address is not null
union all
select customer_id,id_bank as id,DENSE_RANK() over(order by id_bank) as cnt from dbo.[cust_rel_sample]
where id_bank is not null
union all
select customer_id,id_mobile as id,DENSE_RANK() over(order by id_mobile) as cnt from dbo.[cust_rel_sample]
where id_mobile is not null
union all
select customer_id,id_home as id,DENSE_RANK() over(order by id_home) as cnt from dbo.[cust_rel_sample]
where id_home is not null)
select customer_id,count(customer_id)
from cte
group by customer_id