Solved

Query question

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 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