Good morning, I have a query that brings in a calculated column the information of "Data" and "Historicos", in this column I have the Prefix of the item (vehicle license plate) and the column with its calculation. I need to change this query so that it brings me the Prefix information and the count of how many words "Data" appeared for this Item. Below is the query I'm using as a basis for trying 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 large cfme list below:
Theexpectedresultisaquerywiththefollowingstructure:
CAMHistoricoDadosBBM0132050BBM0171545
Inshort,I'mtryingtousethequeryIputupinasubquery,whereIbringthe"DATA" and "HISTORIC" count in the main query, I really could not do that counting for a calculated field.
Can someone please help me? give some tips, examples?
Thank you!