如何在 sqlite 查询的输出中使用相同名称的列作为我在 join 的 ON 子句中进行比较的值?

发布于 2024-11-27 08:29:18 字数 827 浏览 2 评论 0原文

我原来的问题

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-12-04 08:29:18

我猜你真正的问题是引用滥用。 SQL 中的单引号用于引用字符串文字,双引号用于引用需要区分大小写或包含奇数字符的列名和表名。 SQLite 对奇怪的语法相当宽容,因此它可能会猜测 "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;

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:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文