日期:2014-05-18 浏览次数:20666 次
建议你提供详细的资料: 例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。 这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
------解决方案--------------------
select m.tradeDay , m.Amount + isnull(n.Amount,0) from (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) m left join (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) n on m.tradeDay = n.tradeDay 然后判断拿最大的日期及最大的值出来 select max(tradeDay) , max(Amount) from ( select m.tradeDay , m.Amount + isnull(n.Amount,0) Amount from (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) m left join (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) n on m.tradeDay = n.tradeDay ) t 最大的日期所在的记录 select m.tradeDay , m.Amount + isnull(n.Amount,0) Amount from (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) m left join (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) n on m.tradeDay = n.tradeDay order by m.tradeDay desc 最大的值出来所在的记录 select m.tradeDay , m.Amount + isnull(n.Amount,0) from (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) m left join (select convert(varchar(10),tradeDay,120) tradeDay, sum(Amount) Amount from tb group by convert(varchar(10),tradeDay,120)) n on m.tradeDay = n.tradeDay order by Amount desc