0
I have a query where I should register the lines with error with the word Historical, the lines that bring the date LESS than any of the dates of the previous lines should present the word HISTORICO, (In the same Item)
Can you help? the column in question is TYPE,
the result should be like the table below.
Tb_import [cod_title,date,imp_situation]
CD Item ValA ValB Data Tipo
01 A 10 15 2017-05-01 00:01:57.000 0
02 A 10 15 2017-05-01 00:02:15.000 0
03 A 10 15 2017-05-01 00:02:20.000 0
04 A 10 15 2017-05-01 00:03:40.000 0
05 A 10 15 2017-05-01 00:03:35.000 HISTORICO!
06 A 10 15 2017-05-01 00:04:20.000 0
07 A 10 15 2017-05-01 00:04:25.000 0
08 A 10 15 2017-05-01 00:04:50.000 0
09 A 10 15 2017-05-01 00:04:10.000 HISTORICO!
10 A 10 15 2017-05-01 00:04:30.000 0 <-- ??
11 B 10 15 2017-05-01 00:05:10.000 0
12 B 10 15 2017-05-01 00:05:30.000 0
13 B 10 15 2017-05-01 00:05:05.000 HISTORICO!
14 B 10 15 2017-05-01 00:05:50.000 0
Which database are you using? Could you post the query code you have already been able to create? Could post the structure of the tables you need to query (if possible share in sqlfidle)?
– Camilo Santos
Hello Friend , I confess that this is my first post and I’m still learning, the BD is Sqlserver, the information comes all from a single table, I set the example above for fear of posting data of the company I work, but if I can’t give an idea with them, i can try to assemble something different, I’m studying the posts yet to learn how to use sqlfidle,
– Alexandre Gonçalves
Just because of this information you posted makes it hard to help. Try to put more information about the problem, or some query you’ve already done. I don’t quite understand it
– Isaias Tavares
@Alexandregonçalves: What is the version of SQL Server? // To evaluate the sequence in column [Date] it is necessary that each row has a unique identification, which allows to obtain the expected order. How can you identify the order of the rows? In the example you posted there are repeated rows for the CD/Item/Vala/Valb columns.
– José Diz
@Josédiz , Thanks for the attention, in the rush I ended up posting wrong, sql server 2014, The CD column is unique, it doesn’t repeat, Can we get an idea? Tkss
– Alexandre Gonçalves
@Josédiz, A coluna onde traz histórico, seria um Case When, ou um select aninhado? I still have difficulties, I’m learning,
– Alexandre Gonçalves
@Alexandregonçalves: Assuming the sequence of times 4:50, 4:10, 4:30, 5:10, which values should be marked as HISTORICO: only 4:10 or 4:10 and 4:30?
– José Diz
@Josédiz Thank you very much for your attention, I spent all day training your examples, the code example #1 worked perfectly getting this way: SELECT top 100 [GRU_CD] ,[GRU_PF] as 'Crane' ,[GRU_DT_REGISTRO] as 'Registry' ,[GRU_DS_DATUM] ,case when [GRU_DT_REGISTRO] < lag([GRU_DT_REGISTRO]) over ( Order By [GRU_CD] ,[GRU_PF]) then 'HISTORICO' Else '0' end as Type FROM [KLABIN_SGL]. [dbo]. [TB_GRUA] Where [GRU_PF] in ('TE0048') AND DATEPART(mm,[GRU_DT_REGISTRO]) = 5 Order By [GRU_CD] ,[GRU_PF]
– Alexandre Gonçalves
@Josédiz however I need something that works like in code #2, He should check ALL previous items, and if the current line is smaller, then he should bring the word Historico, Assuming the time sequence 4:50, 4:10, 4:30, 5:10 in this case should mark as Historico the items 4:10 & 4:30 taking into account the Item and CD, :
– Alexandre Gonçalves
@Josédiz Segue, SELECT TOP 100 [GRU_CD] ,[GRU_PF] ,[GRU_DT_REGISTRO] , case exists when ( Select * From [KLABIN_SGL]. [dbo]. [TB_GRUA] as T2 Where T2.GRU_CD < T1.GRU_CD And T2. [GRU_DT_REGISTRO] > T1. [GRU_DT_REGISTRO]) Then 'Hist' Else '' end as Type FROM [KLABIN_SGL]. [dbo]. [TB_GRUA] as T1 Where [GRU_PF] in ('TE0048') AND DATEPART(mm,[GRU_DT_REGISTRO]) = 5 Order By [GRU_CD] ,[GRU_PF] ?
– Alexandre Gonçalves
@Josédiz for example on this line, He should mark HIST 2 x, following his example 2 on the lines 1532579 TE0048 2017-05-01 01:35:51.000 0
1532600 TE0048 2017-05-01 01:36:51.000 0
1532618 TE0048 2017-05-01 01:31:51.000 HISTORICO -- Here 1532654 TE0048 2017-05-01 01:35:05.000 0 Here 1532699 TE0048 2017-05-01 01:39:51.000 0 1532703 TE0048 2017-05-01 01:40:51,000 0
– Alexandre Gonçalves