sql - How do I aggregate 3 columns that are different with MIN(DATE)? -
i'm facing simple problem here can't solve, have query:
select min(tea_iniciotarefa), pfj_id_analista, atc_id, srv_id dbo.tarefaetapaareatecnica inner join tarefa t on t.trf_id = tarefaetapaareatecnica.trf_id srv_id = 88 group srv_id, atc_id, pfj_id_analista order atc_id asc
it returns me this:
i able group little group srv_id, atc_id, pfj_id_analista gave me these 8 records, can see pfj_id_analista different.
what want select date of each srv_id , atc_id, pfj_id_analista don't need grup, if remove pfj_id_analista grouping query works, need column.
for eg.: between row number 2 , 3 want date, row 2. same goes rows 5 8, want row 6.
ddl tarefaetapaareatecnica (important key: trf_id)
create table [dbo].[tarefaetapaareatecnica]( [tea_id] [int] identity(1,1) not null, **[trf_id] [int] not null,** [ets_id] [int] not null, [atc_id] [int] not null, [tea_revisao] [int] not null, [pfj_id_projetista] [int] null, [tea_doctosqtd] [int] null, [tea_iniciotarefa] [datetime2](7) null, [pfj_id_analista] [int] null, [tea_fimtarefa] [datetime2](7) null, [tea_horasqtd] [numeric](18, 1) null, [tea_ncfqtd] [int] null, [pat_id] [int] null
ddl tarefa (important keys trf_id , srv_id (which need it)):
create table [dbo].[tarefa]( **[trf_id] [int] identity(1,1) not replication not null,** **[srv_id] [int] not null,** [trt_id] [int] not null, [trf_descr] [varchar](255) null, [trf_entrada] [datetime] not null, [trf_doctosqtd] [int] not null, [trf_devolucao] [datetime] null, [trf_ncfqtd] [int] null, [trf_ehdocinsuf] [bit] null, [trf_observ] [varchar](255) null, [trf_areastrfqtd] [int] null, [trf_areastrfliqqtd] [int] null
thanks lot.
edit: correct query
based on @gordon linoff post:
select t.tea_iniciotarefa, t.pfj_id_analista, t.atc_id, t.srv_id (select t.*, row_number() on (partition atc_id, srv_id order tea_iniciotarefa) seqnum, ta.srv_id dbo.tarefaetapaareatecnica t inner join dbo.tarefa ta on t.trf_id = ta.trf_id ) t seqnum = 1 , t.srv_id = 88
just use window functions:
select t.* (select t.*, row_number() on (partition atc_id, srv_id order ini) seqnum dbo.tarefaetapaareatecnica t ) t seqnum = 1;
this example of filtering, not aggregation. problem getting right value filter on.
Comments
Post a Comment