如何将多行合并为一行
How do I consolidate multiple rows into one
全部,
这是我的问题。它给出了一个结果集,但我已经跨多行得到了它,我想将它放在一行中并删除阅读、写作、听力、口语和总体列中的空值。
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
我需要做什么才能修改查询以完成此操作?
谢谢
您的 GROUP BY
中有 S.TestDate
和 S.IsSchoolLevelScoreEdit
。所以它可能会为您提供每个测试日期的记录。由于您的输出列称为 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
全部,
这是我的问题。它给出了一个结果集,但我已经跨多行得到了它,我想将它放在一行中并删除阅读、写作、听力、口语和总体列中的空值。
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
我需要做什么才能修改查询以完成此操作?
谢谢
您的 GROUP BY
中有 S.TestDate
和 S.IsSchoolLevelScoreEdit
。所以它可能会为您提供每个测试日期的记录。由于您的输出列称为 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