获取SQL中的count列的总和

时间:2018-07-13 16:19:43

标签: sql tsql join count sum

我有以下查询

substring

这将返回以下内容:

enter image description here

但是我想要的只是一列和一行,应该取中间列(public String swapAO(String s) { return s.replaceFirst("^([^a]*)a(.*)o([^o]*)$", "$1o$2a$3") } )的SUM。

如何在代码后端(C#)中实现并防止这样做

2 个答案:

答案 0 :(得分:3)

您可以像这样包装查询:

SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
   your query
) s

答案 1 :(得分:1)

使用“表格表达式”,如:

select sum(Total_Coupon_To_Be_Used) from (
  SELECT dtc.coupon_type_company_name,  
    count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
    dtc.coupon_type_company_coupon_months_combinable
  FROM [dbo].[coupon_type_Company_User] dtcu
  JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
  JOIN person p ON dtcu.userID = p.userID
  WHERE coupon_type_company_coupon_is_combinable = 1 
     or coupon_type_company_has_coupon = 1 
    and dtc.companyID = 1081
    AND p.is_active = 1
  GROUP BY
    dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
    dtc.coupon_type_company_coupon_months_combinable
  ) x