請你幫我建立一個查詢。我有一個表如下來框架成本效益的查詢
Id Info_Id Type
1 2 2
2 6 2
3 5 3
4 8 3
5 2 3
6 2 2
7 5 2
8 8 2
9 5 2
10 8 2
11 8 2
12 5 3
13 6 3
14 8 3
查詢需要被框住,以便按「Info_Id」進行分組。
我需要如下輸出如:
Info_Id CountOfRec Type2 Type3
2 3 2 1
5 4 2 2
6 2 1 1
8 5 3 2
我嘗試如下,但我不是能夠得到高效輸出
select Info_Id, count(Id)as CountOfRec,
(select count(Id)from tbl_TypeInfo where Info_Id = 5 AND Type = 2) as Type2,
(select count(Id)from tbl_ TypeInfo where Info_Id = 5 AND Type = 3) as Type3
from tbl_TypeInfo
where Info_Id = 5
group by Info_Id
輸出是這樣的,
Info_Id CountOfRec Type2 Type3
5 4 2 2
(我必須爲每個「Info_id」循環才能獲得所需的OP,有一千條記錄nd其耗時)
我想從表中突出顯示的輸出。我已經構建的查詢效率不高,並且會有很好的解決方案,可以幫助我解決這個問題。
太棒了!非常感謝它的正常工作。 – 2011-03-07 08:32:10
我想爲Info_Id顯示計數爲零,而Info_Id不存在於上表中,例如:Info_Id像1,3。我怎麼能實現這個PLZ。 – 2011-03-08 08:54:54
@ fairy-g是否有另一張桌子可以放置** all ** info_id's?否則無法知道不存在的值。 – 2011-03-08 09:02:13