Kannan K
Kannan K

Reputation: 4461

Use group by inside subquery

I am trying to display progressive_total and cumulative_sum group by the subincome field in house_details table using mysql. I built my schema in this link

working query:

SELECT *,
       COALESCE(
                  (SELECT SUM(x.rupees)
                   FROM house_details x
                   WHERE MONTH(x.date) < t1.month), '-') AS progressive_total,

  (SELECT SUM(x.rupees)
   FROM house_details x
   WHERE MONTH(x.date) <= t1.month) AS cumulative_sum
FROM
  (SELECT MONTHNAME(t.date) AS `monthname`,
          MONTH(t.date) `month`,
          YEAR(t.date) AS YEAR,
          t.income,
          t.subincome,
          t.ssubincome,
          SUM(rupees) AS amount,
          GROUP_CONCAT(receipt_id) AS receipt_ids
   FROM house_details t
   WHERE YEAR(t.date) = YEAR(CURRENT_DATE())
   GROUP BY month(t.date),
            t.subincome
   ORDER BY t.date) t1

but this gives irrelevant cumulative_sum in the field.

I tried to use group by inside the subquery like this:

query:

SELECT *,
       COALESCE(
                  (SELECT SUM(x.rupees)
                   FROM house_details x
                   WHERE MONTH(x.date) < t1.month
                   GROUP BY x.subincome), '-') AS progressive_total,

  (SELECT SUM(x.rupees)
   FROM house_details x
   WHERE MONTH(x.date) <= t1.month
   GROUP BY x.subincome) AS cumulative_sum
FROM
  (SELECT MONTHNAME(t.date) AS `monthname`,
          MONTH(t.date) `month`,
          YEAR(t.date) AS YEAR,
          t.income,
          t.subincome,
          t.ssubincome,
          SUM(rupees) AS amount,
          GROUP_CONCAT(receipt_id) AS receipt_ids
   FROM house_details t
   WHERE YEAR(t.date) = YEAR(CURRENT_DATE())
   GROUP BY month(t.date),
            t.subincome
   ORDER BY t.date) t1;

but it shows error sub query returns more than one row.

Upvotes: 1

Views: 79

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64496

You can use below query for your expected result set

SELECT *,
COALESCE(
    (SELECT SUM(pt.rupees) FROM (
        SELECT  MONTH(`date`) `month`,
        MAX(id) id,
        SUM(rupees) rupees
        FROM house_details 
        GROUP BY `month`,subincome
    ) pt 
    WHERE CASE WHEN pt.month = t1.month THEN pt.id < t1.id ELSE pt.month < t1.month END 
), 0) AS progressive_total,
(SELECT SUM(rupees) FROM(
        SELECT  MONTH(`date`) `month`,
        MAX(id) id,
        SUM(rupees) rupees
        FROM house_details 
        GROUP BY `month`,subincome
    ) cs 
    WHERE  CASE WHEN cs.month = t1.month THEN cs.id <= t1.id ELSE cs.month <= t1.month END 
) AS cumulative_sum 
FROM (
    SELECT MONTHNAME(t.date) AS `monthname`,
    MAX(id) id,
    MONTH(t.date) `month`,
    YEAR(t.date) AS `year`,
    GROUP_CONCAT(t.income) income,
    t.subincome,
    GROUP_CONCAT(t.ssubincome) ssubincome,
    SUM(rupees) AS amount,
    GROUP_CONCAT(receipt_id) AS receipt_ids 
    FROM house_details t 
    WHERE YEAR(t.date) = YEAR(CURRENT_DATE()) 
    GROUP BY `monthname`,`month`, t.subincome
    ORDER BY `month`
) t1

Demo

Upvotes: 1

missionMan
missionMan

Reputation: 903

Subqueries that written in the previous part of "FROM" must return only one row. Obviously your query turns more than one line here.

Also the query seems a bit complicated. You can easily get the progressive sum like this:

set @csum := 0;
select id, date, rupees ,(@csum := @csum + rupees) as proggressive_sum
from house_details
order by date;

and you can group on it what you want like this:

Monthly groupped

set @csum := 0;
select month,sum_rupees, (@csum := @csum + sum_rupees) as progressive_sum_monthly
from (
    select DATE_FORMAT(date,'%Y-%m') month, sum(rupees) sum_rupees
    from house_details
    GROUP BY DATE_FORMAT(date,'%Y-%m')
    ) gg
order by 1;

Year and subincome groupped :

set @csum := 0;
select subincome, year,sum_rupees, (@csum := @csum + sum_rupees) as progressive_sum_subincome
from (
    select year(date) year, subincome, sum(rupees) sum_rupees
    from house_details
    GROUP BY subincome,year(date)
    ) gg

Upvotes: 1

Related Questions