使用mysql的组和
我有三张表:财务年度表、房屋明细表、消费者明细表。我需要按年度和子收入获得每个税务组的总和。我的表和查询位于此链接中: 获得结果:使用mysql的组和,mysql,Mysql,我有三张表:财务年度表、房屋明细表、消费者明细表。我需要按年度和子收入获得每个税务组的总和。我的表和查询位于此链接中: 获得结果: Name house_number address subincome financial_year gtax htax LTAX -------------------------------------------------------------------------- Bala 22 Mumbai Garbag
Name house_number address subincome financial_year gtax htax LTAX
--------------------------------------------------------------------------
Bala 22 Mumbai Garbage tax 2015-2016 200 NULL NULL
Bala 22 Mumbai Garbage tax 2016-2017 250 NULL NULL
Bala 22 Mumbai House tax 2015-2016 NULL 0 NULL
Bala 22 Mumbai House tax 2016-2017 NULL 145 NULL
Bala 22 Mumbai Light tax 2015-2016 NULL NULL 510
Bala 22 Mumbai Light tax 2016-2017 NULL NULL 200
Name house_number address gtax htax LTAX
--------------------------------------------------------
Bala 22 Mumbai 450 145 710
预期结果:
Name house_number address subincome financial_year gtax htax LTAX
--------------------------------------------------------------------------
Bala 22 Mumbai Garbage tax 2015-2016 200 NULL NULL
Bala 22 Mumbai Garbage tax 2016-2017 250 NULL NULL
Bala 22 Mumbai House tax 2015-2016 NULL 0 NULL
Bala 22 Mumbai House tax 2016-2017 NULL 145 NULL
Bala 22 Mumbai Light tax 2015-2016 NULL NULL 510
Bala 22 Mumbai Light tax 2016-2017 NULL NULL 200
Name house_number address gtax htax LTAX
--------------------------------------------------------
Bala 22 Mumbai 450 145 710
试试这个,它会给出你想要的确切结果,我只是创建一个驱动表,然后求和
SELECT name
, house_number
, address
, SUM(gtax) as gtax
, SUM(htax) as htax
, SUM(LTAX) LTAX
FROM (SELECT c.consumer_name as Name
, c.house_number
, c.address,h.subincome
, h.financial_year
, CASE WHEN h.subincome = 'Garbage tax' THEN f.garbage_tax - sum(h.rupees)END as gtax
, CASE WHEN h.subincome = 'House tax' THEN f.house_tax - sum(h.rupees) END as htax
, CASE WHEN h.subincome = 'Light tax' THEN f.light_tax - sum(h.rupees) END as LTAX
FROM house_details h
INNER JOIN financial_year f ON h.financial_year = f.year AND h.house_id = f.house_number
INNER JOIN consumer_details c ON h.house_id = c.house_number AND h.financial_year != '2017-2018'
GROUP BY h.subincome, h.financial_year) as main
GROUP BY house_number
结果是:
您正在寻找有条件的汇总注意:只有在每个会计年度的每个subincometax类型内部详细信息都有条目的情况下,此解决方案才有效
SELECT c.consumer_name as Name, c.house_number, c.address,
sum(CASE WHEN h.subincome = 'Garbage tax' THEN f.garbage_tax else 0 end) -
sum(CASE WHEN h.subincome = 'Garbage tax' THEN h.rupees else 0 END) as gtax,
sum(CASE WHEN h.subincome = 'House tax' THEN f.house_tax else 0 end) -
sum(CASE WHEN h.subincome = 'House tax' THEN h.rupees else 0 END) as htax,
sum(CASE WHEN h.subincome = 'Light tax' THEN f.light_tax else 0 end) -
sum(CASE WHEN h.subincome = 'Light tax' THEN h.rupees else 0 END) as LTAX
from house_details h
INNER JOIN financial_year f ON h.financial_year = f.year AND h.house_id = f.house_number
INNER JOIN consumer_details c ON h.house_id = c.house_number AND h.financial_year != '2017-2018'
group by c.consumer_name , c.house_number, c.address
结果
+------+--------------+---------+------+------+------+
| Name | house_number | address | gtax | htax | LTAX |
+------+--------------+---------+------+------+------+
| Bala | 22 | Mumbai | 450 | 145 | 710 |
+------+--------------+---------+------+------+------+
1 row in set (0.03 sec)
如果不能保证每个财政年度的每个子收入都有一个条目,那么解决方案就必须从应纳税表财政年度开始,在我看来,该表设计糟糕,缺乏灵活性,并强制采用次优解决方案
select c.consumer_name as Name, s.house_number, c.address,
sum(case when subincome = 'garbage tax' then taxdue else 0 end) - sum(case when subincome = 'garbage tax' then taxpaid else 0 end) as gtax,
sum(case when subincome = 'house tax' then taxdue else 0 end) - sum(case when subincome = 'house tax' then taxpaid else 0 end) as htax,
sum(case when subincome = 'light tax' then taxdue else 0 end) - sum(case when subincome = 'light tax' then taxpaid else 0 end) as ltax
from
(
SELECT F.`house_number`, F.`year`, F.`house_tax` taxdue, F.`createdAt`, F.`updatedAt`,ifnull(h.subincome,'house_tax') subincome,ifnull(H.RUPEES,0) taxpaid
FROM FINANCIAL_YEARS F
LEFT JOIN house_details H ON H.HOUSE_ID = F.HOUSE_NUMBER AND H.SUBINCOME = 'house tax' and f.year = h.financial_year
#where f.house_number = 22
union all
SELECT F.`house_number`, F.`year`, F.`light_tax`, F.`createdAt`, F.`updatedAt`,ifnull(h.subincome,'light tax'),ifnull(H.RUPEES,0)
FROM FINANCIAL_YEARS F
LEFT JOIN house_details H ON H.HOUSE_ID = F.HOUSE_NUMBER AND H.SUBINCOME = 'light tax' and f.year = h.financial_year
#where f.house_number = 2
union all
SELECT F.`house_number`, F.`year`, F.`garbage_tax`, F.`createdAt`, F.`updatedAt`,ifnull(h.subincome,'garbage tax'),ifnull(H.RUPEES,0)
FROM FINANCIAL_YEARS F
LEFT JOIN house_details H ON H.HOUSE_ID = F.HOUSE_NUMBER AND H.SUBINCOME = 'garbage tax' and f.year = h.financial_year
#where f.house_number = 2
) s
join consumer_details c on s.house_number = c.house_number
where s.year <> '2017-2018'
group by c.consumer_name , s.house_number, c.address
将结果粘贴为文本,而不是图像。有些人看不到图片。链接是指向图片而不是sqlfiddle。@P.Salmon是的,我更新了,请检查。请检查更新的答案。顺便说一句,您可能想更改一些表名和列名,使其更有意义。请您解释一下taxdue字段的用途。在unionor union all语句中,提供的字段数必须相同,taxdue是您拥有的3种税种的别名,我几乎可以使用任何字符串代替taxdue。