Skip to main content

Timeline for SQL Preserve the Order in group by

Current License: CC BY-SA 4.0

17 events
when toggle format what by license comment
May 9, 2019 at 18:38 comment added Jon of All Trades Data in SQL Server has no guaranteed order. Whatever order it happened to have been returned in your first query, it could be different the next time you run that exact query. In practice, data will often come back in the order of the clustering index - but that's contingent, not guaranteed; the only guarantee is ORDER BY.
May 7, 2019 at 13:00 review Close votes
May 10, 2019 at 15:05
S May 7, 2019 at 12:18 history suggested Learning_DBAdmin CC BY-SA 4.0
Added currently used query to get the result.
May 7, 2019 at 9:30 vote accept kkakadiya
May 7, 2019 at 9:18 review Suggested edits
S May 7, 2019 at 12:18
S May 7, 2019 at 9:14 history suggested HandyD CC BY-SA 4.0
Added DDL for table from comments
May 7, 2019 at 8:22 comment added kkakadiya Actually, I don't want output by in any of column order. I want output as it is in my DB table or screenshot given. ya, looking at the screenshot, data is not ordered.
May 7, 2019 at 7:53 answer added Randi Vertongen timeline score: 2
May 7, 2019 at 7:43 review Suggested edits
S May 7, 2019 at 9:14
May 7, 2019 at 7:41 comment added HandyD What is the actual order you want? Looking at the screenshots, data is not ordered numerically by fix code, or alphabetically by fixdescription, in your expected output. What column do you want to order the data by?
May 7, 2019 at 7:29 comment added kkakadiya group by is used to get distinct records. DDL route_fixcodes SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[route_fixcodes]( [id] [int] IDENTITY(1,1) NOT NULL, [route_id] [int] NOT NULL, [fixcode] [varchar](4) NOT NULL, [fixdescription] [varchar](32) NOT NULL, CONSTRAINT [PK__route_fi__3213E83FD7609D27] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
May 7, 2019 at 7:06 answer added Zee786 timeline score: 0
May 7, 2019 at 6:58 comment added Learning_DBAdmin What is the use of group by here when there is no any aggregate function? can you also please include DDL of route_fixcodes table.
May 7, 2019 at 6:53 comment added kkakadiya select route_id,fixcode,fixdescription from route_fixcodes group by route_id,fixcode,fixdescription having route_id = 12345 Order by fixcode
May 7, 2019 at 6:52 comment added Learning_DBAdmin Can you please post the query that you are using for group by.
May 7, 2019 at 6:50 review First posts
May 7, 2019 at 12:44
May 7, 2019 at 6:47 history asked kkakadiya CC BY-SA 4.0