Sql query to return SUM and # rows matching query

UserData table (UserID, Sales, Credits)

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

Is this possible in 1 request?

+1


a source to share


2 answers


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

      



+6


a source


You can also provide names for computed columns such as




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

      

0


a source







All Articles