I have a narrow table containing unique key and source data
| Unique_Key | System |
|---|---|
| 1 | IT |
| 1 | ACCOUNTS |
| 1 | PAYROLL |
| 2 | IT |
| 2 | PAYROLL |
| 3 | IT |
| 4 | HR |
| 5 | PAYROLL |
I want to be able to pick a system as a base - in this case IT - then create a dynamic SQL query where it counts:
- distinct unique key in the chosen system
- proportion of shared unique key with other systems. These systems could be dynamic and there are lot more than 4
I'm thinking of using dynamic SQL and PIVOT to first pick out all the system names outside of IT. Then using IT as a base, join to that table to get the information.
select distinct Unique_Key, System_Name
into #staging
from dbo.data
where System_Name <> 'IT'
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(System_Name)
FROM #staging
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Unique_Key, ' + @cols + ' into dbo.temp from
(
select Unique_Key, System_Name
from #staging
) x
pivot
(
count(System_Name)
for System_Name in (' + @cols + ')
) p '
execute(@query)
select *
from
(
select distinct Unique_Key
from dbo.data
where System_Name = 'IT'
) a
left join dbo.temp b
on a.Unique_Key = b.Unique_Key
So the resulting table is:
| Unique_Key | PAYROLL | ACCOUNTS | HR |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 0 |
| 3 | 0 | 0 | 0 |
What I want is one step further:
| Distinct Count IT Key | PAYROLL | ACCOUNTS | HR |
|---|---|---|---|
| 3 | 67% | 33% | 0% |
I can do a simple join with specific case when/sum statement but wondering if there's a way to do it dynamically so I don't need to specify every system name.
Appreciate any tips/hints.