求和联合查询

时间:2011-04-10 18:11:30

标签: sql sql-server union

我正在使用Microsoft SQL Svr Mgmt Studio 2008.我无权创建临时表(公司限制创建或修改表的能力),或者我会使用它来解决此问题。

我已成功使用联合查询来组合三个选择查询的结果。现在我试图总结联盟的结果。

当我执行下面的查询时,我收到:

Incorrect syntax near the keyword 'GROUP'

然后当我删除该组时,我得到:

Incorrect syntax near ')'

到目前为止,这是我的查询:

Select Period, PCC, SUM(BasicHits), SUM(FareHits), SUM(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

在以前的任何问题上,我都无法找到解决方法。

5 个答案:

答案 0 :(得分:6)

您需要为派生表设置别名,还必须使用带有having子句的group by。

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

答案 1 :(得分:5)

SQL Server要求您为派生表/内联视图定义表别名:

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

答案 2 :(得分:2)

  

我没有权限创建一个   临时表(公司限制   能够创建或修改表格)或   我会用它来解决这个问题   问题

尝试使用表变量:

而不是临时表
declare @t table (id int primary key, col1 varchar(50))
insert @t (col1) values ('hello table variable')
select * from @t

表变量可以完成临时表所能做的大部分工作。

就像Martin(现已删除)的回答所示,考虑给子查询一个别名,例如:

select  ... list of columns ...
from    (
        ... subquery ...
        ) as SubQueryAlias
group by
        col1

在您的子查询中,having应该是where

...
FROM        HitsAaa
WHERE       (AAAPeriod = N'2010-10') 
...

答案 3 :(得分:2)

将第一行更改为

Select T.Period, T.PCC, SUM(T.BasicHits), SUM(T.FareHits), SUM(T.SearchHits)

的最后一行
) T GROUP BY T.Period, T.PCC

您需要为内部表

定义表别名(在本例中为T)

此外,您需要GROUP BY内部查询

答案 4 :(得分:-1)

选择 R."Artigo", R."Dscription" 为 "Descricao", R."Codigo", R."客户", R."ItmsGrpCod", Sum(R."Qtd/Kg") 为 "Qtd/Kg", Sum(R."Valor") 为 "Valor", Sum(R."Valor")/Sum(R."Qtd/Kg") 为 "PMVL" 从 ( 选择 T1."ItemCode" 作为 "Artigo", T1."Dscription", T0."CardCode" 为 "Codigo", T0."CardName" 为 "Cliente", T3."ItmsGrpCod", Sum(T1."Quantity")-1 为 "Qtd/Kg", Sum(T1."LineTotal")-1 为 "Valor", ( Sum(T1."LineTotal") / Sum(T1."Quantity") ) 为 "PMVL" 从 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" 其中 T0."DocDate" >= ('2021-03-19') 和 T0."DocDate" <= ('2021-03-19') 和 T3."ItmsGrpCod" 像 '___' 和 T0.CANCELED = 'N' group by T1."ItemCode", T1."Dscription", T0."CardCode", T0."CardName", T3."ItmsGrpCod" 联盟 -- 事实

选择 T1."ItemCode" 为 "Artigo", T1."Dscription", T0."CardCode" 为 "Codigo", T0."CardName" 为 "Cliente", T3."ItmsGrpCod", Sum(T1."Quantity") 为 "Qtd/Kg", Sum(T1."LineTotal") 为 "Valor", ( Sum(T1."LineTotal") / Sum(T1."Quantity) ") ) 作为 "PMVL" 从 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" -- T3 上的 INNER JOIN OITM 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"