get sum of multiple values by months in one table

时间:2018-01-01 02:09:59

标签: mysql sql tsql

I have table with query :

   SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName, TM.terminal_name, 
   CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) / 
   TOPUP.currency_rate, 2) AS decimal(18, 2)) AS 
   Top_Up_Value                       
   FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP 
   INNER JOIN   dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id = 
   TU.terminal_user_id 
   INNER JOIN   dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id 
   INNER JOIN   dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id 
   INNER JOIN   dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id             
   INNER JOIN   dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id = 
   TU.terminal_user_id 
   INNER JOIN   dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id 
   INNER JOIN   dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id = 
   TOPUP.tu_log_id 
   LEFT OUTER JOIN  dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON 
   MA.nc_log_id = NC.nc_log_id
   WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
   and YEAR(TOPUP.tu_timestamp) = '2017'
   AND month(TOPUP.tu_timestamp) = 1
   AND TM.terminal_id = 7
   GROUP BY TOPUP.tu_log_id,DATENAME(Month,TOPUP.tu_timestamp), 
   TM.terminal_name, 
   TOPUP.tu_credit, NC.initial_bal, TOPUP.currency_rate, CU.card_type_id;

   MonthName          Terminal name         Top Up Value
  ------------------------------------------------------
    January            Terminal 1               100
    January            Terminal 1               200     
    January            Terminal 3               150
     Feb               Terminal 1               250
     Feb               Terminal 1               160
     March             Terminal 2               120
     March             Terminal 3               100

and i would like to have total sums of top up value according to months which look like this:

    MonthName       Top Up Value
  -----------------------------------
    January            450              
    February           410
    March              220
    -----
    Dec

as i am beginner in sql , i dont have idea how to do it. Really need help on these. Thanks!

3 个答案:

答案 0 :(得分:1)

 SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,  
  SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) / 
  TOPUP.currency_rate, 2) AS decimal(18, 2))) AS 
  Top_Up_Value                       
  FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP 
 INNER JOIN   dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id = 
  TU.terminal_user_id 

答案 1 :(得分:0)

按照语句查看您的分组,您发布的结果显示的列数少于查询中的列数。调整它,你会得到你的结果

答案 2 :(得分:0)

试试这个

 SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,  
   SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) / 
   TOPUP.currency_rate, 2) AS decimal(18, 2))) AS 
   Top_Up_Value                       
   FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP 
   INNER JOIN   dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id = 
   TU.terminal_user_id 
   INNER JOIN   dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id 
   INNER JOIN   dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id 
   INNER JOIN   dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id             
   INNER JOIN   dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id = 
   TU.terminal_user_id 
   INNER JOIN   dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id 
   INNER JOIN   dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id = 
   TOPUP.tu_log_id 
   LEFT OUTER JOIN  dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON 
   MA.nc_log_id = NC.nc_log_id
   WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
   and YEAR(TOPUP.tu_timestamp) = '2017'
   AND month(TOPUP.tu_timestamp) = 1
   AND TM.terminal_id = 7
   GROUP BY DATENAME(Month,TOPUP.tu_timestamp)