查询以获取下表的总金额,平均值和数量

时间:2014-11-27 16:36:57

标签: sql stored-procedures sql-server-2008-r2

如何编写SQL查询或存储过程以获取下表?

TRANCODE | SECTYPE | QUANTITY | TRADEAMOUNT
BY          CSEU     1609         19999.87 
SL          CSEU      609         7094.85

SQL查询计算:

QTY 1609-609=1000
AVG = 19999.87/1609 =12.43
TRADEAMONUT = QTY * AV = ?  

使用数量的和:

SUM 
      ( CASE 
        WHEN  TranCode ='by' THEN  CAST(Quantity as FLOAT)
        WHEN  TranCode ='ss' THEN  CAST(Quantity as FLOAT)
        WHEN  TranCode ='sl' THEN  CAST(Quantity as FLOAT) * -1
        WHEN  TranCode ='cs' THEN  CAST(Quantity as FLOAT) * -1     
  END) 

如何在SQL查询中使用数学计算?

0 个答案:

没有答案