Sql 查询两个不平衡表

Sql 查询两个不平衡表,sql,tsql,Sql,Tsql,两个表之间的总和返回一个表中不需要的总和乘以另一个表中的行数 我有一个表,按日期记录实际结果,其他表包含按月份记录的计划结果 Table 1(Actual) Date Location Amount 01/01/2019 Loc1 1000 01/02/2019 Loc1 700 01/01/2019 Loc2 7500 01/02/2019 Loc2 1000 02/01/2019 Loc1 500

两个表之间的总和返回一个表中不需要的总和乘以另一个表中的行数

我有一个表,按日期记录实际结果,其他表包含按月份记录的计划结果

Table 1(Actual)

Date        Location   Amount
01/01/2019  Loc1       1000 
01/02/2019  Loc1       700
01/01/2019  Loc2       7500
01/02/2019  Loc2       1000
02/01/2019  Loc1       500

Table 2(Plan)

Year   Month       Location   Amount
2019   1           Loc1       1500
2019   1           Loc2       8000
2019   2           Loc1       800
我尝试了各种不同的连接,使用年(Table1.date)和月(Table1.date)并按分组 月(Table1.Date),但我一直遇到同样的问题,PlanAmount乘以实际表中的行数

在下面第1个月的loc1示例中,我得到

Year Month Location PlanAmount  ActualAmount
2019 1     Loc1     3000        1700
我想退回下面的表格

Year Month Location PlanAmount  ActualAmount
2019 1     Loc1     1500        1700
2019 1     Loc2     8000        8500
2019 2     Loc1     800         500
提前谢谢你的帮助
D

从日期开始获取年和月,并在联接中使用它们,大多数dbms都有年和月函数,您可以根据您的dbms使用这些函数

select year(t1.date) yr,month(t1.date) as monthofyr ,t1.Location,
sum(t1.amount) as actual_amoun,
sum(t2.amount) as planamount
from table1 t1 left join table2 t2 on
month(t1.date)= t2.Month and t1.Location=t2.Location
and year(t1.date)=t2.year
group by year(t1.date) ,month(t1.date),Location

您可以通过
完全联接
联合所有人
/
分组方式

select yyyy, mm, location,
       sum(actual_amount) as actual_amount,
       sum(plan_amount) as plan_amount
from ((select year(date) as yyyy, month(date) as mm, location,
              amount as actual_amount, 0 as plan_amount
       from actual
       group by year(date) as yyyy, month(date) as mm, location
      ) union all
      (select year, month, location,
              0 as actual_amount, amount as plan_amount
       from actual
       group by year, month, location
      )
     ) ap
group by yyyy, mm, location;

这可以确保您有行,即使在另一个表中没有匹配项。

要获得所需的结果,您需要将第一个表按日期年份、日期月份和位置分组,并需要选择列“年”、“月”、“位置”和“组中金额之和”,然后您需要加入该结果r

SELECT 
   plans.year, 
   plans.month, 
   plans.location, 
   plans.plan_amount, 
   grouped_results.actual_amount 
FROM plans 
INNER JOIN (
    SELECT 
        datepart(year, date) AS year, 
        datepart(month, date) AS month, 
        location, 
        SUM(amount) AS actual_amount 
    FROM actuals 
    GROUP BY datepart(year, date), datepart(month, date), location
) as grouped_results
ON 
    grouped_results.year = plans.year AND 
    grouped_results.month = plans.month AND 
    grouped_results.location = plans.location

我认为问题在于分组时使用的是
sum(PlanTable.Amount)
。尝试改用
max(种植量)

select 
    p.Year,
    p.Month,
    p.Location,
    sum(a.Amount) as actual_amount,
    max(p.Amount) as plan_amount
from 
    [Plan] p left join Actual a 
        on year(a.date) = p.year
        and month(a.date) = p.Month 
        and a.Location = p.Location
group by 
    p.year,
    p.month,
    p.Location

这非常有效,与我自己的一些失败尝试非常相似,似乎我在兜圈子,但很高兴知道我离得到它不远:)非常感谢您的回答,这允许从其他表扩展到其他列,我已经测试过了,非常有用!