Syntax error
Msg 102, Level 15, State 1, Line 2087
Incorrect syntax near 'TAB'.
--TARIFAS
--Calcular Média Tarifada
BEGIN TRY DROP TABLE #TARIFASINDMETRO END TRY BEGIN CATCH END CATCH
SELECT
'IND' AS industria,
PD.[dtt_data_captura],
AVG(PD.flt_base) AS flt_base,
D.[Flt Type],
MT.AD,
D.[dtt_data]
INTO #TARIFASINDMETRO
FROM #Data D
INNER JOIN #PreData PD ON PD.GrupoQuery = D.GrupoQuery AND
PD.[dtt_geracao_ql2] = D.QTS AND
PD.[vch_empresa_editada] = D.[vch_empresa_editada] AND
--PD.[vch_tipo_arquivo] = D.[vch_tipo_arquivo] AND
PD.[vch_od] = D.[vch_od] AND
PD.[dtt_data] = D.[dtt_data] AND
PD.[Flt Type] = D.[Flt Type]
INNER JOIN @Metro MT ON MT.OC = D.[vch_od]
WHERE D.[vch_empresa_editada] IN ('G3','JJ','OC')
GROUP BY PD.[dtt_data_captura],
D.[Flt Type], MT.AD, D.[dtt_data]
But by doing the same code by changing the ending: WHERE D. [vch_company_edited] IN ('G3', 'JJ', 'OC'),
It's bringing me the average of the total column and not line by line.
example
IND Média Tarifada
804.9 669.9 NULL NULL 592.9 737.4
804.9 592.9 NULL NULL 592.9 698.9
804.9 639.9 NULL NULL 592.9 722.4
804.9 529.9 NULL NULL 592.9 667.4
804.9 419.9 NULL NULL 592.9 612.4
804.9 409.9 NULL NULL 592.9 607.4
The IND column is the one I need to insert and is repeating. The column of the average tariff is the correct result (average calculation by excel)