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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

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
Vitor MontalvãoMSSQL 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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ãoMSSQL 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
awking00Information Technology SpecialistCommented:
What does some sample of the original data look like and what should the query produce from that data?
0
awking00Information Technology SpecialistCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.