sql在两行查询结果上增加一个合计列
有这样几张表:很简单的连接,全部为内连接,如下:1:通过收支项目PK查询收支项目名称2:通过报销部门PK查询报销部门名称3:通过借款报销人PK查询姓名4:通过单据类型编码...
有这样几张表:
很简单的连接,全部为内连接,如下:
1:通过收支项目PK查询收支项目名称
2:通过报销部门PK查询报销部门名称
3:通过借款报销人PK查询姓名
4:通过单据类型编码查询单据类型名称
对应查询字段如下:
where条件如下(解释下,其实就是查询单据类型为“264x-0201和264x-0202”的单据,对应的名称为信息采购报销单和总务采购报销单,):
查询结果如下:
sql语句如下:
select er_bxzb.djbh as djbh,
er_djlx.djlxmc as djlxmc,
bd_costsubj.costname as costname,
bd_deptdoc.deptname as deptname,
bd_psndoc.psnname as psnname,
sum(er_bxzb.total) as total,
er_bxzb.djrq as djrq
from er_bxzb
inner join bd_costsubj
ON er_bxzb.szxmid = bd_costsubj.pk_costsubj
inner join bd_deptdoc
on er_bxzb.deptid = bd_deptdoc.pk_deptdoc
inner join bd_psndoc
on er_bxzb.jkbxr = bd_psndoc.pk_psndoc
inner join er_djlx
on er_bxzb.djlxbm = er_djlx.djlxbm
where er_bxzb.djlxbm in ('264X-0201', '264X-0202')
group by er_bxzb.djbh,
er_djlx.djlxmc,
bd_costsubj.costname,
bd_deptdoc.deptname,
bd_psndoc.psnname,
er_bxzb.djrq
现在需要查询结果显示成这样:
大神们教教我吧,如果描述不清楚,可以QQ沟通:2394877996 ,谢谢! 展开
很简单的连接,全部为内连接,如下:
1:通过收支项目PK查询收支项目名称
2:通过报销部门PK查询报销部门名称
3:通过借款报销人PK查询姓名
4:通过单据类型编码查询单据类型名称
对应查询字段如下:
where条件如下(解释下,其实就是查询单据类型为“264x-0201和264x-0202”的单据,对应的名称为信息采购报销单和总务采购报销单,):
查询结果如下:
sql语句如下:
select er_bxzb.djbh as djbh,
er_djlx.djlxmc as djlxmc,
bd_costsubj.costname as costname,
bd_deptdoc.deptname as deptname,
bd_psndoc.psnname as psnname,
sum(er_bxzb.total) as total,
er_bxzb.djrq as djrq
from er_bxzb
inner join bd_costsubj
ON er_bxzb.szxmid = bd_costsubj.pk_costsubj
inner join bd_deptdoc
on er_bxzb.deptid = bd_deptdoc.pk_deptdoc
inner join bd_psndoc
on er_bxzb.jkbxr = bd_psndoc.pk_psndoc
inner join er_djlx
on er_bxzb.djlxbm = er_djlx.djlxbm
where er_bxzb.djlxbm in ('264X-0201', '264X-0202')
group by er_bxzb.djbh,
er_djlx.djlxmc,
bd_costsubj.costname,
bd_deptdoc.deptname,
bd_psndoc.psnname,
er_bxzb.djrq
现在需要查询结果显示成这样:
大神们教教我吧,如果描述不清楚,可以QQ沟通:2394877996 ,谢谢! 展开
2个回答
展开全部
select sum(total) over() hj,* from (
select er_bxzb.djbh as djbh,
er_djlx.djlxmc as djlxmc,
bd_costsubj.costname as costname,
bd_deptdoc.deptname as deptname,
bd_psndoc.psnname as psnname,
sum(er_bxzb.total) as total,
er_bxzb.djrq as djrq
from er_bxzb
inner join bd_costsubj
ON er_bxzb.szxmid = bd_costsubj.pk_costsubj
inner join bd_deptdoc
on er_bxzb.deptid = bd_deptdoc.pk_deptdoc
inner join bd_psndoc
on er_bxzb.jkbxr = bd_psndoc.pk_psndoc
inner join er_djlx
on er_bxzb.djlxbm = er_djlx.djlxbm
where er_bxzb.djlxbm in ('264X-0201', '264X-0202')
group by er_bxzb.djbh,
er_djlx.djlxmc,
bd_costsubj.costname,
bd_deptdoc.deptname,
bd_psndoc.psnname,
er_bxzb.djrq
) t
就是在你的语句外再包一层,加个sum() over()就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询