所有
这是我的问题。它提供了一个结果集,但是我已经将它放在多行中,我想将它放在一行中,并在阅读,写作,听力,口语和整体列中删除Null。 / p>
SQL:
Drop Table #STudentIDs
Declare @SchoolID NVARCHAR(max) = '2152'
Declare @StudentID NVARCHAR(max) = '1451753'
Declare @TeacherID varchar(max) = '640470'
CREATE TABLE #STudentIDs
(
STudentID INT primary key
);
INSERT INTO #StudentIDs
SELECT Value
FROM dbo.Fnsplitid(@StudentID, ',');
DECLARE @SchoolIDs TABLE
(
SchoolID INT
);
INSERT INTO @SchoolIDs
SELECT Value
FROM dbo.Fnsplitid(@SchoolID, ',');
DECLARE @RecentCELDTData TABLE
(StudentID INT,
Reading NVarchar(20),
Writing NVarchar(20),
Listening NVarchar(20),
Speaking NVarchar(20),
OverallScore NVarchar(20),
RecentTestDate DATE,
Overall BIT)
INSERT INTO @RecentCELDTData
SELECT S.StudentID,
Max(CASE WHEN S.TestComponentCode='L0100' THEN S.TestPerformanceLevelCode ELSE '' END) AS Reading,
Max(CASE WHEN S.TestComponentCode='L0700' THEN S.TestPerformanceLevelCode ELSE '' END) AS Writing,
Max(CASE WHEN S.TestComponentCode='L0201' THEN S.TestPerformanceLevelCode ELSE '' END) AS Listening,
Max(CASE WHEN S.TestComponentCode='L0202' THEN S.TestPerformanceLevelCode ELSE '' END) AS Speaking,
Max(CASE WHEN S.TestComponentCode='L0800' THEN S.TestPerformanceLevelCode ELSE '' END) AS Overall,
S.TestDate,
S.IsSchoolLevelScoreEdit
FROM (SELECT DISTINCT TD.IsSchoolLevelScoreEdit,STUTCS.StudentID,STUTCS.TestDate,TPLT.TestPerformanceLevelCode,TC.TestComponentCode,
DENSE_RANK() OVER (PARTITION BY STUTCS.StudentID ORDER BY stutcs.studentid,STUTCS.TestDate DESC) AS RNK
FROM #StudentIDs AS STUS
INNER JOIN StudentTestComponentScore(NOLOCK) AS STUTCS
ON STUS.StudentID=STUTCS.StudentID
INNER JOIN TestPurposeType(NOLOCK) AS TPT
ON STUTCS.TestPurposeTypeID=TPT.TestPurposeTypeID
-- AND TPT.TestPurposeName='Annual'
INNER JOIN TestComponentPerformanceMapping(NOLOCK) AS TCPM
ON STUTCS.TestComponentPerformanceMappingID=TCPM.TestComponentPerformanceMappingID
INNER JOIN TestComponent(NOLOCK) AS TC
ON TCPM.TestComponentID=TC.TestComponentID
AND TC.TestComponentCode IN ('L0100','L0700','L0201','L0202','L0800')
INNER JOIN TestScoreType(NOLOCK) AS TST
ON TCPM.TestScoreTypeID=TST.TestScoreTypeID
AND TST.TestScoreTypeCode='SCAL'
INNER JOIN dbo.TestPerformanceLevelType(NOLOCK) TPLT
ON TCPM.TestPerformanceLevelTypeID = TPLT.TestPerformanceLevelTypeID
INNER JOIN TestDefinition(NOLOCK) AS TD
ON TC.TestDefinitionID=TD.TestDefinitionID
AND TD.TestDefinitionCode='CELDT'
) AS S WHERE S.RNK=1 GROUP BY S.StudentID,S.TestDate,S.IsSchoolLevelScoreEdit,S.TestComponentCode,S.TestPerformanceLevelCode
Select * From @RecentCELDTData
以下是给我的结果:
StudentID Reading Writing Listening Speaking OverallScore RecentTestDate Overall
1451753 3-I 2014-09-05 0
1451753 4-EA 2014-09-05 0
1451753 4-EA 2014-09-05 0
1451753 5-ADV 2014-09-05 0
1451753 5-ADV 2014-09-05 0
这就是我需要的样子:
StudentID Reading Writing Listening Speaking OverallScore RecentTestDate Overall
1451753 3-I 5-ADV 5-ADV 4-EA 4-EA 9/5/2014 0
修改查询以完成此操作需要做什么?
由于
答案 0 :(得分:1)
S.TestDate
中有S.IsSchoolLevelScoreEdit
和GROUP BY
。因此,它可能会为您提供每个考试日期的记录。由于您的输出列名为RecentTestDate
,因此您可以在该列上执行MAX
。我不确定IsSchoolLevelScoreEdit
是什么意思......但是如果你想标记它,那么如果它们中的任何一个是一个,你也可以做一个最大值。问题是你不能在位列上执行max函数...所以你必须转换它。你的最终插入语句可能是这样的吗?
INSERT INTO @RecentCELDTData
SELECT S.StudentID,
Max(CASE WHEN S.TestComponentCode='L0100' THEN S.TestPerformanceLevelCode ELSE '' END) AS Reading,
Max(CASE WHEN S.TestComponentCode='L0700' THEN S.TestPerformanceLevelCode ELSE '' END) AS Writing,
Max(CASE WHEN S.TestComponentCode='L0201' THEN S.TestPerformanceLevelCode ELSE '' END) AS Listening,
Max(CASE WHEN S.TestComponentCode='L0202' THEN S.TestPerformanceLevelCode ELSE '' END) AS Speaking,
Max(CASE WHEN S.TestComponentCode='L0800' THEN S.TestPerformanceLevelCode ELSE '' END) AS Overall,
max(S.TestDate) as RecentTestDate,
convert(bit,max(convert(tinyint,S.IsSchoolLevelScoreEdit))) as Overall
FROM
(SELECT DISTINCT TD.IsSchoolLevelScoreEdit,STUTCS.StudentID,STUTCS.TestDate,TPLT.TestPerformanceLevelCode,TC.TestComponentCode, DENSE_RANK() OVER (PARTITION BY STUTCS.StudentID ORDER BY stutcs.studentid,STUTCS.TestDate DESC) AS RNK
FROM #StudentIDs AS STUS
INNER JOIN StudentTestComponentScore(NOLOCK) AS STUTCS
ON STUS.StudentID=STUTCS.StudentID
INNER JOIN TestPurposeType(NOLOCK) AS TPT
ON STUTCS.TestPurposeTypeID=TPT.TestPurposeTypeID
-- AND TPT.TestPurposeName='Annual'
INNER JOIN TestComponentPerformanceMapping(NOLOCK) AS TCPM
ON STUTCS.TestComponentPerformanceMappingID=TCPM.TestComponentPerformanceMappingID
INNER JOIN TestComponent(NOLOCK) AS TC
ON TCPM.TestComponentID=TC.TestComponentID
AND TC.TestComponentCode IN ('L0100','L0700','L0201','L0202','L0800')
INNER JOIN TestScoreType(NOLOCK) AS TST
ON TCPM.TestScoreTypeID=TST.TestScoreTypeID
AND TST.TestScoreTypeCode='SCAL'
INNER JOIN dbo.TestPerformanceLevelType(NOLOCK) TPLT
ON TCPM.TestPerformanceLevelTypeID = TPLT.TestPerformanceLevelTypeID
INNER JOIN TestDefinition(NOLOCK) AS TD
ON TC.TestDefinitionID=TD.TestDefinitionID
AND TD.TestDefinitionCode='CELDT'
) AS S WHERE S.RNK=1
GROUP BY S.StudentID