汇总个人明细记录的最后一笔数据,用一条语句能否实现?
消费记录表:
卡号 消费余额 次数 消费时间 本次消费额
001 100 1
001 95 2 5
001 90 3 (当前最后余额)
002 100 1
002 90 2
002 80 3 (当前最后余额)
003 50 1
003 45 2 (当前最后余额)
现要求把每个账户的最后余额做汇总,得出系统当前余额。
系统余额 = 90 + 80 + 45
能否用一条Sql语句实现 ?
select sum(消费余额) as 总消费余额
from 消费记录表 a
where not exists (select 1 from 消费记录表 where 卡号= a.卡号 and 次数 > a.次数)
select 系tablename group by 卡号)
select sum(消费余额) as 总消费余额
from
(select max(次数),卡号 ,消费余额
from 消费记录表
group by 卡号 ,消费余额)
select sum(消费余额) from
( select min(消费余额) as 消费余额 from 消费记录表group by 卡号) as b
coolingpipe(冷箫轻笛) :
select sum(消费余额) as 总消费余额
from 消费记录表 a
where not exists (select 1 from 消费记录表 where 卡号= a.卡号 and 次数 > a.次数)
能不能解释一下 ? 能查出结果,但还没验证是否正确 ,现在余额有好几十w .
KasenHOo(深渊的水影)
select sum(消费余额) as 总消费余额
from
(select max(次数),卡号 ,消费余额
from 消费记录表
group by 卡号 ,消费余额)
容易理解,但调试通不过 , ')' 附近有语法错误。
Homers() ( ) 信誉:100 Blog 2006-12-30 17:03:11 得分: 0
select sum(消费余额) from
( select min(消费余额) as 消费余额 from 消费记录表group by 卡号) as b
这应该不对的,min(消费余额) , 如果中间充值后会余额就会变大了.
select sum(消费余额) as 总消费余额
from
(select max(次数),卡号 ,消费余额
from 消费记录表
group by 卡号 ,消费余额)a
加个a就可以了你再试下..
select sum(消费余额) as 总消费余额 from(select 消费余额 from tablename where 次数 in
(select max(次数) from tablename group by 卡号))a
select sum(消费余额) as 消费余额 from
(
select a.卡号,a.消费余额,a.次数 from tb a,
(select 卡号 , max(次数) as 次数 from tb group by 卡号) b
where a.卡号 = b.卡号 and a.次数 = b.次数
) t
楼上的对的 ,但还是要高手解释一下2楼的,
select sum(消费余额) as 总消费余额
from 消费记录表 a
where not exists (select 1 from 消费记录表 where 卡号= a.卡号 and 次数 > a.次数)
这是什么意思 ? 两个结果是一样的。
一楼coolingpipe(冷箫轻笛)的回复是比较常用的方法.其中:
where not exists (select 1 from 消费记录表 where 卡号= a.卡号 and 次数 > a.次数)
是求每个卡号的最大次数那一行记录的消费余额.
求最大次数那一行类似的方法还有:
select sum(消费余额) as 总消费余额
from 消费记录表 a
where a.次数 = (select top 1 次数 from 消费记录表 where 卡号= a.卡号 ORDER BY 次数 DESC)
SELECT SUM(消费余额)
FROM table1 a
WHERE a.次数=
(SELECT 次数
FROM table1 ax
WHERE a.卡号=ax.卡号)
SELECT SUM(消费余额)
FROM table1 a
WHERE a.次数 in
(SELECT Max(次数) as 次数
FROM table1 ax
WHERE a.卡号=ax.卡号 Group by ax.卡号)
select sum(余额)
from(
select 卡号,min(余额) as 余额
from t
group by 卡号
) _t
mengmou()mengmou() ( ) 信誉:100 Blog
select sum(余额)
from(
select 卡号,min(余额) as 余额
from t
group by 卡号
) _t
如果中间有充值,余额会增大的。
每个账户中间肯定有好多次充值的 ,卡上金额不会一直递减的。
select sum(消费记录表.消费余额) from 消费记录表 join
(select 卡号,max(消费时间) as mt from 消费记录表 group by 卡号) 消费记录表1
on 消费记录表.卡号=消费记录表1.卡号 and 消费记录表.消费时间=消费记录表1.mt
if object_id('消费记录表')>0 drop table [消费记录表]
create table [消费记录表]
(
卡号 varchar(12),
消费余额 int,
次数 int,
消费时间 datetime,
本次消费额 int
)
insert into 消费记录表
select '001',100,1,getdate(),0 union all select '001',95,2,getdate(),5 union all select '001',90,3,getdate(),0 union all
select '002',100,1,getdate(),0 union all select '002',90,2,getdate(),0 union all select '002',80,3,getdate(),0 union all
select '003',50,1,getdate(),0 union all select '003',45,2,getdate(),0
--执行
select 系统余额=sum(消费余额) from 消费记录表 a where not exists(select * from 消费记录表 where 卡号=a.卡号 and 次数>a.次数)
/*
系统余额
-----------
215
(所影响的行数为 1 行)
*/
drop table [消费记录表]
--方法二:
select 系统余额=sum(消费余额) from 消费记录表 a where 次数=(select max(次数) from 消费记录表 where 卡号=a.卡号)
/*
系统余额
-----------
215
(所影响的行数为 1 行)
*/