日期:2014-05-17 浏览次数:20762 次
select FDate,FContract,FBuySell,sum(FNumber) FNumber,FPrice,sum(FFee)FFee, sum(FProfitLoss) FProfitLoss,FCloseDate,FClosePrice from ( select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , NULL FCloseDate,0.00 FClosePrice ,0.00 FCloseNumber ,0.00 FCloseFee ,0 FCloseProfitLoss union select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , '2012-07-03' FCloseDate,14735.00 FClosePrice ,1.00 FCloseNumber ,10.00 FCloseFee ,175 FCloseProfitLoss ) t group by FDate,FContract,FBuySell,FPrice,FCloseDate,FClosePrice 数据怎么合并成一条 2012-07-03 ZN1210 买 5.00 14700.00 40.00 -500.00 NULL 0.00 0.00 0.00 0 2012-07-03 ZN1210 买 5.00 14700.00 40.00 -500.00 2012-07-03 14735.00 1.00 10.00 175 ==》》 2012-07-03 ZN1210 买 10.00 14700.00 80.00 -1000.00 2012-07-03 14735.00 1.00 10.00 175
--你后面几列怎么来的? select FDate,FContract,FBuySell,sum(FNumber) FNumber,max(FPrice),sum(FFee)FFee, sum(FProfitLoss) FProfitLoss,max(FCloseDate),max(FClosePrice) from ( select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , NULL FCloseDate,0.00 FClosePrice ,0.00 FCloseNumber ,0.00 FCloseFee ,0 FCloseProfitLoss union select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , '2012-07-03' FCloseDate,14735.00 FClosePrice ,1.00 FCloseNumber ,10.00 FCloseFee ,175 FCloseProfitLoss ) t group by FDate,FContract,FBuySell
------解决方案--------------------
--问题还在于你怎么处理这些不同的列,从需求着手解决这个问题更实际 select FDate,FContract,FBuySell,sum(FNumber) FNumber,FPrice,sum(FFee)FFee, sum(FProfitLoss) FProfitLoss,max(FCloseDate) FCloseDate, max(FClosePrice)FClosePrice from ( select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , NULL FCloseDate,0.00 FClosePrice ,0.00 FCloseNumber ,0.00 FCloseFee ,0 FCloseProfitLoss union select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , '2012-07-03' FCloseDate,14735.00 FClosePrice ,1.00 FCloseNumber ,10.00 FCloseFee ,175 FCloseProfitLoss ) t group by FDate,FContract,FBuySell,FPrice
------解决方案--------------------
-->合并前
2012-07-03 ZN1210 买 5.00 14700.00 40.00 -500.00 NULL 0.00 0.00 0.00 0
2012-07-03 ZN1210 买 5.00 14700.00 40.00 -500.00 2012-07-03 14735.00 1.00 10.00 175
-->合并后
2012-07-03 ZN1210 买 10.00 14700.00 80.00 -1000.00 2012-07-03 14735.00 1.00 10.00 175
标红处的结果集应该有错吧,应该是29400.00才对啊
--try --把结果集插入到临时表#tb select FDate,FContract,FBuySell,sum(FNumber) FNumber,FPrice,sum(FFee)FFee, sum(FProfitLoss) FProfitLoss,FCloseDate,FClosePrice into #tb from ( select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , NULL FCloseDate,0.00 FClosePrice ,0.00 FCloseNumber ,0.00 FCloseFee ,0 FCloseProfitLoss union all select '2012-07-03' FDate ,'ZN1210' FContract,'买' FBuySell,5.00 FNumber,14700.00 FPrice,40.00 FFee,-500.00 FProfitLoss , '2012-07-03' FCloseDate,14735.00 FClosePrice ,1.00 FCloseNumber ,10.00 FCloseFee ,175 FCloseProfitLoss ) t group by FDate,FContract,FBuySell,FPrice,FCloseDate,FClosePrice -- select * from #tb --合并数据 s