Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:54:28+00:00 2026-05-27T14:54:28+00:00

I have a sample table (table_name: track_task) like below: task_id stage log_date —————————————- 3

  • 0

I have a sample table (table_name: track_task) like below:

task_id     stage        log_date
----------------------------------------
   3          1      2011-06-01 08:36:21
   9          1      2011-06-03 12:35:47
   3          2      2011-06-05 14:25:42
  21          1      2011-06-11 13:03:34
   9          2      2011-06-11 15:25:57
   3          3      2011-06-12 10:16:09
  21          2      2011-06-15 15:30:29
   3          4      2011-06-22 15:34:33
  21          3      2011-06-23 12:53:49
   9          4      2011-06-25 16:25:08

The data above is automatically populated when a task stage is progressed by some action in the application code. The stages run from 1 to 4. However, due to some logic, a task may skip stage 3. But all tasks end at stage 4. Probable task paths are like so:

(1,2,3,4) / (1,2,4) - Completed tasks
(1,2,3) / (1,2)     - In progress tasks

I need to query and retrieve a report that shows how long (in days) a task takes in each stage at a given time. I have come up with the following query so far:

SELECT z.task_id, a.log_date begin_date, d.log_date end_date, 
       DATEDIFF( b.log_date, a.log_date ) step1_days, 
       DATEDIFF( c.log_date, b.log_date ) step2_days, 
       DATEDIFF( d.log_date, c.log_date ) step3_days, 
       DATEDIFF( d.log_date, a.log_date ) cycle_days
FROM track_task z
LEFT JOIN track_task a ON ( z.task_id = a.task_id AND a.staging_id =1 )
LEFT JOIN track_task b ON ( z.task_id = b.task_id AND b.staging_id =2 )
LEFT JOIN track_task c ON ( z.task_id = c.task_id AND c.staging_id =3 )
LEFT JOIN track_task d ON ( z.task_id = d.task_id AND d.staging_id =4 )
GROUP BY z.oppty_id

to derive a result set like below:

task_id  begin_date   end_date   step1_days  step2_days  step3_days  cycle_days
-------------------------------------------------------------------------------
  3      2011-06-01  2011-06-22       4          7          10           21
  9      2011-06-03  2011-06-25       8         NULL       NULL          22
 21      2011-06-11     NULL          4          8         NULL         NULL

Is this a good way to go about it or there is a better way? How can I have the NULL values reported as zero? How can I retrieve then end_date for a task that is still in progress?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:54:28+00:00Added an answer on May 27, 2026 at 2:54 pm

    That looks about How I would do something like this, assuming there’s only going to ever be one instance of a stage used per task (if not, you need more work, both in defining your requirements, and writing the query).

    To make null appear as 0, use the COALESCE() function:

    SELECT z.task_id, COALESCE(DATEDIFF(b.log_date, a.log_date), 0) as step1_days
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
well i have this messages table with sample values like these: msg_id recipient_id read
I want to have a database table that keeps data with revision history (like
If I have a simple table where the data is such that the rows
I have create this little sample Table Person. The Id is a primary key
i have sample records ranking table SNO NAME TYPE RANK1-RANK2 DATE 2 sd sdf
I have a table with name,age and address.I have totally five rows of data
I have a simple table in MySql whose raison-d'être is to store logs. The
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
I have a simple table in Sybase, let's say it looks as follows: CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.