0
Good Afternoon Please help me out
Error in the Syntax
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_empresa_edited] IN ('G3','JJ','OC') ,
is bringing me the total column average and not row by row.
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 that needs to be inserted and is repeating. The column of the charged average is the correct result (calculation by excel Average)
Could you give us the full SQL please!? because there are several open parentheses without closure, which makes it impossible to know where select ends.
– Andrew Paes
Apparently you have several Selects within GROUP BY, without closing parentheses. What is the purpose of this query? Could give us an example of desired result. You are mounting this query?
– Andrew Paes
It is very large. There is a way to attach?
– Thais
No need to attach it. Simply put it there, we will edit it. because we need the whole query to know where the error is.
– Andrew Paes
Thank you, so now I need to calculate the charged average of the three G3, JJ, OC to appear in a new column.
– Thais
Well, let’s split up. Your error message says that you have an incorrect syntax near the word TAB, but this word is not in your query. Therefore, I assume that your query is only part of a trial or TSQL. I understand that you want to calculate charged average, however, this is not a database operation. so tell which columns of which tables you want to average. And you want the result to appear where, in which table or query?
– Andrew Paes
And now I need a column that brings the average of the three. But by making the same code changing the ending: WHERE D.[vch_empresa_edited] IN ('P1','P2','P3') is bringing me the average of the total column and not row by row.
– Thais
Include in your select the following field, so as to distinguish in the calculation: D.[vch_empresa_edited]
– Andrew Paes
keep your WHERE clause as is and include within the select this field used in the Where clause. Type, "select 'IND' as industria, D.[vch_empresa_editada]"
– Andrew Paes
is bringing me the average of the total column and not row by row. The IND column is the one I need to insert and is repeating. The column of the charged average is the correct result (calculation Average by excel) I edited the code again
– Thais