I am a bit stuck with building a query to count how often the same detail data is stored in a master-detail relationship in a ms sql server database.
I got a master table, which is not that much important.
MASTER_ID
1
2
3
4
What's important is the following detail table.
DID MASTERID No Value
1 1 1 220
2 1 2 250
3 1 3 250
4 2 1 220
5 2 2 250
6 3 1 220
7 3 2 250
8 4 1 220
9 4 2 230
The detail table has n rows, the column No
(int) is a sequential number for the same masterid
. The value is a decimal value.
I would like to group and count the rows that I know how often the exact same detail data is used for the master rows. Definition of the same data: The same number of detail rows and each detail row must be the same (columns no and value).
The wanted result:
Count No Value
1 1 220 (DID 1)
1 2 250 (DID 2)
1 3 250 (DID 3)
2 1 220 (DID 4 and 6)
2 2 250 (DID 5 and 7)
1 1 220 (DID 8)
1 2 230 (DID 9)
The first three rows have count 1 because there is no other master row with exactly these three no and value details. The next two rows have count 2 because there are two master rows which have 1-220 and 2-250 as detail rows. The last two rows have count one because there is only one master which has 1-220 and 2-230 as detail rows.
It's important to understand that for example
No Value
1 150
2 170
and
No Value
1 170
2 150
is not seen as same detail data because of the different order (column no).
Any ideas?