2017-06-10 8 views
-1

ストアドプロシージャとしてクエリを使用していますが、COUNT関数が動作していません。 3つの列だけをグループ化するとうまく動作します。しかし、問題とカウント列を作成する以上の3つの列をグループ化することは値を示し別に...数字を数えていない...助けplzzMS SQLのCOUNT関数

CREATE PROCEDURE[dbo].[getNoOfComplaints_SP] 
@Depart_ID INT, 
@City_ID INT, 
@Nature_ID INT, 
@Detail_ID INT, 
@Status_ID INT, 
@Category_ID INT, 
@Source_ID INT, 
@Gender_ID INT 

AS BEGIN SET NOCOUNT ON; 
SELECT Department_Info.Depart_Name, COUNT(Complaint_Info.Depart_ID) AS NumberOfComplaints 
FROM Complaint_Info 
LEFT JOIN Department_Info 
ON Complaint_Info.Depart_ID = Department_Info.Depart_ID 
GROUP BY 
Department_Info.Depart_Name, 
Complaint_Info.City_ID, 
Complaint_Info.Nature_ID, 
Complaint_Info.Detail_ID, 
Complaint_Info.Status_ID, 
Complaint_Info.Cat_ID, 
Complaint_Info.Source_ID, 
Complaint_Info.G_ID 
Having 
(@City_ID = -1 OR Complaint_Info.City_ID = @City_ID) 
AND 
(@Nature_ID = -1 OR Complaint_Info.Nature_ID = @Nature_ID) 
AND 
(@Detail_ID = -1 OR Complaint_Info.Detail_ID = @Detail_ID) 
AND 
(@Status_ID = -1 OR Complaint_Info.Status_ID = @Status_ID) 
AND 
(@Category_ID = -1 OR Complaint_Info.Cat_ID = @Category_ID) 
AND 
(@Source_ID = -1 OR Complaint_Info.Source_ID = @Source_ID) 
AND 
(@Gender_ID = -1 OR Complaint_Info.G_ID = @Gender_ID) 
END 
+0

あなたは、コードをしてくださいフォーマットしてもらえますか? – hardkoded

+0

フォーマットの手引き:https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks – Jake

+0

いずれにせよ、GROUP BY句のフィールドはあなたのSELECT句。 HAVINGをWHEREに変更する必要があります。 – Jake

答えて

0

COUNT関数は、各グループ内の行をカウントします。 GROUP BY句で

Department_Info.Depart_Name, 
Complaint_Info.City_ID, 
Complaint_Info.Nature_ID, 
Complaint_Info.Detail_ID, 
Complaint_Info.Status_ID, 
Complaint_Info.Cat_ID, 
Complaint_Info.Source_ID, 
Complaint_Info.G_ID 

を追加した場合、COUNT関数は同じDepart_Name、CITY_ID、Nature_ID、Detail_ID、STATUS_ID、CAT_ID、SOURCE_ID、G_IDを持っているエントリの数を返します。

おそらくあなたは間違っているでしょう。私は私の問題を解決し

0

...

CREATE PROCEDURE[dbo].[getNoOfComplaints_SP] 
@Depart_ID INT, 
@City_ID INT, 
@Nature_ID INT, 
@Detail_ID INT, 
@Status_ID INT, 
@Category_ID INT, 
@Source_ID INT, 
@Gender_ID INT 
AS 
BEGIN 
SET NOCOUNT ON; 
SELECT Department_Info.Depart_Name, COUNT(abc.Depart_ID) AS NumberOfComplaints FROM 
(SELECT Complaint_Info.Depart_ID, COUNT(Complaint_Info.Depart_ID) AS NumberOfComplaints FROM Complaint_Info 
GROUP BY Complaint_Info.Depart_ID, Complaint_Info.City_ID, Complaint_Info.Nature_ID, Complaint_Info.Detail_ID, Complaint_Info.Status_ID, Complaint_Info.Cat_ID, Complaint_Info.Source_ID, Complaint_Info.G_ID 
Having 
(@Depart_ID = -1 OR Complaint_Info.Depart_ID = @Depart_ID) 
AND 
(@City_ID = -1 OR Complaint_Info.City_ID = @City_ID) 
AND 
(@Nature_ID = -1 OR Complaint_Info.Nature_ID = @Nature_ID) 
AND 
(@Detail_ID = -1 OR Complaint_Info.Detail_ID = @Detail_ID) 
AND 
(@Status_ID = -1 OR Complaint_Info.Status_ID = @Status_ID) 
AND 
(@Category_ID = -1 OR Complaint_Info.Cat_ID = @Category_ID) 
AND 
(@Source_ID = -1 OR Complaint_Info.Source_ID = @Source_ID) 
AND 
(@Gender_ID = -1 OR Complaint_Info.G_ID = @Gender_ID)) AS abc 
JOIN Department_Info ON abc.Depart_ID = Department_Info.Depart_ID 
Group By Department_Info.Depart_Name 
END