select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
一个表 俩个条件 怎么合并啊??
#7
#8
是不是想要得到结果:
部门科室,人员,药费,西药费
#9
1. 结果集合并
直接中间用 union all 连接,就可以了。
2. 表2的西药费字段,合并到表一后:
;with cte1 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
GROUP BY b.BCK03, c.BCE03
),
cte2 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in
(select BBY01 from BAG1 where BBX01 in
(select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
)
select a.部门科室, a.人员, a.药费, b.西药费
from cte1 as a
left join cte2 b
on a.部门科室 = b.部门科室 and a.人员=b.人员
#10
select m.*,n.西药费 from
(select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03) as m,
(
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03) as n
where m.部门科室=n.部门科室 and m.人员=n.人员
最简单的应该是这样,你可以再做下优化。
#11
谢谢
那要是我还有个条件呢
#12
select m.*,n.西药费 from
(select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03) as m,
(
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03) as n
where m.部门科室=n.部门科室 and m.人员=n.人员
最简单的应该是这样,你可以再做下优化。
我知道了 我试试
#13
谢谢大神 学习了
#14
把两个语句中的 from 后面的语句,用 union all 合并一下,条件分开,最后来个 group by
或者把两个语句简单的 union all 一下,再 group by 一下,就是有点麻烦,估计执行计划也好不了。
#15
看了一下,那個西藥費好像就多了BBY01 in ()這個條件,這樣的話直接把上面的select into 到臨時表 再一起查這樣效率可能會高點
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
一个表 俩个条件 怎么合并啊??
#7
这种需求,有点难。。
#8
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
一个表 俩个条件 怎么合并啊??
是不是想要得到结果:
部门科室,人员,药费,西药费
#9
[/code]
一个表 俩个条件 怎么合并啊??
1. 结果集合并
直接中间用 union all 连接,就可以了。
2. 表2的西药费字段,合并到表一后:
;with cte1 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
GROUP BY b.BCK03, c.BCE03
),
cte2 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in
(select BBY01 from BAG1 where BBX01 in
(select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
)
select a.部门科室, a.人员, a.药费, b.西药费
from cte1 as a
left join cte2 b
on a.部门科室 = b.部门科室 and a.人员=b.人员
#10
select m.*,n.西药费 from
(select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03) as m,
(
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03) as n
where m.部门科室=n.部门科室 and m.人员=n.人员
最简单的应该是这样,你可以再做下优化。
#11
[/code]
一个表 俩个条件 怎么合并啊??
1. 结果集合并
直接中间用 union all 连接,就可以了。
2. 表2的西药费字段,合并到表一后:
;with cte1 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
GROUP BY b.BCK03, c.BCE03
),
cte2 as
(
select
b.BCK03 as 部门科室, c.BCE03 as 人员, SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00'
and a.vaj47<'2015-09-07 00:00:00' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in
(select BBY01 from BAG1 where BBX01 in
(select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03
)
select a.部门科室, a.人员, a.药费, b.西药费
from cte1 as a
left join cte2 b
on a.部门科室 = b.部门科室 and a.人员=b.人员
谢谢
那要是我还有个条件呢
#12
select m.*,n.西药费 from
(select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
GROUP BY b.BCK03,c.BCE03) as m,
(
select
b.BCK03 as 部门科室,
c.BCE03 as 人员,
SUM(VAJ38) as 西药费
from V_VAJ_FULL a
join BCK1 b on a.BCK01B = b.BCK01
join BCE1 c on c.BCE02 = a.BCE02B
where a.VAJ05='2' and a.vaj47>='2015-09-06 00:00:00' and a.vaj47<='2015-09-06 23:59:59' and a.BDN01='1'
and BBY01 in (select BBY01 from BBY1 where BBY01 in (select BBY01 from BAG1 where BBX01 in (select BBX01 from BBX1 where BDO01='81'))
)
GROUP BY b.BCK03,c.BCE03) as n
where m.部门科室=n.部门科室 and m.人员=n.人员
最简单的应该是这样,你可以再做下优化。
我知道了 我试试
#13
谢谢大神 学习了
#14
把两个语句中的 from 后面的语句,用 union all 合并一下,条件分开,最后来个 group by
或者把两个语句简单的 union all 一下,再 group by 一下,就是有点麻烦,估计执行计划也好不了。
#15
看了一下,那個西藥費好像就多了BBY01 in ()這個條件,這樣的話直接把上面的select into 到臨時表 再一起查這樣效率可能會高點