我使用查询作为存储过程,但COUNT函数不起作用,即它不计算coulmns。当我只分组三列时,它才能正常工作。但是分组超过三列创建问题,计数列分别显示值...不计算数字...帮助plzz
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 :(得分:0)
COUNT函数计算每个组中的行数。如果你添加
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
所以你可能做错了。
答案 1 :(得分: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