1
Good morning, I have a query that brings in a column calculated the information of "Data" and "Historicos", in this column I have the item Prefix (vehicle plate) and the column with the respective calculation. I need to change this query so that you bring me the Prefix information and count how many words "Data" appeared for this Item. Below follows the query I am used as a basis to try to develop this:
SELECT
[CAM_CD] as COD
,[CAM_PF] As CAM
,[CAM_DS_PLACA] as PLACA
,[CAM_CR_LATITUDE] as LATITUDE
,[CAM_CR_LONGITUDE] as LONGITUDE
,[CAM_DT_REGISTRO] as DT_REGISTRO
,convert(char(8), [CAM_DT_REGISTRO], 108) as 'HoraA'
,DATEPART(Hour, [CAM_DT_REGISTRO]) as 'HoraB'
,DATEPART(DD,[CAM_DT_REGISTRO]) as 'Dia'
,DATEPART(MM,[CAM_DT_REGISTRO]) as 'Mes'
,DATEPART(YY,[CAM_DT_REGISTRO]) as 'Ano'
,[CAM_DT_BD]
--Coluna que deve ser contada na nova query
,case when
([CAM_DT_REGISTRO] < lag([CAM_DT_REGISTRO],1)over (PARTITION BY [CAM_PF] order by [CAM_CD] asc)) OR
([CAM_DT_REGISTRO] < lag([CAM_DT_REGISTRO],2)over (PARTITION BY [CAM_PF] order by [CAM_CD] asc))
then 'Historico' else 'Dados'
end as [Tipo]
FROM [KLABIN_SGL].[dbo].[TB_CAMINHAO] CONSCM
Where [CAM_DT_REGISTRO] between GETDATE()-1 and GETDATE()
and DATEPART(Hour, [CAM_DT_REGISTRO]) <= DATENAME(HOUR,SYSDATETIME())-1
Order By [CAM_PF], [CAM_CD];
The result of this table brings a great list cfme below:
The expected result is a query with the following structure:
CAM Historico Dados
BBM013 20 50
BBM017 15 45
In short, I’m trying to use the query that I mounted in a subquery, where bring in the main query the count of "DATA" and "HISTORICO", I really could not do this count of a calculated field.
Can someone please help me? give a few tips, examples?
Grateful!
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack