I found 85 duplicated names in my base with
SELECT Name, COUNT(*)
FROM Tab
GROUP BY Name
HAVING COUNT(*) > 1
ORDER BY 2 DESC, 1;
with the result set:
Name COUNT(*)
a 28
b 12
c 10
d 8
e 5
f 4
g 3
h 3
i 2
...
My concern is to sort these duplicated names (303 entries in total) per status
SELECT Status, COUNT(*) FROM Tab
WHERE Name IN (SELECT Name FROM Tab GROUP BY Name HAVING COUNT(*) > 1)
GROUP BY Status
ORDER by Name;
give me this result:
Status COUNT(*)
Ended 38
Deleted 21
InUse 244
Now I would like a combinaison of both queries meaning to show the duplicated names counted by status.
Name | Ended | Deleted | InUse |
---|---|---|---|
a | 6 | 2 | 20 |
b | 0 | 0 | 12 |
c | 0 | 8 | 2 |
d | 6 | 1 | 1 |
e | 4 | 0 | 1 |
f | 0 | 3 | 1 |
g | 1 | 1 | 1 |
h | 1 | 2 | 0 |
i | 1 | 0 | 1 |
If I can have an extra column as Total will be great also but my main goal is to build a query for this result set
2
Answers
An example of Nathan_Sav’s suggestion –
you can use Window function For Calculation Count and Create To Result with Pivot
This Code work to use sql server
You can to insert the basic data with the following codes