链接0个或多个丢失的数据字段更新

时间:2013-05-10 09:01:51

标签: sql tsql join jointable

我有两张桌子,一张我的办公室名单和一份分配给办公室的收入清单

create table #Income(City varchar(50),Office varchar(50),YearsBudget money)
insert #Income
select 'London', null,     5000 UNION
select 'Paris',  null,     6000 UNION
select null,    'Sales',   7000 UNION
select 'London','Support',10000

create table #Offices(City varchar(50),Office varchar(50),Ratio float)
insert #Offices
select 'London','Research Lab'     ,.15 UNION
select 'London','Customer Services',.45 UNION
select 'London','Sales'            ,.05 UNION
select 'London','Admin'            ,.19 UNION
select 'London','Support'          ,.17 UNION
select 'Paris' ,'Sales'            ,.15 UNION
select 'Paris' ,'Admin'            ,.45 UNION
select 'Paris' ,'Support'          ,.05 UNION
select 'Madrid','Sales'            ,.45 UNION
select 'Madrid','Research Lab'     ,.25 

例如,在#Income的一行数据中,我有5000英镑,我必须分配给所有伦敦办事处,在另一行我有6000英镑,我需要分配给所有已知的巴黎办事处

这可以在以下SQL

中实现
select o.City,o.Office, convert(money,i.YearsBudget/DATA.RSum*o.Ratio) as ThisYearsBudget
from #Income as i
Left Join #Offices as O
on i.City=o.City
Left join (select child.City,SUM(child.Ratio) as RSum from #Offices as child group by child.City ) DATA
ON i.city=DATA.City 
where i.Office is null

为了向所有销售办事处分配7000英镑,向伦敦支持办公室分配10000英镑,需要另外2个选择陈述和另一个选择陈述,以对3个分摊视图的结果进行求和和分组。我可以在一个简单的视图中实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

示例数据

create table Income(City varchar(50),Office varchar(50),YearsBudget money)
insert Income
select 'London', null,     5000 UNION
select 'Paris',  null,     6000 UNION
select null,    'Sales',   7000 UNION
select 'London','Support',10000;

create table Offices(City varchar(50),Office varchar(50),Ratio float)
insert Offices
select 'London','Research Lab'     ,.15 UNION
select 'London','Customer Services',.45 UNION
select 'London','Sales'            ,.05 UNION
select 'London','Admin'            ,.19 UNION
select 'London','Support'          ,.17 UNION
select 'Paris' ,'Sales'            ,.15 UNION
select 'Paris' ,'Admin'            ,.45 UNION
select 'Paris' ,'Support'          ,.05 UNION
select 'Madrid','Sales'            ,.45 UNION
select 'Madrid','Research Lab'     ,.25;

<强>查询

   select o.City,
          o.Office,
          ThisYearsBudget = SUM(convert(money,sq.YearsBudget*o.Ratio/sq.totalratio))
     from Offices o
     join (
          select i.city, i.office, i.yearsbudget, totalratio = sum(o.ratio)
            from income i
            join offices o on isnull(i.city,o.city)=o.city
                          and isnull(i.office,o.office)=o.office
        group by i.city, i.office, i.yearsbudget
          ) sq on isnull(sq.city,o.city)=o.city
              and isnull(sq.office,o.office)=o.office
 group by grouping sets ((o.City, o.Office)
                         --,() -- uncomment this line to see grand total
                        )
 order by City, Office;

<强>结果:

|   CITY |            OFFICE | THISYEARSBUDGET |
------------------------------------------------
| London |             Admin |        940.5941 |
| London | Customer Services |       2227.7228 |
| London |      Research Lab |        742.5743 |
| London |             Sales |        785.9863 |
| London |           Support |      10841.5842 |
| Madrid |             Sales |       4846.1538 |
|  Paris |             Admin |       4153.8462 |
|  Paris |             Sales |            3000 |
|  Paris |           Support |        461.5385 |

SQL Fiddle Demo