Solved

Query question

Posted on 2011-02-15
3
1,372 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

947 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

19 Experts available now in Live!

Get 1:1 Help Now