易截截图软件、单文件、免安装、纯绿色、仅160KB

求一SQL语句 - MS-SQL Server / 基础类

有表如下
a b c
001 收入 5000
001 费用 2000
001 合计 3000
002 收入 6000
002 费用 3000
002 合计 3000
003 收入 4000
003 费用 1000
003 合计 2000
...
合计=收入-费用
求一语句 查找出当b=收入时c字段不正确的记录


select 不正确记录=x.a from ta x join ta y on x.合计<>y.收入-y.费用 where x.b='收入'

SQL code:

select a,sum(case b when '收入' then c else -c end)
from your_table
group by a
having sum(case b when '收入' then c else -c end) <> 0



SQL code:

use PracticeDB
go
if exists (select 1 from sysobjects where name='tb_a')
drop table tb_a
go
create table tb_a (a varchar(10), b varchar(10),c numeric(10))
go
insert into tb_a
select '001', '收入' ,5000 union all
select '001', '费用' ,2000 union all
select '001', '合计' ,3000 union all
select '002', '收入' ,6000 union all
select '002', '费用' ,3000 union all
select '002', '合计' ,3000 union all
select '003', '收入' ,4000 union all
select '003', '费用' ,1000 union all
select '003', '合计' ,2000
合计=收入-费用
求一语句 查找出当b=收入时c字段不正确的记录


select * from tb_a

;with t
as
(
select a,sum(case b when '收入' then c else 0 end) [收入],


相关问答:

求一sql语句 - MS-SQL Server / 疑难问题

现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......

求一SQL - MS-SQL Server / 基础类

tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......

sql 问题 - MS-SQL Server / 基础类

需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......

sql语句问题 - MS-SQL Server / 疑难问题

现在有一个部门表dept(部门名称,部门号。。)有一个人员表emp(姓名,人员编号,职位,薪资,部门)
emp表中的内容是这样的:
a 1 工程师 3000 软件部
b 2 普通员工 2000 硬件部
c 3 工程师 4000 硬件部
d ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号