How to combine input from two records into one output record?

79 views Asked by At

I am generating a report which displays how long the plant was running for the day.

Since the operator shuts down the system at lunchtime, I have 2 records for the plant operation hours for each day. I want to display only one record that contains the production Start Time (6:00 am) and production End Time (around 4:00 pm). I have got a table Runtime_Combined that has an auto-incrementing index.

I want to select start date (e.g. 9/1/2021 6:04 AM, which has runtime_combined_ndx = 1) and end date (e.g. 9/1/21 4:23 PM, which has runtime_combined_ndx = 2).

SELECT ProductionStartDate, ProductionEndDate  
FROM Runtime_Combined     
WHERE month(ProductionStartDate) = month (ProductionStartDate)     
And day(ProductionStartDate) = day( ProductionStartDate) 

Sample data

2

There are 2 answers

0
Gordon Linoff On

You can use aggregation:

SELECT date(ProductionStartDate), sum(runtime_combined_ndx)  
FROM Runtime_Combined     
GROUP BY date(ProductionStartDate);
0
yurmix On

Use grouping. Something like:

SELECT MIN(ProductionStartDate) AS Start, MAX(ProductionEndDate) As End
FROM Runtime_Combined
WHERE <....>
GROUP BY DATE(ProductionStartDate)