算你好,我有兩個表如下的Sql行與分組
tblContactType
typeId typeName active
1 Email 1
2 Phone 1
3 Address 1
4 Fax 1
tblContact
id IdName typeId groupId
100 test 1 1
101 test2 1 1
102 test3 1 2
103 test4 2 2
104 test5 2 3
105 test6 3 3
想要的結果要與列名的typeName數和組ID分組。結果應該是與某個組關聯的類型總數,這些類型與聯繫人相關聯。
GroupId EmailCount PhoneCount AddressCount FaxCount
1 2 0 0 0
2 1 1 0 0
3 0 1 1 0
你嘗試過這麼遠嗎?你可以通過左JOIN,GROUP BY,SUM和IIF或CASE – maSTAShuFu
來實現這一點,你知道數據透視查詢嗎? –