尝试加入两个Pivot SQL查询查询

时间:2014-10-30 16:44:49

标签: sql-server sql-server-2012

我创建了两个sql查询,第一个数量为12个月,第二个总和超过12个月。我试图将这些合并到一个查询中,但无论我尝试什么错误。

预期输出在一行上为24列,例如Jan,JanQty,Feb,FebQty,Mar,MarQty等。

任何帮助都会很棒!

First Query(Qty):

SELECT
SUM(JanQty) as 'JanQty',
SUM(FebQty) as 'FebQty',
SUM(MarQty) as 'MarQty',
SUM(AprQty) as 'AprQty',
SUM(MayQty) as 'MayQty',
SUM(JuneQty) as 'JuneQty',
SUM(JulyQty) as 'JulyQty',
SUM(AugQty) as 'AugQty',
SUM(SeptQty) as 'SeptQty',
SUM(OctQty) as 'OctQty',
SUM(NovQty) as 'NovQty',
SUM(DecQty) as 'DecQty'
from (

SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
from
(select SUM(T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
from INV1 T0
inner join OINV T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and 
year(T1.DocDate) = 2014
group by t0.Quantity, t1.DocDate) s

Pivot
(SUM(QtyBal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p

Union ALL

SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
from
(select SUM(-T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
from RIN1 T0
inner join ORIN T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and 
year(T1.DocDate) = 2014
group by -t0.Quantity, t1.DocDate) s

Pivot
(SUM(QtyBal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p

) sq

第二个查询(值):

SELECT
SUM(Jan) as 'Jan',
SUM(Feb) as 'Feb',
SUM(Mar) as 'Mar',
SUM(Apr) as 'Apr',
SUM(May) as 'May',
SUM(June) as 'June',
SUM(July) as 'July',
SUM(Aug) as 'Aug',
SUM(Sept) as 'Sept',
SUM(oct) as 'Oct',
SUM(nov) as 'Nov',
SUM(Dec) as 'Dec'
from (

SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
from
(select SUM(T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
from INV1 T0
inner join OINV T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and 
year(T1.DocDate) = 2014
group by t0.LineTotal, t1.DocDate) s

Pivot
(SUM(Bal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p

Union ALL

SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
from
(select SUM(-T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
from RIN1 T0
inner join ORIN T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and 
year(T1.DocDate) = 2014
group by -t0.LineTotal, t1.DocDate) s

Pivot
(SUM(Bal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p

) sq

1 个答案:

答案 0 :(得分:0)

尝试使用CROSS JOIN:

SELECT 
    Jan, 
    JanQty
FROM 
  (
    SELECT
        SUM(JanQty) as 'JanQty',
        SUM(FebQty) as 'FebQty',
        SUM(MarQty) as 'MarQty',
        SUM(AprQty) as 'AprQty',
        SUM(MayQty) as 'MayQty',
        SUM(JuneQty) as 'JuneQty',
        SUM(JulyQty) as 'JulyQty',
        SUM(AugQty) as 'AugQty',
        SUM(SeptQty) as 'SeptQty',
        SUM(OctQty) as 'OctQty',
        SUM(NovQty) as 'NovQty',
        SUM(DecQty) as 'DecQty'
    FROM 
      (
        SELECT
            ISNULL([1],0) as JanQty,
            ISNULL([2],0) as FebQty,
            ISNULL([3],0) as MarQty,
            ISNULL([4],0) as AprQty,
            ISNULL([5],0) as MayQty,
            ISNULL([6],0) as JuneQty,
            ISNULL([7],0) as JulyQty,
            ISNULL([8],0) as AugQty,
            ISNULL([9],0) as SeptQty,
            ISNULL([10],0) as OctQty,
            ISNULL([11],0) as NovQty,
            ISNULL([12],0) as DecQty
        FROM 
          (
            SELECT 
                SUM(T0.Quantity) as QtyBal,
                MONTH(T1.DocDate) as Month
            FROM 
                INV1 T0
                 inner join 
                OINV T1 on t0.DocEntry = t1.DocEntry
            WHERE 
                t1.DocDate BETWEEN '20140101' AND '20141231' and 
                year(T1.DocDate) = 2014
            GROUP BY t0.Quantity, t1.DocDate
          ) s

        PIVOT 
          (
            SUM(QtyBal) FOR 
            Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
          ) p

        UNION ALL

        SELECT
            ISNULL([1],0) as JanQty,
            ISNULL([2],0) as FebQty,
            ISNULL([3],0) as MarQty,
            ISNULL([4],0) as AprQty,
            ISNULL([5],0) as MayQty,
            ISNULL([6],0) as JuneQty,
            ISNULL([7],0) as JulyQty,
            ISNULL([8],0) as AugQty,
            ISNULL([9],0) as SeptQty,
            ISNULL([10],0) as OctQty,
            ISNULL([11],0) as NovQty,
            ISNULL([12],0) as DecQty
        from
          (
            select 
                SUM(-T0.Quantity) as QtyBal,
                MONTH(T1.DocDate) as Month
            from 
                RIN1 T0
                 inner join 
                ORIN T1 on t0.DocEntry = t1.DocEntry
            where 
                t1.DocDate BETWEEN '20140101' AND '20141231' and 
                year(T1.DocDate) = 2014
            group by -t0.Quantity, t1.DocDate) s

            Pivot
              (
                SUM(QtyBal) FOR 
                Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
              ) p
      ) sqa
  ) qty
     CROSS JOIN 
  (
    SELECT
        SUM(Jan) as 'Jan',
        SUM(Feb) as 'Feb',
        SUM(Mar) as 'Mar',
        SUM(Apr) as 'Apr',
        SUM(May) as 'May',
        SUM(June) as 'June',
        SUM(July) as 'July',
        SUM(Aug) as 'Aug',
        SUM(Sept) as 'Sept',
        SUM(oct) as 'Oct',
        SUM(nov) as 'Nov',
        SUM(Dec) as 'Dec'
    FROM
      (
        SELECT
            ISNULL([1],0) as Jan,
            ISNULL([2],0) as Feb,
            ISNULL([3],0) as Mar,
            ISNULL([4],0) as Apr,
            ISNULL([5],0) as May,
            ISNULL([6],0) as June,
            ISNULL([7],0) as July,
            ISNULL([8],0) as Aug,
            ISNULL([9],0) as Sept,
            ISNULL([10],0) as Oct,
            ISNULL([11],0) as Nov,
            ISNULL([12],0) as Dec
        FROM
          (
            SELECT 
                SUM(T0.LineTotal) as Bal,
                MONTH(T1.DocDate) as Month
            FROM 
                INV1 T0
                 inner join 
                OINV T1 on t0.DocEntry = t1.DocEntry
            WHERE 
                t1.DocDate BETWEEN '20140101' AND '20141231' and 
                year(T1.DocDate) = 2014
            GROUP BY t0.LineTotal, t1.DocDate
          ) s

        PIVOT 
          (
            SUM(Bal) FOR 
            Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
          ) p

        UNION ALL 

        SELECT
            ISNULL([1],0) as Jan,
            ISNULL([2],0) as Feb,
            ISNULL([3],0) as Mar,
            ISNULL([4],0) as Apr,
            ISNULL([5],0) as May,
            ISNULL([6],0) as June,
            ISNULL([7],0) as July,
            ISNULL([8],0) as Aug,
            ISNULL([9],0) as Sept,
            ISNULL([10],0) as Oct,
            ISNULL([11],0) as Nov,
            ISNULL([12],0) as Dec
        from
          (
            select 
                SUM(-T0.LineTotal) as Bal,
                MONTH(T1.DocDate) as Month
            from 
                RIN1 T0
                 inner join 
                ORIN T1 on t0.DocEntry = t1.DocEntry
            where 
                t1.DocDate BETWEEN '20140101' AND '20141231' and 
                year(T1.DocDate) = 2014
            group by -t0.LineTotal, t1.DocDate
          ) s

        Pivot
          (
            SUM(Bal) FOR 
            Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
          ) p
      ) sqb
  ) Bal