select 表1.Deptid ,表1.deptname,表1.qb,A.monthly, 表4.cost_code cost_name,A.cost,表2.hr_number,表2.case_number,  cost*1.0/case_number as  case_jun  
from 表1,表2,表4,
(select sum(cost)cost, Deptid,cost_code,monthly
from 表3 
group by Deptid,cost_code,monthly)A
where 表1.Deptid =表2.Deptid 
  and 表2.monthly=A.monthly
   and 表1.Deptid =A.Deptid
    and 表4.Cost_name=A.Cost_namecost*1.0/case_number as  case_jun :需要的话 截断一部分数据和类型转变,
还有根据具体要求可以修改语句里的分组条件,及连接,可以是left join,或者完全相等,这里我写了  完全相等

解决方案 »

  1.   


    select 表1.Deptid ,表1.deptname,表1.qb,A.monthly, 表4.cost_code cost_name,A.cost,表2.hr_number,表2.case_number,  left(cast(round(cost*1.0/case_number ,1)as varchar(20)),4)as  case_jun  
    from 表1,表2,表4, 
    (select sum(cost)cost, Deptid,cost_code,monthly 
    from 表3 
    group by Deptid,cost_code,monthly)A 
    where 表1.Deptid =表2.Deptid 
      and 表2.monthly=A.monthly 
      and 表1.Deptid =A.Deptid 
        and 表4.Cost_name=A.Cost_name 
      

  2.   


    --测试数据
    create table 表一(deptid int ,deptname varchar(20),qb varchar(20) )
    create table 表二(deptid int ,hr_number int, qb varchar(20),monthly varchar(30),case_number int )
    create table 表三(deptid int ,cost_code varchar(20),qb varchar(20),monthly varchar(30),cost int )
    create table 表四(cost_name varchar(20) ,cost_code varchar(20))
    insert into 表一 values(001,'x档案','上海')
    insert into 表一 values(002,'x档案','上海')
    insert into 表二 values(001,73,'上海','2009年04月',54506)
    insert into 表二 values(002,49,'上海','2009年05月',50912)
    insert into 表三 values(001,'rent','上海','2009年04月',8888)
    insert into 表三 values(001,'rent','上海','2009年04月',8000)
    insert into 表三 values(002,'rent','上海','2009年05月',7777)
    insert into 表三 values(002,'rent','上海','2009年05月',6000)--查询
    select distinct a.deptid ,a.deptname,a.qb,b.hr_number,b.monthly,b.case_number,
    (select cost_code from 表四 )as cost_name,
    cost = (select sum(cost) from 表三 where deptid = a.deptid  group by deptid),
    left(((select sum(cost) from 表三 where deptid = a.deptid  group by deptid)*1.0/(select case_number from 表二 where deptid = a.deptid)) ,4)as case_jun
    from 表一 a Join 表二 b on a.deptid = b.deptid
    join 表三 c on c.deptid = a.deptid--结果
    ------------------------------------------------------------------------------------
    1 x档案 上海 73 2009年04月 54506 房租 16888 0.30
    2 x档案 上海 49 2009年05月 50912 房租 13777 0.27
      

  3.   

    yuyangyangde  查询语句没有问题,但结果会报错,“子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”请问是什么问题?谢谢~!
    ljhcy99 高手的可以得到我要的结果,非常感谢~!
    一会等yuyangyangde 后一起放分,因为《子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。》我在执行其它的一个查询时也遇到了,谢谢·
      

  4.   

    select distinct a.deptid ,a.deptname,a.qb,b.hr_number,b.monthly,b.case_number,
    (select cost_code from 表四 )as cost_name,
    (select sum(cost) from 表三 where deptid = a.deptid  group by deptid) as cost ,
    (
    case (select sum(cost) from 表三 where deptid = a.deptid  group by deptid)
     when 0 then 0 
     else cast((select sum(cost) from 表三 where deptid = a.deptid  group by deptid)*1.0/(select case_number from 表二 where deptid = a.deptid) as dec(10,2))
     end
    )  
    as case_jun
     from 表一 a Join 表二 b on a.deptid = b.deptid
    join 表三 c on c.deptid = a.deptid
      

  5.   

    你好!有出现了这个错误:子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
      

  6.   

    你好!有出现了这个错误:子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
      

  7.   


    select distinct a.deptid ,a.deptname,a.qb,b.hr_number,b.monthly,b.case_number,
    (select cost_code from 表四 )as cost_name,
    (select top 1 sum(cost) from 表三 where deptid = a.deptid  group by deptid) as cost ,
    (
    case (select top 1 sum(cost) from 表三 where deptid = a.deptid  group by deptid)
     when 0 then 0 
     else cast((select top 1 sum(cost) from 表三 where deptid = a.deptid  group by deptid)*1.0/(select top 1 case_number from 表二 where deptid = a.deptid) as dec(10,2))
     end
    )  
    as case_jun
     from 表一 a Join 表二 b on a.deptid = b.deptid
    join 表三 c on c.deptid = a.deptid这样呢?