如何在 sqlite 查询的输出中使用相同名称的列作为我在 join 的 ON 子句中进行比较的值?
我原来的问题
当我在 SQLite 中执行以下查询时,出现此错误:
查询错误:滥用聚合:sum() 无法执行语句
当我将“Loan”列的名称更改为类似“loan_amount”的名称时,错误消失了,我的查询工作正常。为什么“贷款”
出现问题?
select
t.*
, coalesce(sum(ded0.after_tax_ded_amt), 0) as "Loan"
, coalesce(sum(ded1.after_tax_ded_amt), 0) as ee_advance_amount
from totals t
left join totals as ded0
on t.ee_ssn = ded0.ee_ssn
and t.deduction_code = "Loan"
and ded0.deduction_code = "Loan"
left join totals as ded1
on t.ee_ssn = ded1.ee_ssn
and t.deduction_code = "EE Advance"
and ded1.deduction_code = "EE Advance"
group by t.ee_ssn;
中后启示
我很确定我明白了为什么会出现错误,是因为我在连接的子句中与“Loan”
进行比较吗?
如果是这样,我如何在查询输出中仍然使用“Loan”一词作为我的列名称?
My original question
When I execute the following query in SQLite, I get this error:
Query Error: misuse of aggregate: sum() Unable to execute statement
When I change the name of the "Loan"
column to something like loan_amount
the error goes away and my query works fine. Why is there a problem with "Loan"
?
select
t.*
, coalesce(sum(ded0.after_tax_ded_amt), 0) as "Loan"
, coalesce(sum(ded1.after_tax_ded_amt), 0) as ee_advance_amount
from totals t
left join totals as ded0
on t.ee_ssn = ded0.ee_ssn
and t.deduction_code = "Loan"
and ded0.deduction_code = "Loan"
left join totals as ded1
on t.ee_ssn = ded1.ee_ssn
and t.deduction_code = "EE Advance"
and ded1.deduction_code = "EE Advance"
group by t.ee_ssn;
Mid-post revelation
I'm pretty sure I figured out why I get the error, is it because I am comparing to "Loan"
in the on-clause of my joins?
If so, how can I still use the word "Loan" for my column name in the output of my query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你真正的问题是引用滥用。 SQL 中的单引号用于引用字符串文字,双引号用于引用需要区分大小写或包含奇数字符的列名和表名。 SQLite 对奇怪的语法相当宽容,因此它可能会猜测
"Loan"
的含义,但猜测不正确。试试这个:I'd guess that your real problem is quote misuse. Single quotes in SQL are for quoting string literals, double quotes are for quoting column and table names that need to be case sensitive or contain odd characters. SQLite is fairly forgiving of odd syntax so it is probably making a guess about what
"Loan"
means and guessing incorrectly. Try this: