SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORDER BY Query Question


ORDER BY Query Question

Author
Message
kylemkr85
kylemkr85
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 31
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_email
WHERE
wcv.workitem_status_lookup_value = 'Open' and wcv.workitem_created_on > '2006-01-01 12:00:00.000'
ORDER BY
[Ticket Age] DESC, [Assigned To] ASC
kylemkr85
kylemkr85
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 31
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.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97635 Visits: 38988
Give this a shot:



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_email
WHERE
wcv.workitem_status_lookup_value = 'Open' and
wcv.workitem_created_on > '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




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
kylemkr85
kylemkr85
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 31
Thanks for the quick response, however, I am receiving the following error below:

Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97635 Visits: 38988
kylemkr85 (2/8/2013)
Thanks for the quick response, however, I am receiving the following error below:

Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.


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:



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_email
WHERE
wcv.workitem_status_lookup_value = 'Open' and
wcv.workitem_created_on > '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
BaseData
ORDER BY
CASE WHEN [Assigned To] = 'Group 1' THEN 0 ELSE 1 END ASC,
[Ticket Age] DESC




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search