Sum a union query
I'm using Microsoft SQL Svr Mgmt Studio 2008. I don't have access to create a temporary table (company restricts ability to create or modify tables) or I would use that to solve this problem.
I have successfully used a union query to combine the results of three select queries. Now I am trying to sum the results of the union.
When I execute the query below I receive:
Incorrect syntax near the keyword 'GROUP'
And then when I remove the group by I get:
Incorrect syntax near ')'
Here's my query so far:
Select Period, PCC, SUM(BasicHits), SUM(FareHits), S开发者_StackOverflow中文版UM(SearchHits)
From (
SELECT AAAPeriod AS Period,
AAAFromPCC AS PCC,
- SUM(AAABasic) AS BasicHits,
- SUM(AAAFare) AS FareHits,
- SUM(AAASearch) AS SearchHits
FROM HitsAaa
HAVING (AAAPeriod = N'2010-10')
UNION ALL
SELECT AAAPeriod,
AAAtoPCC,
SUM(AAABasic),
SUM(AAAFare),
SUM(AAASearch)
FROM HitsAaa
HAVING (AAAPeriod = N'2010-10')
UNION ALL
SELECT AgtPeriod,
AgtPcc,
SUM(AgtBasic),
SUM(AgtFare),
SUM(AgtSearch)
FROM HitsAgent
HAVING (AgtPeriod = N'2010-10')
)GROUP BY Period, PCC
I haven't been able to find a solution to this on any of the previous questions.
You need to alias your derived table, you must also use a group by with a having clause.
SELECT
q1.Period,
q1.PCC,
SUM(q1.BasicHits),
SUM(q1.FareHits),
SUM(q1.SearchHits)
FROM (SELECT
AAAPeriod AS Period,
AAAFromPCC AS PCC,
- SUM(AAABasic) AS BasicHits,
- SUM(AAAFare) AS FareHits,
- SUM(AAASearch) AS SearchHits
FROM HitsAaa
GROUP BY
AAAPeriod,
AAAFromPCC
HAVING (AAAPeriod = N'2010-10')
UNION ALL
SELECT
AAAPeriod AS Period,
AAAtoPCC AS PCC,
SUM(AAABasic) AS BasicHits,
SUM(AAAFare) AS FareHits,
SUM(AAASearch) AS SearchHits
FROM HitsAaa
GROUP BY
AAAPeriod,
AAAtoPCC
HAVING (AAAPeriod = N'2010-10')
UNION ALL
SELECT
AgtPeriod AS Period,
AgtPcc AS PCC,
SUM(AgtBasic) AS BasicHits,
SUM(AgtFare) AS FareHits,
SUM(AgtSearch) AS SearchHits
FROM HitsAgent
GROUP BY
AgtPeriod,
AgtPCC
HAVING (AgtPeriod = N'2010-10')) q1
GROUP BY
q1.Period,
q1.PCC
SQL Server requires that you define a table alias for a derived table/inline view:
SELECT x.period, x.pcc, SUM(x.BasicHits), SUM(x.FareHits), SUM(x.SearchHits)
FROM (SELECT AAAPeriod AS Period,
AAAFromPCC AS PCC,
- SUM(AAABasic) AS BasicHits,
- SUM(AAAFare) AS FareHits,
- SUM(AAASearch) AS SearchHits
FROM HitsAaa
WHERE AAAPeriod = N'2010-10'
GROUP BY aaaperiod, aaafrompcc
UNION ALL
SELECT AAAPeriod,
AAAtoPCC,
SUM(AAABasic),
SUM(AAAFare),
SUM(AAASearch)
FROM HitsAaa
WHERE AAAPeriod = N'2010-10'
GROUP BY aaaperiod, aaafrompcc
UNION ALL
SELECT AgtPeriod,
AgtPcc,
SUM(AgtBasic),
SUM(AgtFare),
SUM(AgtSearch)
FROM HitsAgent
WHERE AgtPeriod = N'2010-10'
GROUP BY agtperiod, agtpcc) AS x
GROUP BY x.period, x.pcc
I don't have access to create a temporary table (company restricts ability to create or modify tables) or I would use that to solve this problem.
Instead of a temporary table, try using a table variable:
declare @t table (id int primary key, col1 varchar(50))
insert @t (col1) values ('hello table variable')
select * from @t
A table variable can do most of the things a temporary table can.
Like Martin's (now deleted) answer suggests, consider giving the subquery an alias, like:
select ... list of columns ...
from (
... subquery ...
) as SubQueryAlias
group by
col1
And in your subquery, the having
should probably be a where
:
...
FROM HitsAaa
WHERE (AAAPeriod = N'2010-10')
...
Change your first line to
Select T.Period, T.PCC, SUM(T.BasicHits), SUM(T.FareHits), SUM(T.SearchHits)
and the last line to
) T GROUP BY T.Period, T.PCC
You need to define a table alias (in this case T) for inner tables
Also, you need to GROUP BY
the inner queries
select R."Artigo", R."Dscription" as "Descricao", R."Codigo", R."Cliente", R."ItmsGrpCod", Sum(R."Qtd/Kg") as "Qtd/Kg", Sum(R."Valor") as "Valor", Sum(R."Valor")/Sum(R."Qtd/Kg") as "PMVL" from ( select T1."ItemCode" as "Artigo", T1."Dscription", T0."CardCode" as "Codigo", T0."CardName" as "Cliente", T3."ItmsGrpCod", Sum(T1."Quantity")-1 as "Qtd/Kg", Sum(T1."LineTotal")-1 as "Valor", ( Sum(T1."LineTotal") / Sum(T1."Quantity") ) as "PMVL" from ORIN T0 INNER JOIN RIN1 T1 ON T0."DocEntry" = T1."DocEntry" RIGHT JOIN OITM T3 ON T1."ItemCode" = T3."ItemCode" LEFT JOIN IBT1 T2 ON T0."DocEntry" = T2."BaseEntry" AND T1."ItemCode"=T2."ItemCode" and T2."BaseLinNum"=T1."LineNum" where T0."DocDate" >= ('2021-03-19') and T0."DocDate" <= ('2021-03-19') and T3."ItmsGrpCod" like '___' and T0.CANCELED = 'N' group by T1."ItemCode", T1."Dscription", T0."CardCode", T0."CardName", T3."ItmsGrpCod" union -- facturas
select T1."ItemCode" as "Artigo", T1."Dscription", T0."CardCode" as "Codigo", T0."CardName" as "Cliente", T3."ItmsGrpCod", Sum(T1."Quantity") as "Qtd/Kg", Sum(T1."LineTotal") as "Valor", ( Sum(T1."LineTotal") / Sum(T1."Quantity") ) as "PMVL" from OINV T0 INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry" -- LEFT JOIN OITM T3 ON T1."ItemCode" = T3."CardCode" left JOIN OITM T3 ON T1."ItemCode" = T3."ItemCode" -- INNER JOIN OITM T3 ON T3."ItemCode" = T3."CardCode" LEFT JOIN IBT1 T2 ON T0."DocEntry" = T2."BaseEntry" AND T1."ItemCode"=T2."ItemCode" and T2."BaseLinNum"=t1."LineNum"
where T0."DocDate" >= ('2021-03-19') and T0."DocDate" <= ('2021-03-19')
and T3."ItmsGrpCod" like '___'
group by T1."ItemCode", T1."Dscription", T0."CardCode", T0."CardName",T3."ItmsGrpCod"
) as R
group by R."Artigo", R."Dscription", R."Codigo", R."Cliente",R."ItmsGrpCod"
精彩评论