sql >> Databasteknik >  >> RDS >> Mysql

Använd grupp efter insida underfråga

Du kan använda nedanstående fråga för din förväntade resultatuppsättning

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



  1. Använda Oracle JDeveloper med MySQL Database Service på Oracle Cloud Platform, del 1

  2. BIGINT mysql-prestanda jämfört med INT

  3. WHERE-sats efter en UNION i SQL?

  4. Python 'int32' kan inte konverteras till en MySQL-typ