sql - a specific way to use max() -


i have datetime, total, material, , mix columns.

this equipment runs mix x amount of hours, switches mix b x amount of hours. switches mix a.

i have query just:

select max(total), material, mix  database  group material, mix 

however need catch fact mix ran twice , 1 value recorded , wouldn't know how material used in 1 skipped.

is there way @ max(total), new max every time mix or material changes? (not actual different mix / material when changes mix b mix a.

edit show data looks like

timestamp                  mix           material          tons   2017-02-08 07:49:25.000    mixa          mat1              30.05 2017-02-08 07:50:25.000    mixa          mat1              30.27 2017-02-08 07:51:25.000    mixa          mat1              30.50 2017-02-08 07:52:25.000    mixa          mat1              30.76 2017-02-08 07:53:25.000    mixa          mat1              31.01 2017-02-08 07:58:25.000    mixb          mat1              0.1 2017-02-08 07:59:25.000    mixb          mat1              0.8 2017-02-08 08:00:25.000    mixb          mat1              1.3 2017-02-08 08:01:25.000    mixb          mat1              2.0 2017-02-08 08:02:25.000    mixb          mat1              2.5 2017-02-08 08:10:25.000    mixa          mat1              0.1 2017-02-08 08:01:25.000    mixa          mat1              0.5 

so, before first record shown above machine running amount of time. switched mixes, ran, switched , ended production day.

i'm trying find efficient way find amount of material used, because tons column running total , can't sum it. max() simple, miss second run of mix a.

it looks gap-and-islands me.

sample data

note, added mat2 show need partition , fixed timestamp of last entry.

declare @t table(ts datetime2(0), mix varchar(50), material varchar(50), tons float);  insert @t (ts, mix, material, tons) values ('2017-02-08 07:49:25', 'mixa', 'mat1', 30.05), ('2017-02-08 07:50:25', 'mixa', 'mat1', 30.27), ('2017-02-08 07:51:25', 'mixa', 'mat1', 30.50), ('2017-02-08 07:52:25', 'mixa', 'mat1', 30.76), ('2017-02-08 07:53:25', 'mixa', 'mat1', 31.01), ('2017-02-08 07:58:25', 'mixb', 'mat1', 0.1  ), ('2017-02-08 07:59:25', 'mixb', 'mat1', 0.8  ), ('2017-02-08 08:00:25', 'mixb', 'mat1', 1.3  ), ('2017-02-08 08:01:25', 'mixb', 'mat1', 2.0  ), ('2017-02-08 08:02:25', 'mixb', 'mat1', 2.5  ), ('2017-02-08 08:10:25', 'mixa', 'mat1', 0.1  ), ('2017-02-08 08:11:25', 'mixa', 'mat1', 0.5  ), ('2017-02-08 07:49:25', 'mixa', 'mat2', 30.05), ('2017-02-08 07:50:25', 'mixa', 'mat2', 30.27), ('2017-02-08 07:51:25', 'mixa', 'mat2', 30.50), ('2017-02-08 07:52:25', 'mixa', 'mat2', 30.76), ('2017-02-08 07:53:25', 'mixa', 'mat2', 31.01), ('2017-02-08 07:58:25', 'mixb', 'mat2', 0.1  ), ('2017-02-08 07:59:25', 'mixb', 'mat2', 0.8  ), ('2017-02-08 08:00:25', 'mixb', 'mat2', 1.3  ), ('2017-02-08 08:01:25', 'mixb', 'mat2', 2.0  ), ('2017-02-08 08:02:25', 'mixb', 'mat2', 2.5  ), ('2017-02-08 08:10:25', 'mixa', 'mat2', 0.1  ), ('2017-02-08 08:11:25', 'mixa', 'mat2', 0.5  ); 

query

with cte_rn (     select         ts         ,mix         ,material         ,tons         ,row_number() on (partition material order ts) rn1         ,row_number() on (partition material, mix order ts) rn2     @t ) ,cte_groups (     select         ts         ,mix         ,material         ,tons         ,rn1         ,rn2         ,rn1 - rn2 groupnumber     cte_rn ) select     material     ,mix     ,max(tons) maxtons     ,max(ts) maxts cte_groups group     material     ,groupnumber     ,mix order     material     ,maxts ; 

result

+----------+------+---------+---------------------+ | material | mix  | maxtons |        maxts        | +----------+------+---------+---------------------+ | mat1     | mixa | 31.01   | 2017-02-08 07:53:25 | | mat1     | mixb | 2.5     | 2017-02-08 08:02:25 | | mat1     | mixa | 0.5     | 2017-02-08 08:11:25 | | mat2     | mixa | 31.01   | 2017-02-08 07:53:25 | | mat2     | mixb | 2.5     | 2017-02-08 08:02:25 | | mat2     | mixa | 0.5     | 2017-02-08 08:11:25 | +----------+------+---------+---------------------+ 

you can sum further add 31.01 , 0.5 mat1 if need it. add 1 more group material, mix. question not clear result should be.

how works

run query step-by-step, cte-by-cte understand how works.

"gaps , islands" classic problem. when mix changes, "island" followed "gap".

cte_rn calculates 2 sets of row numbers. difference between them number of group (cte_groups).

with ... select * cte_groups order material, ts; 

produces

+---------------------+------+----------+-------+-----+-----+-------------+ |         ts          | mix  | material | tons  | rn1 | rn2 | groupnumber | +---------------------+------+----------+-------+-----+-----+-------------+ | 2017-02-08 07:49:25 | mixa | mat1     | 30.05 |   1 |   1 |           0 | | 2017-02-08 07:50:25 | mixa | mat1     | 30.27 |   2 |   2 |           0 | | 2017-02-08 07:51:25 | mixa | mat1     | 30.5  |   3 |   3 |           0 | | 2017-02-08 07:52:25 | mixa | mat1     | 30.76 |   4 |   4 |           0 | | 2017-02-08 07:53:25 | mixa | mat1     | 31.01 |   5 |   5 |           0 | | 2017-02-08 07:58:25 | mixb | mat1     | 0.1   |   6 |   1 |           5 | | 2017-02-08 07:59:25 | mixb | mat1     | 0.8   |   7 |   2 |           5 | | 2017-02-08 08:00:25 | mixb | mat1     | 1.3   |   8 |   3 |           5 | | 2017-02-08 08:01:25 | mixb | mat1     | 2     |   9 |   4 |           5 | | 2017-02-08 08:02:25 | mixb | mat1     | 2.5   |  10 |   5 |           5 | | 2017-02-08 08:10:25 | mixa | mat1     | 0.1   |  11 |   6 |           5 | | 2017-02-08 08:11:25 | mixa | mat1     | 0.5   |  12 |   7 |           5 | | 2017-02-08 07:49:25 | mixa | mat2     | 30.05 |   1 |   1 |           0 | | 2017-02-08 07:50:25 | mixa | mat2     | 30.27 |   2 |   2 |           0 | | 2017-02-08 07:51:25 | mixa | mat2     | 30.5  |   3 |   3 |           0 | | 2017-02-08 07:52:25 | mixa | mat2     | 30.76 |   4 |   4 |           0 | | 2017-02-08 07:53:25 | mixa | mat2     | 31.01 |   5 |   5 |           0 | | 2017-02-08 07:58:25 | mixb | mat2     | 0.1   |   6 |   1 |           5 | | 2017-02-08 07:59:25 | mixb | mat2     | 0.8   |   7 |   2 |           5 | | 2017-02-08 08:00:25 | mixb | mat2     | 1.3   |   8 |   3 |           5 | | 2017-02-08 08:01:25 | mixb | mat2     | 2     |   9 |   4 |           5 | | 2017-02-08 08:02:25 | mixb | mat2     | 2.5   |  10 |   5 |           5 | | 2017-02-08 08:10:25 | mixa | mat2     | 0.1   |  11 |   6 |           5 | | 2017-02-08 08:11:25 | mixa | mat2     | 0.5   |  12 |   7 |           5 | +---------------------+------+----------+-------+-----+-----+-------------+ 

then group material, groupnumber, mix final result.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -