我需要根据Month显示来自多个表的行为事件的总计数。最重要的是,即使计数为0,我也需要显示几个月。我创建了一个Months表来存储该数据,然后加入行为/学生表来获取计数。我的左外连接条款之一被忽略了,我无法弄清楚原因:
SELECT ISNULL(i5_btincident.syr, '1617') AS 'SchoolYear'
,m.MonthNumber
,Count(i5_btincident.incident_id) AS 'Incidentct'
,m.sortorder
,m.MonthName
FROM Months m
LEFT OUTER JOIN i5_btincident i5_btincident
ON m.MonthNumber = month(i5_btincident.incident_date)
AND i5_btincident.sch = '310'
AND i5_btincident.dst = '002'
AND i5_btincident.syr = '1617'
--and i5_btincident.positive_or_negative like :f1_RB_Behavior.BehCode
LEFT OUTER JOIN i5_btstupart i5_btstupart
ON (i5_btincident.syr = i5_btstupart.syr)
AND (i5_btincident.incident_ID = i5_btstupart.incident_ID)
AND (i5_btincident.dst = i5_btstupart.dst)
AND (i5_btincident.sch = i5_btstupart.sch)
LEFT OUTER JOIN i5_student
ON i5_student.syr = i5_btstupart.syr
AND i5_student.attend_dst = i5_btstupart.dst
AND i5_student.attend_sch = i5_btstupart.sch
AND i5_student.stuid = i5_btstupart.stuid
AND i5_student.is_active = 'Y'
LEFT OUTER JOIN i5_grad_status i5_grad_status
ON i5_student.syr = i5_grad_status.syr
AND i5_student.attend_dst = i5_grad_status.dst
AND i5_student.attend_sch = i5_grad_status.sch
AND i5_student.stuid = i5_grad_status.stuid
GROUP BY i5_btincident.syr
,m.MonthNumber
,m.sortOrder
,m.MonthName
ORDER BY m.sortorder
我的左外连接到i5_btstudent的和i5_student.is_active =&#39; <&#39; 被忽略了......为什么?
工作样本更新:
CREATE TABLE LRB_student (
stuid tinyint,
name varchar(10),
dst varchar(3),
sch varchar(3),
syr varchar(4),
is_active varchar(1)
);
go
CREATE TABLE LRB_btincident (
incid tinyint,
descr varchar(10),
dst varchar(3),
sch varchar(3),
syr varchar(4),
incdate datetime
);
go
CREATE TABLE LRB_btstupart (
incid tinyint,
stuid tinyint,
dst varchar(3),
sch varchar(3),
syr varchar(4),
);
go
CREATE TABLE [dbo].[Months](
[SortOrder] [tinyint] NOT NULL,
[MonthNumber] [nchar](2) NOT NULL,
[MonthName] [nchar](12) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO LRB_student VALUES(1,'student1','001','002','1617','Y');
INSERT INTO LRB_student VALUES(2,'student2','001','002','1617','Y');
INSERT INTO LRB_student VALUES(3,'student3','001','002','1617','N');
INSERT INTO LRB_student VALUES(4,'student4','001','002','1617','Y');
INSERT INTO LRB_student VALUES(5,'student5','001','002','1617','Y');
INSERT INTO LRB_student VALUES(6,'student6','001','002','1617','N');
INSERT INTO LRB_student VALUES(7,'student7','001','002','1617','Y');
go
INSERT INTO LRB_btincident VALUES(10,'Cheating','001','002','1617','2016-09-01 00:00:00.000');
INSERT INTO LRB_btincident VALUES(11,'Assault','001','002','1617','2016-10-01 00:00:00.000');
INSERT INTO LRB_btincident VALUES(12,'Technology','001','002','1617','2016-11-01 00:00:00.000');
INSERT INTO LRB_btincident VALUES(13,'PDA','001','002','1617','2016-12-01 00:00:00.000');
INSERT INTO LRB_btincident VALUES(14,'Tardies','001','002','1617','2017-01-01 00:00:00.000');
INSERT INTO LRB_btincident VALUES(15,'Cheating','001','002','1617','2017-02-01 00:00:00.000');
去
INSERT INTO LRB_btstupart VALUES(10,1,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(11,1,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(12,3,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(12,5,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(13,4,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(14,5,'001','002','1617');
INSERT INTO LRB_btstupart VALUES(15,6,'001','002','1617');
go
INSERT INTO Months VALUES(1,'08','August');
INSERT INTO Months VALUES(2,'09','September');
INSERT INTO Months VALUES(3,'10','October');
INSERT INTO Months VALUES(4,'11','November');
INSERT INTO Months VALUES(5,'12','December');
INSERT INTO Months VALUES(6,'01','January');
INSERT INTO Months VALUES(7,'02','February');
INSERT INTO Months VALUES(8,'03','March');
SELECT
ISNULL(lrb_btincident.syr,'1617') as 'SchoolYear',
m.MonthNumber,
COUNT(lrb_btincident.incid) as 'Incidentct',
m.sortorder,
m.MonthName
FROM Months m
LEFT OUTER JOIN lrb_btincident lrb_btincident ON m.MonthNumber = month (lrb_btincident.incdate) AND lrb_btincident.sch = '002'
AND lrb_btincident.dst = '001'
AND lrb_btincident.syr = '1617'
LEFT OUTER JOIN lrb_btstupart lrb_btstupart ON (lrb_btincident.syr = lrb_btstupart.syr)
AND (lrb_btincident.incID = lrb_btstupart.incID)
AND (lrb_btincident.dst = lrb_btstupart.dst)
AND (lrb_btincident.sch = lrb_btstupart.sch)
LEFT OUTER JOIN lrb_student ON lrb_student.syr = lrb_btstupart.syr
AND lrb_student.dst = lrb_btstupart.dst
AND lrb_student.sch = lrb_btstupart.sch
AND lrb_student.stuid = lrb_btstupart.stuid
AND lrb_student.is_active = 'N' -- this is being ignored
GROUP BY
lrb_btincident.syr,
m.MonthNumber,
m.sortOrder,
m.MonthName
ORDER BY m.sortorder
解决方案(对不起格式化 - 只是想把它拿出来):
WITH MnthInc(Year,SortOrder,Month,Count)
AS
(
select i5_btincident.syr as "SchoolYear",
case month(i5_btincident.incident_date)
when 8 then 1
when 9 then 2
when 10 then 3
when 11 then 4
when 12 then 5
when 1 then 6
when 2 then 7
when 3 then 8
when 4 then 9
when 5 then 10
when 6 then 11
when 7 then 12
else 0
end "MonthSort",
datename(month,i5_btincident.incident_date) as "IncidentMonth",
Count( i5_btincident.incident_id ) as "IncidentCt"
from ( i5_btincident i5_btincident
inner join i5_btstupart i5_btstupart on (i5_btincident.syr = i5_btstupart.syr) and (i5_btincident.incident_ID = i5_btstupart.incident_ID) and (i5_btincident.dst = i5_btstupart.dst) and (i5_btincident.sch = i5_btstupart.sch) )
--inner join evi_User_Table evi_User_Table on (i5_btstupart.stuid = evi_User_Table.stuid) and (i5_btstupart.syr = evi_User_Table.syr)
inner join i5_student on i5_student.syr = i5_btstupart.syr and i5_student.attend_dst = i5_btstupart.dst and i5_student.attend_sch = i5_btstupart.sch and i5_student.stuid = i5_btstupart.stuid
left join i5_grad_status i5_grad_status on i5_student.syr = i5_grad_status.syr and i5_student.attend_dst = i5_grad_status.dst and i5_student.attend_sch = i5_grad_status.sch and i5_student.stuid = i5_grad_status.stuid
where i5_btincident.sch = '310'
and i5_btincident.dst = '002'
and i5_btincident.syr = '1617'
and i5_btincident.positive_or_negative like 'N%'
group by i5_btincident.syr,
case month(i5_btincident.incident_date)
when 8 then 1
when 9 then 2
when 10 then 3
when 11 then 4
when 12 then 5
when 1 then 6
when 2 then 7
when 3 then 8
when 4 then 9
when 5 then 10
when 6 then 11
when 7 then 12
else 0
end,
datename(month,i5_btincident.incident_date)
)
Select Months.SortOrder,Months.MonthName as 'IncidentMonth',ISNULL(MnthInc.Count,0) as 'IncidentCt'
from Months
LEFT OUTER JOIN MnthInc on MnthInc.SortOrder = Months.SortOrder AND MnthInc.Month = Months.MonthName
ORDER BY 1