- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对Sqlite完全陌生,我有一个具有以下结构的帐户表:
创建表帐户(ID整数主键,accountName文本不为空,货币文本不为空,accountImage文本不为空);
并具有以下结构的记录表:
创建表记录(id整数主键,数量整数,类别文本不为null,描述文本,日期日期时间,recordImage文本不为null,accountId整数,外键(accountId)引用accounts(id));
Iam尝试执行此查询,以返回类别等于total_income的收入与类别等于total_expense的费用的记录的总金额,这是查询:
SELECT *, (
SELECT SUM(records.amount) AS total_income FROM records
WHERE records.category = "income", records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
), (
SELECT SUM(records.amount) AS total_expense FROM records
WHERE records.category = "expense",
records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
)
FROM accounts INNER JOIN records ON accounts.id = records.accountId;
最佳答案
您在2个WHERE子句中使用逗号而不是AND或OR。
而不是(请参阅注释 -- <<<<<<<<<< ...
):-
SELECT *, (
SELECT SUM(records.amount) AS total_income FROM records
WHERE records.category = "income", -- <<<<<<<<<< comma not AND or OR
records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
), (
SELECT SUM(records.amount) AS total_expense FROM records
WHERE records.category = "expense", -- <<<<<<<<<< comma not AND or OR
records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
)
FROM accounts INNER JOIN records ON accounts.id = records.accountId;
SELECT *, (
SELECT SUM(records.amount) FROM records -- <<<<<<<<<<< remove the AS clause
WHERE records.category = "income" AND -- <<<<<<<<<< AND instead of comma
records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
) AS total_expense, -- <<<<<<<<<< Moved to name the column in the result
(
SELECT SUM(records.amount) FROM records -- <<<<<<<<<<< remove the AS clause
WHERE records.category = "expense" AND -- <<<<<<<<<< AND instead of comma
records.date BETWEEN DATE("now", "start of month") AND DATE("now", "start of month", "+1 month", "-1 day")
) AS total_expense -- <<<<<<<<<< Moved to name the column in the result
FROM accounts INNER JOIN records ON accounts.id = records.accountId;
as you say I am getting "repeated rows" for every record associated with an account and not an accumulation. I will pass this info to a recyclerView Adapter so it would be a problem. Now I end the query with FROM accounts and I get just one row per account with their corresponding total.
-- Create testing schema and data
DROP TABLE IF EXISTS accounts;
DROP TABLE If EXISTS records;
CREATE TABLE IF NOT EXISTS records (amount INTEGER, category TEXT, date TEXT, accountId INTEGER);
CREATE TABLE IF NOT EXISTS accounts (id INTEGER PRIMARY KEY, accountname TEXT);
INSERT INTO accounts (accountname) VALUES('account1'),('account2');
INSERT INTO records (amount, category, date, accountId) VALUES
-- account 1
(300,'income','2018-12-31',1),
(25,'expense','2018-12-31',1),
(100,'income','2019-01-01',1),
(30,'expense','2019-01-01',1),
(40,'expense','2019-01-02',1),
(200,'income','2019,01-02',1),
-- account 2
(600,'income','2018-12-31',2),
(325,'expense','2018-12-31',2),
(700,'income','2019-01-01',2),
(330,'expense','2019-01-01',2),
(440,'expense','2019-01-02',2),
(5200,'income','2019,01-02',2)
;
/* The query
assumes that the records table is not a WITHOUT ROWID table
and that the id reflects the insertion order which reflects
the order in which transactions are considered to have happened
*/
SELECT *,
r.rowid AS ambiguos_recordsid, -- for demo/testing
date('now','start of month') AS startdate, -- for demo/testing
date('now','start of month', '+1 month', '-1 day') AS enddate, -- for demo/testing
r.date BETWEEN date('now','start of month') AND date('now','start of month', '+1 month', '-1 day') AS resultdate, -- for demo/testing
(
SELECT sum(amount)
FROM records
WHERE
records.rowid <= r.rowid AND category = 'income'
AND (date BETWEEN date('now','start of month') AND date('now','start of month', '+1 month', '-1 day'))
AND accountid = r.accountid
) AS rolling_income,
(
SELECT sum(amount)
FROM records
WHERE
records.rowid <= r.rowid AND category = 'expense'
AND (date BETWEEN date('now','start of month') AND date('now','start of month', '+1 month', '-1 day'))
AND accountid = r.accountid
) AS rolling_expense
FROM records AS r JOIN accounts on accountId = accounts.id
WHERE r.date BETWEEN date('now','start of month') AND date('now','start of month', '+1 month', '-1 day')
ORDER BY r.accountid, r.rowid
;
关于sqlite - SQLite复杂查询内部联接和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394002/
我有一张维护要求表和相关的每月执行频率 维护 +----------+------+ | maint_id | freq | +----------+------+ | 1 | 6
我目前有这些表: CREATE TABLE #SECURITY_TEMP (ID CHAR(30)) CREATE TABLE #SECURITY_TEMP_PRICE_HISTORY (ID CHA
我有一张维护要求表和相关的每月执行频率 维护 +----------+------+ | maint_id | freq | +----------+------+ | 1 | 6
我有一个如下所示的表格 表tbl_veh VIN Record DateChange 11223344 123A 6/24/2012 11223344
我的应用程序的数据模型 群组有很多用户 用户所属组 用户有很多打卡 冲床属于用户 冲床属于PayPeriod PayPeriod hasMany Punch 找电话 $groups = $this->
使用 Join 或 GroupJoin,是否有任何方法可以为父表和子表中的字段生成聚合值。给定一个 Orders 表和一个 OrderDetails 表,使用下面的 2 个步骤我可以从 Orders
我有一个包含用户 ID 和用户名的用户表。另一个表有一个兴趣列表,包括兴趣和姓名。第三个表是一个连接表,包含 userid 和interestid。 对于每对用户,我想获取他们共同兴趣的数量。我尝试了
假设我有下表: 表A a_name | age | country Jordan | 5 | Germany Molly | 6 | Spain Paris | 7 | France John | 7
数据源是 CSV 文件的集合,因此没有实际的数据库。这是与日本已有数十年历史的遗留系统的集成。 我有一个 c# 函数需要将 2 个 DataTables 和 2 个列名作为参数。我的函数需要对这两个数
我有一种情况要在具有数据的elasticsearch中编写搜索查询,如下所示 {id:"p1",person:{name:"name",age:"12"},relatedTO:{id:"p2"}} {
我想创建具有父子关系的文档。 我有如下数据 parent_id = null的父文档数据 { "id": 1, "work
我有以下按预期工作的 SQL: SELECT p.ACCT_ID AS [Acct], a.ACCT_NAME AS [AcctName], p.PD_NO AS [Period],
我想使用 CriteriaBuilder 在连接 2 个表的位置进行查询。在 MySQL 中,我尝试进行的查询如下所示: SELECT * FROM order LEFT JOIN item ON o
使用带有collection标签的域Service,如下所示: @Entity public class Service extends AbstractEntity { private st
我有一个解决方案,通过连接同一个表两次,但我想知道以下查询是否可以优化? select oc.name as father_occupation, o.name as mother_occupatio
我知道除了知道什么是“最喜欢的编程卡通”之外,stackoverflow 会对我有所帮助:P 这是接受的答案: Bill Karwin 感谢大家的帮助(我想给你们加倍投票) 我的查询结果是这样的(这是
我有两个表格,可以在附图中看到。 表 A 包含部门、月份和平均。 表 B 包含月份、年份、RangeStart、RangeEnd 和 颜色。 如果您查看表 B 的屏幕截图,您将看到每个月都有绿色、黄色
我有一个 Linq to sql 如下: var members=db.Members.Include(x=> x.Contact).Count(); 现在由于一些错误的数据,我的成员中的所有联系人都
我有两个表(用户和帖子),我想写出一个用户的所有帖子(以及其他内容)。我想我应该使用 JOIN 和 WHERE,但我在使用 WHERE 时遇到错误。 这是我的代码: SELECT username,
在 Hibernate 是我的持久性提供者的项目中,我可以使用“join fetch”表达式发出查询,Hibernate 将生成反射(reflect)该内容的 SQL:包含使用有效比较路径的联接表达式
我是一名优秀的程序员,十分优秀!