How to Group By and Order By - SQL Server -
i need group uniqueidentifier column , sort group datetime column, table contains xml column.
table schema: 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] sample seed data
insert [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'); requirement: need group [dbo].[studentmark].[studentid] , sort column [dbo].[studentmark].[generatedon] within group.
i tried following sql query causing error
select max([studentmarkid]), [studentid], [subjectid], [scoreinfo], [generatedon] [dbo].[studentmark] group [studentid] order [generatedon] desc error:
column 'dbo.studentmark.subjectid' invalid in select list because not contained in either aggregate function or group clause.
expected result set:
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' i refereed following question can't fix it: sql group order by
for kind information i'm using sql server 2016.
kindly assist me.
select max([studentmarkid]), [studentid], [subjectid], convert(varchar(max),[scoreinfo]) [scoreinfo] , [generatedon] [dbo].[studentmark] group [studentid], [subjectid], convert(varchar(max), [scoreinfo]), [generatedon] order [generatedon] desc check also
select max([studentmarkid]) on (partition [studentid] order [generatedon] desc) maxstudentmarkid, [studentid], [subjectid], convert(varchar(max),[scoreinfo]) [scoreinfo] , [generatedon] [dbo].[studentmark] output -
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
Comments
Post a Comment