【问题标题】:Select from Two tables从两个表中选择
【发布时间】:2017-06-06 14:08:21
【问题描述】:

我有两张桌子Table ATable B

我想在两个月和 year=2017 之间为 TableA 选择每个 Code 的总和列作为 A,将 TableB 选择为 B,但我想从 TableB 中选择所有行作为 C,其中 Year=2017 没有月份的过滤器。

低于我尝试过的:

SELECT  A.Annee as Year,  
        code_ville as Code,
        SUM(UniteVendu) AS Unit, 
        SUM(CAVendu) AS CA,  
        SUM(B.UniteVentePrev) AS Unit_P, 
        SUM(B.CAVentePrev) AS Ca_P,
        SUM(C.UniteVentePrev) AS Unit_PPP, 
        SUM(C.CAVentePrev) AS Ca_PPP
 FROM   TableA A 
   left join TableB B on A.code=B.codeP 
                     and  A.Annee=PP.Annee 
                     and A.mois =B.mois    
   left join TableB C on A.code=C.codeP 
                     and A.Annee=C.Annee 
 where  A.Annee = 2017   
 and    A.mois >= 1 
 and    A.mois <= 3  
 GROUP BY  R.Annee, 
           code_ville

但我得到的结果不正确。

【问题讨论】:

  • UNION 在这种情况下不起作用吗?您可以使用 UNION 通过添加额外的列来区分 TableName 来进行查看。
  • 月份和mois一样吗?您需要一个 union all 来合并两个表中的所有行。
  • 是的 mois as Month

标签: sql sql-server


【解决方案1】:

我想我在这里了解您的要求,如果我这样做了,它们实际上相当简单。最初,您需要获取YearCode 组合的列表,以便您可以从中构建最终结果数据集,joining 到您的TableATableB 中,并进行一些有条件的sum 计算.

请注意,我添加了一些额外的数据来展示我的方法的全部功能:

-- Built test data
declare @a table([Year] int,[Month] int,Code int, Unit int, CA decimal(20,2));
declare @b table([Year] int,[Month] int,CodeP int, Unit_P int, CA_P decimal(20,0));
insert into @a values (2017,1,280,1,298259.42),(2017,1,281,0,0),(2017,2,280,1,298220.99),(2017,2,281,0,0),(2017,3,282,0,0),(2017,3,280,1,298033.09),(2017,4,280,1,298000.00);
insert into @b values (2017,1,280,1,250000),(2017,1,280,3,950000),(2017,3,281,1,250000),(2017,3,282,1,250000),(2017,6,282,1,250000);

-- Derived table to get all combinations of Year and Code across both tables
with y as
(
    select [Year]
          ,Code
    from @a
    union    -- Use of UNION ensures that a unique list is returned.
    select [Year]
          ,CodeP
    from @b
)
select y.[Year]
      ,y.Code
      ,sum(case when a.[Month] between 1 and 3 then a.Unit else 0 end) as Unit
      ,sum(case when a.[Month] between 1 and 3 then a.CA else 0 end) as CA
      ,sum(case when b.[Month] between 1 and 3 then b.Unit_P else 0 end) as Unit_P
      ,sum(case when b.[Month] between 1 and 3 then b.CA_P else 0 end) as CA_P
      ,isnull(sum(a.Unit),0) as Unit_PPP
      ,isnull(sum(a.CA),0) as CA_PPP
from y  -- Then join this list onto both tables to get the totals
    left join @a a
        on(y.[Year] = a.[Year]
            and y.Code = a.Code
            )
    left join @b b
        on(y.[Year] = b.[Year]
            and y.Code = b.CodeP
            )
group by y.[Year]
        ,y.Code
order by y.[Year]
        ,y.Code;

输出:

+------+------+------+------------+--------+---------+----------+------------+
| Year | Code | Unit |     CA     | Unit_P |  CA_P   | Unit_PPP |   CA_PPP   |
+------+------+------+------------+--------+---------+----------+------------+
| 2017 |  280 |    6 | 1789027.00 |     16 | 4800000 |        8 | 2385027.00 |
| 2017 |  281 |    0 |       0.00 |      2 |  500000 |        0 |       0.00 |
| 2017 |  282 |    0 |       0.00 |      1 |  250000 |        0 |       0.00 |
+------+------+------+------------+--------+---------+----------+------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多