orderby further filtering

end_date minust start_date column gives duration.

if durationn for multiple projects same then i have to order by start date rather than just oder by duration. how to achieve this

select start_date, end_date,(end_date-start_date) as duration order by duration ( how to add here order by start_date?? if duration for say project1 and project 2 and project 3 out of 100 projects is same say 20 days)


please advise
LVL 7
gudii9Asked:
Who is Participating?
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
Operand data type date is invalid for subtract operator.
Sorry, I forgot that you're working with dates. Use the DATEDIFF function:
SELECT START_DATE, END_DATE, datediff(day, START_DATE, END_DATE) AS DURATION 
FROM PROJECTS 
order by datediff(day, START_DATE, END_DATE), START_DATE

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Just add the start_date in the ORDER BY statement. It will order first by duration and next by start_date:
select start_date, end_date,(end_date-start_date) as duration 
from tablename
order by duration, start_date

Open in new window

0
 
gudii9Author Commented:
let me try
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
gudii9Author Commented:
SELECT START_DATE, END_DATE, from projects AS DURATION) from PROJECTS orderby (END_DATE-START_DATE) AS DURATION order by DURATION

says incorrect syntax at from
0
 
gudii9Author Commented:
i am trying using ms sql server

SELECT START_DATE, END_DATE, from projects AS DURATION FROM PROJECTS orderby (END_DATE-START_DATE) AS DURATION, START_DATE

aboe also says error at from?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I don't know how did you end with 2 FROMs but it only one that is necessary:
SELECT START_DATE, END_DATE, (END_DATE-START_DATE) AS DURATION 
FROM PROJECTS 
order by (END_DATE-START_DATE), START_DATE

Open in new window

0
 
gudii9Author Commented:
from and FROM are different?
similarly orderby, order by,ORDERBY, ORDER BY different?
0
 
gudii9Author Commented:
getting below error
Msg 8117, Level 16, State 1, Server WIN-ILO9GLLB9J0, Line 2
Operand data type date is invalid for subtract operator.
https://www.hackerrank.com/challenges/projects/problem
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
from and FROM are different?
No but you just need a FROM clause. You used two and that's why the error.

similarly orderby, order by,ORDERBY, ORDER BY different?
There's no difference if you use lower or upper case but orderby doesn't exist. It's "order by" (with a space).
0
 
gudii9Author Commented:
SELECT START_DATE, END_DATE, datediff(day, START_DATE, END_DATE) AS DURATION 
FROM PROJECTS 
order by datediff(day, START_DATE, END_DATE), START_DATE

Open in new window


above says wrong answer
Your code did not pass this test case.

Your Output (stdout)

2015-10-01 2015-10-02 1
2015-10-02 2015-10-03 1
2015-10-03 2015-10-04 1
2015-10-04 2015-10-05 1
2015-10-11 2015-10-12 1
2015-10-12 2015-10-13 1
2015-10-15 2015-10-16 1
2015-10-17 2015-10-18 1
2015-10-19 2015-10-20 1
2015-10-21 2015-10-22 1
2015-10-25 2015-10-26 1
2015-10-26 2015-10-27 1
2015-10-27 2015-10-28 1
2015-10-28 2015-10-29 1
2015-10-29 2015-10-30 1
2015-10-30 2015-10-31 1
2015-11-01 2015-11-02 1
2015-11-04 2015-11-05 1
2015-11-05 2015-11-06 1
2015-11-06 2015-11-07 1
2015-11-07 2015-11-08 1
2015-11-11 2015-11-12 1
2015-11-12 2015-11-13 1
2015-11-17 2015-11-18 1
Compiler Message

Wrong Answer
0
 
gudii9Author Commented:
SELECT START_DATE, END_DATE AS DURATION 
FROM PROJECTS 
order by datediff(day, START_DATE, END_DATE), START_DATE

Open in new window


i removed one column now it say

our code did not pass this test case.

Your Output (stdout)

2015-10-01 2015-10-02
2015-10-02 2015-10-03
2015-10-03 2015-10-04
2015-10-04 2015-10-05
2015-10-11 2015-10-12
2015-10-12 2015-10-13
2015-10-15 2015-10-16
2015-10-17 2015-10-18
2015-10-19 2015-10-20
2015-10-21 2015-10-22
2015-10-25 2015-10-26
2015-10-26 2015-10-27
2015-10-27 2015-10-28
2015-10-28 2015-10-29
2015-10-29 2015-10-30
2015-10-30 2015-10-31
2015-11-01 2015-11-02
2015-11-04 2015-11-05
2015-11-05 2015-11-06
2015-11-06 2015-11-07
2015-11-07 2015-11-08
2015-11-11 2015-11-12
2015-11-12 2015-11-13
2015-11-17 2015-11-18
Compiler Message

Wrong Answer
0
 
gudii9Author Commented:
SELECT START_DATE, END_DATE AS DURATION 
FROM PROJECTS 
order by datediff(day, START_DATE, END_DATE) ASC, START_DATE ASC

Open in new window

above also did not work
please advise
0
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
I don't have access to your data and neither to the original question. By what you have asked here, I gave the adequate answer.
0
 
awking00Commented:
What does some sample of the original data look like and what should the query produce from that data?
0
 
awking00Commented:
Can you please post the test case and the expected results?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Solution provided based on the original question.
@Author, please open a new question when the requirements change.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.