【问题标题】:How to Group By and Order By - SQL Server如何分组和排序 - SQL Server
【发布时间】: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


【解决方案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

【讨论】:

  • 请检查您查询的结果记录,它按4, 3 , 2, 1[StudentMarkId] 的顺序返回记录集
  • 这是由于 ORDER BY [GeneratedOn] DESC。你能分享预期的输出吗?
  • 第二个查询有效,我的问题是我们无法通过使用Group By 来实现这一点?
  • 您只想在一列上使用聚合函数,因此分区方式会根据您的要求提供更好的解决方案。有关更多信息,请查看 - stackoverflow.com/questions/2404565/…
猜你喜欢
  • 1970-01-01
  • 2020-04-16
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
相关资源
最近更新 更多