Solved

Query question

Posted on 2011-02-15
3
1,371 Views
Last Modified: 2012-05-11
I have a query that pulls some column values. One of the columns is a date called "Upload Date".

ex. Select this, that, theother, Uploaddate
      From thistable
      Where status=1

Now I want to basically add 1 more column called "Completed Date" to my resultset. For this,I would need to tweak the Where clause.

  ex. Select this, that, theother,CompletedDate      
         From thistable
        Where status=3

Then I want to Use DateDiff() function to get the days difference between "Upload Date" and "CompletedDate"  and call that value "Turnaround" .

So essentially if we left the where clause out what I want is this :
  ex. Select this, that, theother,UploadDate ,CompletedDate,Turnaround"    
         From thistable


How to do it? Any advice would be greatly appreciated.


     
0
Comment
Question by:jazzcatone
3 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34899483
What would you want to see for Turnaround when the status value is not 3 (complete)?

The general query would look like:
Select this, that, theother,UploadDate ,CompletedDate, DATEDIFF(day, UploadDate, CompletedDate) AS Turnaround From thistable
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 34899675
I assume that you have UploadDate and CompletedDate in two different rows for the same combination of this,that and theother.
You can try like this.
select this,that,theother,DATEDIFF(DAY,UploadDate,CompletedDate)
  from (
select this,that,theother,
       max(Case Status when 1 then UploadDate end) UploadDate,
       max(Case Status when 3 then CompletedDate end) CompletedDate
  from thistable
 group by this,that,theother) t1

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 34899728
You need a field that identifies the record when it is uploaded and when it is completed

then the query could be
Select this, that, theother, Uploaddate, B.CompletedDate, DATEDIFF(day, A.UploadDate, B.CompletedDate) AS Turnaround
From thistable A
inner join (select SomeID, CompletedDate
            From thistable
            Where status=3) B on B.SomeID = A.SomeID
where A.status=1

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now