带有2个条件的MSQLite查询

带有2个条件的MSQLite查询,sqlite,Sqlite,我有一个SQLite表,包含4列Id、日期、金额、详细信息和类型 类型可以是“预算”、“日”、“月” 我需要计算每个月的金额 Select substr(date,4) as A, sum(amount) as B from records_TB where NOT type like '%BUDGET' group by substr(date,4) order by substr(date,-1,7) 正确输出如下: 08-2020 14752 09-2020 20780 10-2

我有一个SQLite表,包含4列Id、日期、金额、详细信息和类型

类型可以是“预算”、“日”、“月” 我需要计算每个月的金额

Select substr(date,4) as A, sum(amount) as B 
from records_TB  
where NOT type like '%BUDGET'
group by substr(date,4) 
order by substr(date,-1,7)
正确输出如下:

08-2020 14752
09-2020 20780
10-2020 21725
11-2020 14236
12-2020 27635
01-2021 25977
02-2021 27004
03-2021 25149
...   ...
现在我想为Type=“MONTHLY”再添加一列 我试过:

 Select substr(date,4) as A, sum(amount) as B, 
       (select sum(amount)  from records_TB where type = "MONTHLY" ) fix 
 from records_TB  
 where NOT type like '%BUDGET'
 group by substr(date,4) 
 order by substr(date,-1,7)

但它不起作用。请帮助

您可能想要这样的东西

 Select substr(date,4) as dt, sum(amount) as total, 
       SUM(CASE WHEN type='MONTHLY' THEN amount ELSE 0 END) AS total_monthly,
       SUM(CASE WHEN type='DAY' THEN amount ELSE 0 END) AS total_day
 from records_TB  
 where NOT type like '%BUDGET'
 group by substr(date,4) 
 order by substr(date,-1,7)

是的,工作很好!多谢各位