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


Looking to sort two queries by Date, Time


Looking to sort two queries by Date, Time

Author
Message
chef423
chef423
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3662 Visits: 443
SELECT
LTRIM(RIGHT(CONVERT(varchar(6), [ie].[Invoice_Number],100),7)) AS [Invoice#]
--, FORMAT([ie].[DateTime], 'MM-dd-yyyy') AS [Void_Date]
, CONVERT(varchar(10),REPLACE(CONVERT(varchar(10), [ie].[DateTime],101),'/','-')) AS [Void_Date]
, LTRIM(RIGHT(CONVERT(varchar(20), [ie].[DateTime],100),7)) AS [Void_Time]
, [ie].[ItemNum] AS [Item_#_Removed]
, [inv].[ItemName]
, '$' + CONVERT(VARCHAR(18), CAST([ie].[Amount] AS MONEY)) AS [Amount]
, CONVERT(VARCHAR(10), CAST([ie].[Quantity] AS INT)) AS [Quantity]
, LTRIM(RIGHT(CONVERT(varchar(20), [ie].[Reason_Code],100),14)) AS [Reason_Code]
FROM
Invoice_Exceptions AS [ie]
JOIN inventory AS [inv]
ON [ie].[ItemNum] = [inv].[ItemNum]
AND [ie].[DateTime] >= @Yesterday AND [ie].[DateTime] < @PeriodEnd
ORDER BY DateTime ASC


AND


SELECT
CONVERT(varchar(10),REPLACE(CONVERT(varchar(10), [E].Exception_DateTime,101),'/','-')) AS [Date],
LTRIM(RIGHT(CONVERT(varchar(20), [E].Exception_DateTime,100),7)) AS [Time],
CONVERT(VARCHAR(10), CAST([E].Cashier_ID AS INT)) AS [Cashier_ID],
LTRIM(RIGHT(CONVERT(varchar(25), [P].shortDescription,100),25)) AS [Description],
LTRIM(RIGHT(CONVERT(varchar(50), [E].Reason_Code,100),50)) AS [Reason]
FROM
Exceptions AS [E]
JOIN Permissions AS [P]
ON [P].PermissionID = [E].Exception_Type and Exception_Type IN (20,21,36)
AND [E].Exception_DateTime >= @Yesterday AND [E].Exception_DateTime < @PeriodEnd
ORDER BY Exception_DateTime ASC


I understand with a UNION the queries need to be similar with the same lines of code. The issue for me it sorting by 2 different Date and Time (DateTime, Exception_DateTime)

Thank you in advance for any assistance
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)SSC Guru (104K reputation)

Group: General Forum Members
Points: 104856 Visits: 18582
I can't work out what your problem is - there are no UNIONs in your code.

John
Steve Jones
Steve Jones
SSC Guru
SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)

Group: Administrators
Points: 437407 Visits: 20561
I assume you mean that you want to UNION these two queries. The columns need to be the same number and data types must be compatible: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql

In terms of sorting, what is the issue?


Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
drew.allen
drew.allen
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48485 Visits: 14720
And there is only one sort on the entire UNION.

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)SSC Guru (128K reputation)

Group: General Forum Members
Points: 128007 Visits: 21218
<<Scratches head>>
There are three queries...

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
chef423
chef423
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3662 Visits: 443
Steve Jones - SSC Editor - Tuesday, January 30, 2018 8:58 AM
I assume you mean that you want to UNION these two queries. The columns need to be the same number and data types must be compatible: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql

In terms of sorting, what is the issue?

Sorting by DateTime and Exception_DateTime

So if the 1st query returns a value on 1-29-2018 @ 8:00AM and then the 2nd query returns a value @ 8:02AM and then back to the 1st and so forth...

chef423
chef423
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3662 Visits: 443
ChrisM@Work - Tuesday, January 30, 2018 9:25 AM
<<Scratches head>>
There are three queries...

Tehe, you are correct, I updated the code. The query I removed is not relevant, my apologies.

Steve Jones
Steve Jones
SSC Guru
SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)SSC Guru (437K reputation)

Group: Administrators
Points: 437407 Visits: 20561
You're not really explaining the issue you are having. I get that you want a sort order among the queries, but what don't you understand or what doesn't work?

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
chef423
chef423
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3662 Visits: 443
Steve Jones - SSC Editor - Tuesday, January 30, 2018 9:40 AM
You're not really explaining the issue you are having. I get that you want a sort order among the queries, but what don't you understand or what doesn't work?

Attempting to ORDER BY DateTime and ORDER BY Exception_DateTime (so both queries sort into one table by Date then Time)

I apologize for not being more clear. I need both of these queries to place the data into one output and sort the data by Date then Time.

.

drew.allen
drew.allen
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48485 Visits: 14720
chef423 - Tuesday, January 30, 2018 9:46 AM
Steve Jones - SSC Editor - Tuesday, January 30, 2018 9:40 AM
You're not really explaining the issue you are having. I get that you want a sort order among the queries, but what don't you understand or what doesn't work?

Attempting to ORDER BY DateTime and ORDER BY Exception_DateTime (so both queries sort into one table by Date then Time)

I apologize for not being more clear. I need both of these queries to place the data into one output and sort the data by Date then Time.

.

This is exactly why we ask for sample data and expected results. It provides very clear starting and end points. There are links in my signature about how to do so.

Also, sorting by date then time is easy. All you have to do is sort on a datetime field.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
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