日期:2014-05-18 浏览次数:20698 次
--有一购物卡类似表
--test
--cardNo(购物卡号),trandeType(交易类型:存款,取款),date(交易日期),money(交易金额)
--数据:001,存款,2012-1-1,500
-- 002,取款,2012-2-18,400
-- 003,存款,2012-2-2,600
-- 002,存款,2012-2-19,500
-- 002,取款,2012-1-1,400
-- 001,存款,2012-2-10,500
--问题:
-- 查询本月交易金额最高的卡号?
--结果应该是
-- 002
--请教如何求解
declare @test table
(cardNo varchar(3),trandeType varchar(4),date datetime,money int)
insert into @test
select '001','存款','2012-1-1',500 union all
select '002','取款','2012-2-18',400 union all
select '003','存款','2012-2-2',600 union all
select '002','存款','2012-2-19',500 union all
select '002','取款','2012-1-1',400 union all
select '001','存款','2012-2-10',500
select top 1 SUM(money)as 本月交易金额, cardNo from @test group by cardNo order by SUM(money) desc
本月交易金额 cardNo
----------- ------
1300 002
(1 行受影响)