我在 Microsoft SQL Server 中有一个数据,格式如下:
id1 id2 month quantA quantB
1 10 1 5 15
1 10 1 10 20
1 10 2 5 10
1 10 2 10 NULL
1 11 1 NULL NULL
1 11 2 5 NULL
1 11 2 10 5
2 10 1 10 20
2 10 1 5 NULL
2 11 2 NULL NULL
我需要构建一个按以下列分组id1
的表:month
id1
month
var1 = count how many *distinct* id2 by month and id1 for which quantA!=Null
var2 = count how many *distinct* id2 by month and id1 for which quantB!=Null