Sql查询返回与查询匹配的SUM和#行

时间:2022-07-05 20:11:38

Table UserData (UserID, Sales, Credits)

表UserData(UserID,Sales,Credits)

I need to return the SUM of sales, sum of credits and the # of rows returned for a given date range.

我需要返回销售额,信用额和给定日期范围内返回的行数。

Is it possible in 1 query?

在1个查询中是否可以?

2 个解决方案

#1


SELECT COUNT(*), SUM(Sales), SUM(Credits)
FROM UserData
WHERE TransactionDate BETWEEN @StartDate AND @EndDate

#2


You can also give names to the computed columns like this

您也可以像这样为计算列命名


   SELECT 
      COUNT(*) AS NumRows, SUM(Sales) AS SalesTotal, SUM(Credits) AS CreditsTotal
   FROM UserData
   WHERE TransactionDate BETWEEN @StartDate AND @EndDate

#1


SELECT COUNT(*), SUM(Sales), SUM(Credits)
FROM UserData
WHERE TransactionDate BETWEEN @StartDate AND @EndDate

#2


You can also give names to the computed columns like this

您也可以像这样为计算列命名


   SELECT 
      COUNT(*) AS NumRows, SUM(Sales) AS SalesTotal, SUM(Credits) AS CreditsTotal
   FROM UserData
   WHERE TransactionDate BETWEEN @StartDate AND @EndDate