将不同的表链接到SQL查询中

时间:2014-11-06 15:16:33

标签: sql sql-server-2012

我有以下SQL查询,按月计算数量和值,然后将它们放入24列,所以数据如下所示:

Jan , JanQty, Feb  , FebQTY, Mar , MarQty 
1285,   35  , 3228 ,   36  , 1750,   18    

但现在我想添加业务合作伙伴(OINV& ORIN)和项目(INV1& RIN1)等字段,以便数据从不同的表格中删除项目/业务合作伙伴的更多行,以便它看起来像这样:

Business Partner, Items   , Jan, JanQty, Feb  , FebQTY, Mar, MarQty
SAP Ltd         , Software, 150, 1     , 0    , 0     , 500, 2 
SAP Ltd         , Phones  , 175, 4     , 145  , 1     , 100, 1
SAP Ltd         , Tablets , 10 , 9     , 86   , 8     , 400, 3 
JPC Ltd         , Software, 350, 7     , 999  , 9     , 250, 4 
JPC Ltd         , PC's    , 350, 7     , 999  , 9     , 250, 4
JPC Ltd         , Software, 350, 7     , 999  , 9     , 250, 4

我已尝试了许多不同的方法,但无法让它发挥作用,任何帮助都会很棒。

以下是我的查询

SELECT 
        Jan, 
        JanQty,
        Feb,
        FebQty
        Mar,
        MarQty,
        Apr,
        AprQty,
        May,
        MayQty,
        June,
        JuneQty,
        July,
        JulyQty,
        Aug,
        AugQty,
        Sept,
        SeptQty,
        Oct,
        OctQty,
        Nov,
        NovQty,
        Dec,
        DecQty
    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((case when T0.NoInvtryMv = 'Y' then t0.Quantity else -t0.Quantity end)) 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 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 

2 个答案:

答案 0 :(得分:1)

我假设2个附加字段已经是基表的一部分了?在这种情况下,您可以在嵌套查询的最低级别将它们添加为组,例如

 SELECT             Business_Partner, 
                    Items,
                    ISNULL([1],0) as Jan,
                    ISNULL([2],0) as Feb,
                    ISNULL([3],0) as Mar,

将该组分组到您转动数据的位置。然后,将新字段包含为非透视列。

这是这样的:

SELECT
            Business_Partner,
            Items,
            ISNULL([1],0) as JanQty,
            ISNULL([2],0) as FebQty,
            ISNULL([3],0) as MarQty
          FROM
          (SELECT 
                Business_Partner,
                Items,
                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)

答案 1 :(得分:0)

如果您提供一些测试数据和您正在使用的架构,将来会更容易回答。

话虽如此,下面是我如何去做的一个例子。如果您从多个表中提取数据,那么我建议从单独的表中提取您需要的数据,然后在获取所有数据的查询周围包含类似于下面的选择。如果可能的话,我会避免旋转,因为我认为如果你已经知道会有静态列(在这种情况下是monthamount / monthquantity),它会过于复杂。

CREATE TABLE #Test
(
    BusinessPartner VARCHAR(15),
    Item VARCHAR(25),
    Price INT,
    [MONTH] int
)

INSERT INTO #Test
SELECT 'SAP', 'Software', 44, 1
UNION
SELECT 'SAP', 'Software', 51, 1
UNION
SELECT 'SAP', 'Software', 115, 2
UNION
SELECT 'SAP', 'Phones', 11, 1
UNION
SELECT 'JDC', 'Software', 21, 1
UNION
SELECT 'JDC', 'Software', 21345, 2
UNION
SELECT 'JDC', 'Software', 2346, 2
UNION
SELECT 'JDC', 'Phones', 1123, 1
UNION
SELECT 'JDC', 'Phones', 5415, 1

SELECT BusinessPartner, 
    Item, 
    Jan = SUM(CASE WHEN MONTH = 1 THEN Price ELSE 0 END),
    JanQty = SUM(CASE WHEN MONTH = 1 THEN 1 ELSE 0 END),
    Feb = SUM(CASE WHEN MONTH = 2 THEN Price ELSE 0 END),
    FebQty = SUM(CASE WHEN MONTH = 2 THEN 1 ELSE 0 END)
    FROM #Test
    GROUP BY BusinessPartner, Item
    ORDER BY BusinessPartner, Item