﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / ORDER BY Query Question / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 15:34:23 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: ORDER BY Query Question</title><link>http://www.sqlservercentral.com/Forums/Topic1417938-145-1.aspx</link><description>[quote][b]kylemkr85 (2/8/2013)[/b][hr]Thanks for the quick response, however, I am receiving the following error below:Msg 145, Level 15, State 1, Line 1ORDER BY items must appear in the select list if SELECT DISTINCT is specified.[/quote]Oh well, you get what you get when you don't provide DDL, sample data, and expected results.  No tested code.For better answers to your questions, please read and follow the instructions in the first article you will find referenced in my signature block below.Now, how about this:[code="sql"]WITH BaseData AS (SELECT DISTINCT    wcv.contact_name    ,wcv.contact_organization_name    ,wcv.workitem_title    ,wcv.workitem_number    ,wcv.workitem_created_on    ,wcv.assigned_to_worker_name [Assigned To]    ,datediff(d, workitem_created_on, getdate()) [Ticket Age]FROM     contact_view cv    JOIN workitem_current_view wcv        ON cv.contact_email = wcv.contact_emailWHERE     wcv.workitem_status_lookup_value = 'Open' and    wcv.workitem_created_on &amp;gt; '2006-01-01 12:00:00.000')SELECT    contact_name,    contact_organization_name,    workitem_title,    workitem_number,    workitem_created_on,    [Assigned To],    [Ticket Age]FROM    BaseDataORDER BY    CASE WHEN [Assigned To] = 'Group 1' THEN 0 ELSE 1 END ASC,    [Ticket Age] DESC[/code]</description><pubDate>Fri, 08 Feb 2013 18:56:13 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: ORDER BY Query Question</title><link>http://www.sqlservercentral.com/Forums/Topic1417938-145-1.aspx</link><description>Thanks for the quick response, however, I am receiving the following error below:Msg 145, Level 15, State 1, Line 1ORDER BY items must appear in the select list if SELECT DISTINCT is specified.</description><pubDate>Fri, 08 Feb 2013 16:16:37 GMT</pubDate><dc:creator>kylemkr85</dc:creator></item><item><title>RE: ORDER BY Query Question</title><link>http://www.sqlservercentral.com/Forums/Topic1417938-145-1.aspx</link><description>Give this a shot:[code="sql"]SELECT DISTINCT    wcv.contact_name    ,wcv.contact_organization_name    ,wcv.workitem_title    ,wcv.workitem_number    ,wcv.workitem_created_on    ,wcv.assigned_to_worker_name [Assigned To]    ,datediff(d, workitem_created_on, getdate()) [Ticket Age]FROM     contact_view cv    JOIN workitem_current_view wcv        ON cv.contact_email = wcv.contact_emailWHERE     wcv.workitem_status_lookup_value = 'Open' and    wcv.workitem_created_on &amp;gt; '2006-01-01 12:00:00.000'ORDER BY    CASE WHEN wcv.assigned_to_worker_name = 'Group 1' THEN 0 ELSE 1 END ASC,    [Ticket Age] DESC    --[Assigned To] ASC [/code]</description><pubDate>Fri, 08 Feb 2013 16:12:52 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: ORDER BY Query Question</title><link>http://www.sqlservercentral.com/Forums/Topic1417938-145-1.aspx</link><description>To clarify a little, I want the results to be displayed by ticket age, however, I want a certain AssignedTo group to display first before the rest. I don't want all the AssignedTo groups grouped together however.</description><pubDate>Fri, 08 Feb 2013 15:57:34 GMT</pubDate><dc:creator>kylemkr85</dc:creator></item><item><title>ORDER BY Query Question</title><link>http://www.sqlservercentral.com/Forums/Topic1417938-145-1.aspx</link><description>I have the following query below, but I want to ORDER BY a certain value in the Assigned To field first before it orders by ticket age. So lets say I have a AssignedTo value of 'Group1'. I want Group1 to be displayed first, then all remaining values to be displayed after by ticket age and group if that makes any sense.Current Query:SELECT DISTINCT		wcv.contact_name		,wcv.contact_organization_name		,wcv.workitem_title		,wcv.workitem_number		,wcv.workitem_created_on		,wcv.assigned_to_worker_name [Assigned To]		,datediff(d, workitem_created_on, getdate()) [Ticket Age]FROM 		contact_view cv		JOIN 		workitem_current_view wcv		ON 		cv.contact_email = wcv.contact_emailWHERE 		wcv.workitem_status_lookup_value = 'Open' and wcv.workitem_created_on &amp;gt; '2006-01-01 12:00:00.000'ORDER BY 		[Ticket Age] DESC, [Assigned To] ASC</description><pubDate>Fri, 08 Feb 2013 15:55:09 GMT</pubDate><dc:creator>kylemkr85</dc:creator></item></channel></rss>