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)
I’m not sure that I understand why you would need to join the table to itself. It sounds like you just want
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
NULLfor the calculated difference which may or may not be what you want.