求一条sql语句,找出余额没有变化的账户记录

tteagle 2011-12-29 08:18:41
表结构及测试数据
账号,日期,余额
001,20110101,100
002,20110101,200
003,20110101,100

001,20110102,100
002,20110102,210
003,20110102,150

001,20110103,100
002,20110103,220
003,20110103,100
.......
现在要找出某个时间段内,余额没有变化的那些账号(比如上面001账号),请大侠出手,谢了
...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
having expression主要是对group by 阶段执行后生成的虚拟表里的数据根据expression进行筛选
tteagle 2011-12-29
  • 打赏
  • 举报
回复
谢谢两位大侠了,两位大侠都是对的,第一位来的早,分数多点。
不大会用having,惭愧啊。
  • 打赏
  • 举报
回复
if OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
账号 varchar(10),
日期 varchar(10),
余额 int)
insert into tbl
select '001','20110101',100 union all
select '002','20110101',200 union all
select '003','20110101',100 union all
select '001','20110102',100 union all
select '002','20110102',210 union all
select '003','20110102',150 union all
select '001','20110103',100 union all
select '002','20110103',220 union all
select '003','20110103',100

select 账号 from tbl where 日期 between '20110101' and '20110103'
group by 账号
having MAX(余额)=MIN(余额)
/*
结果表
账号
001
*/
美到心痛 2011-12-29
  • 打赏
  • 举报
回复
改一下上面的


select 账号
from a
where 日期 between '20110101' and '20110103'
group by 账号
having max(余额)=min(余额)
美到心痛 2011-12-29
  • 打赏
  • 举报
回复

create table a(账号 varchar(10),日期 varchar(10),余额 int)
insert into a
select '001','20110101',100
union all select
'002','20110101',200
union all select
'003','20110101',100
union all select
'001','20110102',100
union all select
'002','20110102',210
union all select
'003','20110102',150
union all select
'001','20110103',100
union all select
'002','20110103',220
union all select
'003','20110103',100
select * from a

select 账号
from a
where 日期 between '2011-01-01' and '2011-01-03'
group by 账号
having max(余额)=min(余额)

/*
001
*/

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧