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 9240909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:16:09+00:00 2026-06-18T08:16:09+00:00

So I have a table, at it’s base is the following structure: WorkOrder SubOrder

  • 0

So I have a table, at it’s base is the following structure:

  • WorkOrder
  • SubOrder
  • Status
  • StatusDate
  • Type
  • Category

(There are other things, but we’re ignoring them for the sake of the question)

I’m filtering for a certain subset of type/category, and I know how to do that. But what I need to come out is the following

WorkOrder | SubOrder | Type - Category | Min(StatusDate) where Status = Open | Min(StatusDate) where Status = Complete | Calculated Date Difference between the two

I can get one at a time and that’s no issue. Ror the time being, I’ve just been monkeying with it in Excel. I need to however get this into a SQL statement for a report I need to start generating. There may or may not be a StatusDate with the status Complete (if the SubOrder has not been marked complete elsewhere), and the dataset needs to reflect that as well. There could be multiple times that each SubOrder was opened and completed, so we just need the first (hence the Min())

Any ideas on how I should approach this task? (And no, changing the data structure is not allowed, OTS software, and we’re not paying to change it)

  • 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-06-18T08:16:10+00:00Added an answer on June 18, 2026 at 8:16 am

    I’m not sure that I understand why you would need to join the table to itself. It sounds like you just want

    SELECT WorkOrder,
           SubOrder,
           Type,
           Category,
           min( case when Status = 'Open' 
                     then StatusDate 
                     else null 
                 end) min_open_status_date,
           min( case when Status = 'Complete' 
                     then StatusDate 
                     else null 
                 end) min_complete_status_date,
           min( case when Status = 'Complete' 
                     then StatusDate 
                     else null 
                  end) - 
             min( case when Status = 'Open' 
                       then StatusDate 
                       else null 
                    end)
      FROM table_name
     WHERE <<whatever conditions>>
     GROUP BY WorkOrder,
              SubOrder,
              Type,
              Category
    

    If that is not what you are looking for, it would be very helpful to edit your question post the DDL to create the table, the DML to populate some rows, and tell us exactly what output you want for that sample data. Or create a fiddle on sqlfiddle.com. Otherwise, we have to guess at what you want. For example, you say that you want to calculate the difference between two dates but one of the two dates may not exist– do you want to use some baseline date for the calculation if there is no closed date? This query will return NULL for the calculated difference which may or may not be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table called site_down. There I have the following fields values. Day_of_month_from Day_of_month_to
I have table with around 70 000 rows. There is 6000 rows that i
We have table structure like id | startDay | startMonth | startYear | endDay
i have table for quiz results, in the table there a cell for grade.
I have table with structure like this: -------------------------------------------- | id | tid | uid
I have table in my data base with these specs: one PK 3 fields
I have table entries in my database with structure: id | card | event
I have table of over 4 million rows and accidentally in one column there
I have table of products, and there are 2 fields: price, currency. Possible values
I have table like below : <table> <thead> <th>Product Type</th> <th>Quantity</th> <th>Unit</th> <th>Total</th> </thead>

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.