如何得出这样的总计?

时间:2021-11-27 02:18:13
已知:
---------------------检查费
select * from(
select top 100 percent

a.chk_item,CAST(null AS char) as b, '次' as c,cast (sum(a.chk_price)/b.fee  as int) as 数

量,b.fee as 单价,sum(a.chk_price) as 合计 from t_hospital_check a, t_patient_check_fee b 

where a.chk_id='1121' and a.chk_item=b.chk_name group by a.chk_item,b.fee

)

t


union all
---------------------检查合计
select * from(

select top 100 percent

'检查费合计' as a,CAST(null AS char) as b,CAST(null AS char) as c,CAST(null AS int) as 

d,CAST(null AS money) as e,sum(a.chk_price) as 合计 from t_hospital_check a, 

t_patient_check_fee b where a.chk_id='1121' and a.chk_item=b.chk_name


)t

union all
---------------------手术费
select * from(
select top 100 percent

a.operation,CAST(null AS char) as b, '次' as c,cast(sum(b.fee)/a.fee as int ) as 数

量,a.fee,sum(b.fee) as 手术费 from operation a,shoushufei b  where a.operation=b.name and 

b.hosp_id='1121' and b.fee<>0 and b.riqi between '2004-10-05'and '2004-12-06' group by 

a.operation,a.fee

)t


union all
---------------------手术费合计
select * from(

select top 100 percent

'手术费合计' as a,CAST(null AS char) as b,CAST(null AS char) as c,CAST(null AS int) as 

d,CAST(null AS money) as e,sum(b.fee) as 合计 from operation a,shoushufei b  where 

a.operation=b.name and b.hosp_id='1121' and b.fee<>0 and b.riqi between '2004-10-

05'and '2004-12-06' 


)t



输出结果:
--------------------------------------------------------------
B超脏器          NULL 次 3 15.0000 45.0000
检查费合计 NULL NULL NULL NULL 45.0000

基础麻醉          NULL 次 12 4.0000 48.0000
局麻          NULL 次 1 8.0000 8.0000
手术费合计 NULL NULL NULL NULL 56.0000

--------------------------------------------------------------
现在如何得出如下总计:

总计=检查费合计(45.0000)+手术费合计(56.0000)=101.000
而不是总计=45+45+48+8+56=202.000(这个用VB里的DataReport之Function函数时统计时=原来的2倍)
所以我想能不能只算出这个统计=检查费合计(45.0000)+手术费合计(56.0000)=101.000用SQL和其他方法都可以



--------------------------------------------------------------

6 个解决方案

#1


能不能把问题说的简单点啊?

#2


看的头晕

#3


study

#4


写个麻烦的
用UNION ALL
select 
  '总计' as a
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum([合计]) as 合计 
from(
select 
  '检查费合计' as a,
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum(a.chk_price) as 合计 
from 
  t_hospital_check a, 
  t_patient_check_fee b   
where 
  a.chk_id='1121' 
  and a.chk_item=b.chk_name
union all
select 
  '手术费合计' as a,
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum(b.fee) as 合计 
from 
  operation a,shoushufei b  
where 
  a.operation=b.name 
  and b.hosp_id='1121' 
  and b.fee<>0 
  and b.riqi between '2004-10-05'and '2004-12-06' 
)

#5


--------简单一点

select a.A,a.B,Null as c, a.C as d from test1 a  where account='geonado' 

union all

select  '合计1' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(a.C) as d  from test1 a where account='geonado' 

union all

select b.D,b.E,b.G,b.H  from  test2 b where account='geonado'

union all

select  '合计2' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(H) as d
  
from test2 where account='geonado' 
----------------------------------------------------------------------输出结果:

aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000

------------------------------------------------------------------想要输出的结果:

aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000
总计     NULL     NULL      180.000(就是说此时合计=合计1+合计2,而不是合计=2*(合计1+合计)2)

#6


我自己这样来搞可以实现,总觉得很麻烦,看看大家有没有更简单的方法呢


select a.A,a.B,Null as c, a.C as d from test1 a  where account='geonado' 

union all

select  '合计1' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(a.C) as d  from test1 a where account='geonado' 

union all

select b.D,b.E,b.G,b.H  from  test2 b where account='geonado'

union all

select  '合计2' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(H) as d
  
from test2 where account='geonado' 


-------------在原来的SQL语句上加拉下面的代码---------------------------------
union all

select  '合计' as a,CAST(null AS char) as b,CAST(null AS char) as c,(
select sum(d) from (select sum(C) as d
from test1  where account='geonado' 
union all
select  sum(H) as d
from test2  where account='geonado')  a ) as d

下面是正确的输出结果:
----------------------------------------------------------------------------
aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000
合计 NULL NULL 180.0000
-----------------------------------------------------------------------------

#1


能不能把问题说的简单点啊?

#2


看的头晕

#3


study

#4


写个麻烦的
用UNION ALL
select 
  '总计' as a
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum([合计]) as 合计 
from(
select 
  '检查费合计' as a,
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum(a.chk_price) as 合计 
from 
  t_hospital_check a, 
  t_patient_check_fee b   
where 
  a.chk_id='1121' 
  and a.chk_item=b.chk_name
union all
select 
  '手术费合计' as a,
   cast(null as char) as b,
   cast(null as char) as c,
   cast(null as int) as d,
   cast(null as money) as e,
   sum(b.fee) as 合计 
from 
  operation a,shoushufei b  
where 
  a.operation=b.name 
  and b.hosp_id='1121' 
  and b.fee<>0 
  and b.riqi between '2004-10-05'and '2004-12-06' 
)

#5


--------简单一点

select a.A,a.B,Null as c, a.C as d from test1 a  where account='geonado' 

union all

select  '合计1' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(a.C) as d  from test1 a where account='geonado' 

union all

select b.D,b.E,b.G,b.H  from  test2 b where account='geonado'

union all

select  '合计2' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(H) as d
  
from test2 where account='geonado' 
----------------------------------------------------------------------输出结果:

aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000

------------------------------------------------------------------想要输出的结果:

aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000
总计     NULL     NULL      180.000(就是说此时合计=合计1+合计2,而不是合计=2*(合计1+合计)2)

#6


我自己这样来搞可以实现,总觉得很麻烦,看看大家有没有更简单的方法呢


select a.A,a.B,Null as c, a.C as d from test1 a  where account='geonado' 

union all

select  '合计1' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(a.C) as d  from test1 a where account='geonado' 

union all

select b.D,b.E,b.G,b.H  from  test2 b where account='geonado'

union all

select  '合计2' as a,CAST(null AS char) as b,CAST(null AS char) as c,sum(H) as d
  
from test2 where account='geonado' 


-------------在原来的SQL语句上加拉下面的代码---------------------------------
union all

select  '合计' as a,CAST(null AS char) as b,CAST(null AS char) as c,(
select sum(d) from (select sum(C) as d
from test1  where account='geonado' 
union all
select  sum(H) as d
from test2  where account='geonado')  a ) as d

下面是正确的输出结果:
----------------------------------------------------------------------------
aa bb NULL 100.0000
aa1 bb1 NULL 24.0000
aa2 bb2 NULL 26.0000
合计1 NULL NULL 150.0000
dd1 ee1 gg1 10.0000
dd2 ee2 gg2 20.0000
合计2 NULL NULL 30.0000
合计 NULL NULL 180.0000
-----------------------------------------------------------------------------