简体   繁体   中英

Adding multiple columns' rows together

I'm trying to arrange data in a table. The table has the following columns: Customer Name, Amount, Day. The customer names are not distinct, the amount is an amount represented by dollars and the Day is over the course of 365 days.

I'm trying to arrange the amount paid per quarter, regardless of the customer name.

This is a homework assignment and I've tried this code

SELECT day as 'Quarter', SUM(amount) as 'Total Earnings'
FROM invoices
WHERE day BETWEEN 0 and 90
GROUP BY day

I'm running into 3 problems. I did the above code just to test that it would work for one quarter before i tried to tackle the whole year.

The first problem is that I need the day 'value' to be 'First' and I'm not sure how to do that at all.

Secondly, it is totaling the amounts, but not 0-90, it's totaling 1, 2, 3... 89, 90 . Rather than a single row with the total 'amounts' for days 0-90.

Lastly, I'm not sure how to do another sum for the other quarters (91-180, 181-270, 271-365) . I'm assuming possibly subqueries, but I'm not sure how to do that while using WHERE/BETWEEN .

My output should be something like:

Quarter | Total Earnings
-------------------------
First   | 111111111
Second  | 111111111
Third   | 111111111
Fourth  | 111111111
SELECT 'first' AS quarter, SUM(amount) AS total_earnings
FROM invoices where day between 0 AND 90
UNION ALL
SELECT 'second' AS quarter, SUM(amount) AS total_earnings
FROM invoices where day between 91 AND 180
UNION ALL
SELECT 'third' AS quarter, SUM(amount) AS total_earnings
FROM invoices where day between 181 AND 270
UNION ALL
SELECT 'fourth' AS quarter, SUM(amount) AS total_earnings
FROM invoices where day >= 271 

This will get you the expected results. The group by you were using will try to group based on day unlike on quater

You could use a CASE to find what quarter a day is in and then group by that. Something like this:

 SELECT `quarter` AS 'Quarter',
        SUM(amount) AS 'Total Earnings'
   FROM (
      SELECT CASE WHEN DAY < (365/4)
                  THEN 'First'
                  WHEN t.`day` < (365/4)*2
                  THEN 'Second'
                  WHEN t.`day` < (365/4)*3
                  THEN 'Third'
                  ELSE 'Fourth'
              END AS `quarter`,
              t.* 
         FROM `table` t
         ) t2
GROUP BY `quarter`;

You could of course replace the 365/ whatever with just a number of days or set a variable for the number of days in a year like SET @days_in_year = 365; . I'm just manually calculating to give a quick explanation of what the number is.

With a CASE statement you can evaluate the Quarter and then you can group by Quarter :

SELECT 
  case 
    when day BETWEEN 0 and 90 then 'First'
    when day BETWEEN 91 and 180 then 'Second'
    when day BETWEEN 181 and 270 then 'Third'
    else 'Fourth'
  end Quarter, 
  SUM(amount) as `Total Earnings`
FROM invoices
GROUP BY Quarter

Change the day ranges as you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM