2017-08-21 38 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」實現此目的? –