我的代码是:
,CASE WHEN ResidenceInd = 1 THEN 'Residence' ELSE 'Day' END AS ClassType
,sc.Score
,'1' AS IsLP
,IncludeInGraph
,LessonOrder
FROM (
SELECT
sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
,DHDR.LessonOrder
FROM StdtSessColScore sc
INNER JOIN DSTempSetColCalc dcal
ON dcal.DSTempSetColCalcId = sc.DSTempSetColCalcId
INNER JOIN StdtSessionHdr hdr
ON hdr.StdtSessionHdrId=sc.StdtSessionHdrId
INNER JOIN Class Cls
ON Cls.ClassId=hdr.StdtClassId
INNER JOIN DSTempHdr DHDR
ON DHDR.DSTempHdrId=hdr.DSTempHdrId
WHERE
hdr.IOAInd='N'
AND hdr.SessMissTrailStus ='N'
AND hdr.SessionStatusCd='S'
AND (SELECT StatusId
FROM DSTempHdr DS
WHERE DS.DSTempHdrId = hdr.DSTempHdrId)
<>
(SELECT LookupId
FROM LookUp
WHERE LookupType = 'TemplateStatus'
AND LookupName='Deleted')
GROUP BY
DHDR.LessonOrder
,sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
) AS StdCalcs
,ReportPeriod
WHERE
StdCalcs.StudentId=1240
AND CONVERT(DATE,'2016/01/02') <= CONVERT(DATE,ReportPeriod.PeriodDate)
AND CONVERT(DATE,ReportPeriod.PeriodDate) <= CONVERT(DATE,'2016/12/28')
AND sc.SchoolId = StdCalcs.SchoolId
AND sc.StudentId = StdCalcs.StudentId
AND sc.DSTempSetColCalcId = StdCalcs.DSTempSetColCalcId
AND CONVERT(DATE, Hdr.EndTs) = CONVERT(DATE, ReportPeriod.PeriodDate)
AND Hdr.LessonPlanId = StdCalcs.LessonPlanId
AND dcal.CalcType = StdCalcs.CalcType
AND Hdr.StdtClassId = StdCalcs.StdtClassId
AND Hdr.IOAInd = 'N' AND Hdr.SessMissTrailStus ='N'
AND Hdr.SessionStatusCd = 'S'
AND sc.Score >= 0
AND Hdr.IsMaintanace = 0
GROUP BY
代码继续......
我收到了一个错误:
multipart identifier sc.SchoolId,sc.StudentId,sc.DSTempSetColCalcId,Hdr.EndTs,Hdr.LessonPlanId,dcal.CalcType,Hdr.StdtClassId,Hdr.IOAInd,Hdr.SessMissTrailStus,Hdr.SessionStatusCd,sc.ScoreHdr.IsMaintanace无法受约束。
实际上我们如何解决它...我试图添加sc.score和
AND sc.SchoolId = StdCalcs.SchoolId
AND sc.StudentId = StdCalcs.StudentId
AND sc.DSTempSetColCalcId = StdCalcs.DSTempSetColCalcId
AND CONVERT(DATE, Hdr.EndTs) = CONVERT(DATE, ReportPeriod.PeriodDate)
AND Hdr.LessonPlanId=StdCalcs.LessonPlanId
AND dcal.CalcType = StdCalcs.CalcType
AND Hdr.StdtClassId = StdCalcs.StdtClassId
AND Hdr.IOAInd = 'N' AND Hdr.SessMissTrailStus = 'N'
AND Hdr.SessionStatusCd = 'S'
AND sc.Score >= 0
AND Hdr.IsMaintanace=0
感谢任何帮助
答案 0 :(得分:0)
检查以下代码中的评论。如果从子查询中获取数据作为表,则无法使用子查询的内部表别名,但必须使用您将其作为表格提供的别名。
CASE WHEN ResidenceInd=1 THEN 'Residence' ELSE 'Day' END AS ClassType
,StdCalcs.Score --here was the wrong table-alias
,'1' AS IsLP
,IncludeInGraph
,LessonOrder
FROM (
SELECT
sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
,DHDR.LessonOrder
FROM StdtSessColScore sc
INNER JOIN DSTempSetColCalc dcal
ON dcal.DSTempSetColCalcId = sc.DSTempSetColCalcId
INNER JOIN StdtSessionHdr hdr
ON hdr.StdtSessionHdrId=sc.StdtSessionHdrId
INNER JOIN Class Cls ON Cls.ClassId=hdr.StdtClassId
INNER JOIN DSTempHdr DHDR ON DHDR.DSTempHdrId=hdr.DSTempHdrId
WHERE hdr.IOAInd='N' AND hdr.SessMissTrailStus ='N' AND hdr.SessionStatusCd='S' AND (SELECT StatusId FROM DSTempHdr
DS WHERE DS.DSTempHdrId=hdr.DSTempHdrId)<>(SELECT LookupId FROM LookUp WHERE LookupType='TemplateStatus' AND LookupName='Deleted')
GROUP BY
DHDR.LessonOrder
,sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
) AS StdCalcs
,ReportPeriod
WHERE StdCalcs.StudentId=1240 AND CONVERT(DATE,'2016/01/02') <= CONVERT(DATE,ReportPeriod.PeriodDate) AND
CONVERT(DATE,ReportPeriod.PeriodDate) <= CONVERT(DATE,'2016/12/28') and sc.SchoolId=StdCalcs.SchoolId AND sc.StudentId=StdCalcs.StudentId
AND sc.DSTempSetColCalcId=StdCalcs.DSTempSetColCalcId AND CONVERT(DATE,Hdr.EndTs)=CONVERT(DATE,ReportPeriod.PeriodDate)
AND Hdr.LessonPlanId=StdCalcs.LessonPlanId AND dcal.CalcType=StdCalcs.CalcType AND Hdr.StdtClassId=StdCalcs.StdtClassId
AND Hdr.IOAInd='N' AND Hdr.SessMissTrailStus ='N' AND Hdr.SessionStatusCd='S' AND sc.Score>=0 AND Hdr.IsMaintanace=0
GROUP BY
答案 1 :(得分:0)
这不是答案!它只是建议您如何构建查询以更好地理解它。我建议你花更多的时间来如何美化你的代码。您将来在调试上花费的时间更少,并了解您的代码正在做什么。这是你的代码:
SELECT CASE
WHEN ResidenceInd = 1 THEN 'Residence'
ELSE 'Day'
END AS ClassType
,StdCalcs.Score --here was the wrong table-alias
,'1' AS IsLP
,IncludeInGraph
,LessonOrder
FROM
(
SELECT
sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
,DHDR.LessonOrder
FROM StdtSessColScore AS sc INNER JOIN
DSTempSetColCalc AS dcal ON dcal.DSTempSetColCalcId = sc.DSTempSetColCalcId INNER JOIN
StdtSessionHdr AS hdr ON hdr.StdtSessionHdrId = sc.StdtSessionHdrId INNER JOIN
Class AS Cls ON Cls.ClassId = hdr.StdtClassId INNER JOIN
DSTempHdr AS DHDR ON DHDR.DSTempHdrId = hdr.DSTempHdrId
WHERE hdr.IOAInd = 'N' AND
hdr.SessMissTrailStus = 'N' AND
hdr.SessionStatusCd = 'S' AND
(
SELECT StatusId
FROM DSTempHdr DS
WHERE DS.DSTempHdrId = hdr.DSTempHdrId
) <>
(
SELECT LookupId
FROM LookUp
WHERE LookupType = 'TemplateStatus' AND
LookupName = 'Deleted'
)
GROUP BY DHDR.LessonOrder
,sc.SchoolId
,sc.StudentId
,sc.DSTempSetColCalcId
,dcal.CalcType
,hdr.LessonPlanId
,hdr.StdtClassId
,Cls.ResidenceInd
,dcal.IncludeInGraph
) AS StdCalcs,
ReportPeriod
WHERE StdCalcs.StudentId = 1240 AND
CONVERT(DATE, '2016/01/02') <= CONVERT(DATE, ReportPeriod.PeriodDate) AND
CONVERT(DATE, ReportPeriod.PeriodDate) <= CONVERT(DATE, '2016/12/28') AND
sc.SchoolId = StdCalcs.SchoolId AND
sc.StudentId = StdCalcs.StudentId AND
sc.DSTempSetColCalcId=StdCalcs.DSTempSetColCalcId AND
CONVERT(DATE, Hdr.EndTs) = CONVERT(DATE, ReportPeriod.PeriodDate) AND
Hdr.LessonPlanId=StdCalcs.LessonPlanId AND
dcal.CalcType = StdCalcs.CalcType AND
Hdr.StdtClassId = StdCalcs.StdtClassId AND
Hdr.IOAInd = 'N' AND
Hdr.SessMissTrailStus = 'N' AND
Hdr.SessionStatusCd = 'S' AND
sc.Score >= 0 AND
Hdr.IsMaintanace = 0
GROUP BY
在 select,group by,order by 子句中:尝试将每列放在其自己的行上。
在表单子句中:将每个表和连接上的连接放在一行上。
在 where 子句中:将每个条件放在一行上。将AND或OR置于开头或结尾。
无处不在:在等号前后写一个空格。您可以更轻松地看到左侧和右侧成员。