想要在整数列表中 return 0 IF SUM(something) is null
Want to return 0 in the integer list IF SUM(something) is null
因此,我在 Android 中使用 Java 中的 Room。
我正在像这样从数据库中返回付款列表
"SELECT IFNULL(SUM(payment), 0) FROM works_db_table WHERE account_year = :year " +
" GROUP BY account_month ORDER BY account_month
但是,return如果某月的付款为0,则没有任何意义。例如,如果第1个月的付款为200,则第2个月的付款为0,第3个月的付款为100,
它将 return [200, 100],而不是 [200, 0, 100]。
我试过使用 IFNULL,但还是一样。
@forpas 的回答解决了这个问题,但是他的代码有一些错误。下面的代码是他的代码的略微修改版本-
SELECT Ifnull(Sum(w.payment), 0)
FROM (SELECT 1 month
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9
UNION ALL
SELECT 10
UNION ALL
SELECT 11
UNION ALL
SELECT 12) m
LEFT JOIN works_db_table w
ON w.account_month = m.month
AND w.account_year = :year
GROUP BY m.month
人类很容易推断出少了一个月,但由于数据在技术上不存在,数据库不知道它应该存在。解决方案是创建“Filler”months/dates,不向他们付款。
(免责声明:我在另一个论坛上找到了这个,但我现在找不到了。虽然有很多方法可以做到这一点)
我不是很熟悉 Room,但是在 SQL 服务器 我使用这个查询:
-- Set the start date of the data
DECLARE @DateFrom date = CAST('2020-01-01' as date);
-- Set the end date of the data
DECLARE @DateTo date = CAST('2020-12-31' as date);
-- Generate a table with one entry per month
WITH DateRanges AS
(
SELECT @DateFrom AS 'DateValue'
UNION ALL
SELECT DATEADD(MONTH, 1, DateValue)
FROM DateRanges
WHERE DateValue < @DateTo
)
select
*
from
DateRanges
只需将您现有的数据联合到“DateRanges”CTE,它将填补空白
您需要一个查询 returns 所有月份 1-12 然后 LEFT
将其加入 table:
SELECT IFNULL(SUM(w.payment), 0)
FROM (
SELECT 1 month UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) m LEFT JOIN works_db_table w
ON w.account_month = m.month AND w.account_year = :year
GROUP BY m.month
因此,我在 Android 中使用 Java 中的 Room。
我正在像这样从数据库中返回付款列表
"SELECT IFNULL(SUM(payment), 0) FROM works_db_table WHERE account_year = :year " +
" GROUP BY account_month ORDER BY account_month
但是,return如果某月的付款为0,则没有任何意义。例如,如果第1个月的付款为200,则第2个月的付款为0,第3个月的付款为100, 它将 return [200, 100],而不是 [200, 0, 100]。
我试过使用 IFNULL,但还是一样。
@forpas 的回答解决了这个问题,但是他的代码有一些错误。下面的代码是他的代码的略微修改版本-
SELECT Ifnull(Sum(w.payment), 0)
FROM (SELECT 1 month
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9
UNION ALL
SELECT 10
UNION ALL
SELECT 11
UNION ALL
SELECT 12) m
LEFT JOIN works_db_table w
ON w.account_month = m.month
AND w.account_year = :year
GROUP BY m.month
人类很容易推断出少了一个月,但由于数据在技术上不存在,数据库不知道它应该存在。解决方案是创建“Filler”months/dates,不向他们付款。
(免责声明:我在另一个论坛上找到了这个,但我现在找不到了。虽然有很多方法可以做到这一点)
我不是很熟悉 Room,但是在 SQL 服务器 我使用这个查询:
-- Set the start date of the data
DECLARE @DateFrom date = CAST('2020-01-01' as date);
-- Set the end date of the data
DECLARE @DateTo date = CAST('2020-12-31' as date);
-- Generate a table with one entry per month
WITH DateRanges AS
(
SELECT @DateFrom AS 'DateValue'
UNION ALL
SELECT DATEADD(MONTH, 1, DateValue)
FROM DateRanges
WHERE DateValue < @DateTo
)
select
*
from
DateRanges
只需将您现有的数据联合到“DateRanges”CTE,它将填补空白
您需要一个查询 returns 所有月份 1-12 然后 LEFT
将其加入 table:
SELECT IFNULL(SUM(w.payment), 0)
FROM (
SELECT 1 month UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) m LEFT JOIN works_db_table w
ON w.account_month = m.month AND w.account_year = :year
GROUP BY m.month