【发布时间】:2018-01-29 10:11:12
【问题描述】:
我需要按UniqueIdentifier 列分组,然后按DateTime 列对组进行排序,该表还包含XML 列。
表格架构: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。
请帮助我。
【问题讨论】:
-
您通常按选定的列进行分组,这些列不是集合函数的参数。在这种情况下,[StudentId]、[SubjectId]、[ScoreInfo]、[GeneratedOn]。
-
@jarlh - 我收到以下错误
The XML data type cannot be compared or sorted, except when using the IS NULL operator. -
编辑您的问题并提供示例数据和示例结果。
GROUP BY查询每组生成一行。组内没有排序。 -
@GordonLinoff - 我已经给出了示例种子数据,请再等 2 分钟,我将更新预期结果。
标签: sql sql-server group-by sql-order-by sql-server-2016