求一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) [收入],
相关问答:
可能因为工作的原因 接触数据库这块比较少,之前都是做程序这块,数据库这块都有专门的人来做 分工都很明细 所以对数据库这一块完全不了解。前段时间 去面试了几家公司 几乎都是在数据库这块挂掉的 连个简单的SQ ......
sql的软件在哪里可以下啊!在网上找了蛮多都用不了啊
随便搞一D版吧,
迅雷第一个就可以用
2000,2005都这样
http://119.147.41.16/down?cid=0698C2D64D7D637D90A6D2482298E6717D4F15CD&t=2&fmt=-1 ......
字段1,字段2.....字段N,Status,ParentID
1,Name1....test1,1,99
1,Name1....test1,3,99
1,Name2....test2,1,101
1,Name2....test2,3,101
1,Name3....test3,2,101
1,Name1....test1,4,101
想要的结果是:
1,Na ......
有这样一条SQL
Select Get_Costtaxrate(col1), Get_Tcostvalue(col1) from a
其中Get_Costtaxrate、Get_Tcostvalue都是函数,这两个函数里面都是查找一个大表,Get_Tcostvalue还需要调用Get_C ......
A表 有两个字段
id 唯一数字域
InfoTxt text 类型
我现在要把 id 不是14 的所有 InfoTxt字段 文本后面 都加上 'aaa'
按下面执行下来 只有表最后一行加上了 'aaa' ......