2017-08-21 26 views
0

我需要按UniqueIdentifier列进行分组,然后按组排列DateTime列,该表还包含XML列。如何分组按顺序排列 - SQL Server

表架构:StudentMark

CREATE TABLE [dbo].[StudentMark] 
(
    [StudentMarkId] [int] IDENTITY(1,1) NOT NULL, 
    [StudentId] [uniqueidentifier] NULL, 
    [SubjectId] [uniqueidentifier] NULL, 
    [ScoreInfo] [xml] NULL, 
    [GeneratedOn] [datetime2](2) NOT NULL, 

    CONSTRAINT [PK_StudentMark] 
     PRIMARY KEY CLUSTERED ([StudentMarkId] ASC) 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

样品种子数据

INSERT INTO [dbo].[StudentMark] ([StudentId], [SubjectId], [ScoreInfo], GeneratedOn]) 
VALUES ('FC3CB475-B480-4129-9190-6DE880E2D581', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15'), 
     ('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15'), 
     ('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20'), 
     ('FC3CB475-B480-4129-9190-6DE880E2D581', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20'); 

要求:我需要组由[dbo].[StudentMark].[StudentId]和一个组内的排序的列[dbo].[StudentMark].[GeneratedOn]

我尝试以下SQL查询,但它造成的错误

SELECT 
    MAX([StudentMarkId]), [StudentId], [SubjectId], [ScoreInfo], [GeneratedOn] 
FROM 
    [dbo].[StudentMark] 
GROUP BY 
    [StudentId] 
ORDER BY 
    [GeneratedOn] DESC 

错误

列“dbo.StudentMark.SubjectId”是因为它是在选择列表中无效不包含在聚合函数或GROUP BY子句中。

预期的结果集

3, '0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20' 

2, '0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15' 

4, 'FC3CB475-B480-4129-9190-6DE880E2D581', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20' 

1, 'FC3CB475-B480-4129-9190-6DE880E2D581', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15' 

我审阅了以下问题,但我不能修复:SQL Group with Order by

对于我使用SQL Server 2016您的实物资料。

请帮助我。

+0

您通常GROUP BY不在参数一组函数选定列。在这种情况下[StudentId],[SubjectId],[ScoreInfo],[GeneratedOn]。 – jarlh

+0

@jarlh - 出现以下错误:XML数据类型无法进行比较或排序,除非使用IS NULL运算符。# –

+1

编辑您的问题并提供样本数据和样本结果。 “GROUP BY”查询每组产生一行。 “组内”没有排序。 –

回答

1
SELECT MAX([StudentMarkId]), 
    [StudentId], 
    [SubjectId], 
    convert(varchar(max),[ScoreInfo]) as [ScoreInfo] , [GeneratedOn] 
FROM [dbo].[StudentMark] 
GROUP BY [StudentId], [SubjectId], convert(varchar(max), [ScoreInfo]), [GeneratedOn] 
ORDER BY [GeneratedOn] DESC 

入住这也

SELECT MAX([StudentMarkId]) 
     over (partition by [StudentId] order by [GeneratedOn] desc) as maxStudentMarkId, 
    [StudentId], 
    [SubjectId], 
    convert(varchar(max),[ScoreInfo]) as [ScoreInfo] , [GeneratedOn] 
FROM [dbo].[StudentMark] 

输出 -

maxStudentMarkId StudentId SubjectId ScoreInfo GeneratedOn 
4 FC3CB475-B480-4129-9190-6DE880E2D581 AB172272-D2E9-49E1-8040-6117BB6743DB <StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 2017-08-16 09:06:20.00 
4 FC3CB475-B480-4129-9190-6DE880E2D581 0D72F79E-FB48-4D3E-9906-B78A9D105081 <StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 2017-08-10 10:10:15.00 
3 0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2 AB172272-D2E9-49E1-8040-6117BB6743DB <StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 2017-08-16 09:06:20.00 
3 0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2 0D72F79E-FB48-4D3E-9906-B78A9D105081 <StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 2017-08-10 10:10:15.00 
+0

请检查您的查询的结果记录,其返回记录集的顺序为'4,3,2,1'或'[StudentMarkId]' –

+0

,这是归因于ORDER BY [GeneratedOn] DESC。你能分享预期的产出吗? – Anagha

+0

第二个查询正在工作,我的问题是我们无法通过使用“Group By”实现此目的? –