Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sqlite查询的输出中,如何将相同的列名称用作比较联接';什么是条款?_Sqlite_Sum_Aggregate_Misuse - Fatal编程技术网

在sqlite查询的输出中,如何将相同的列名称用作比较联接';什么是条款?

在sqlite查询的输出中,如何将相同的列名称用作比较联接';什么是条款?,sqlite,sum,aggregate,misuse,Sqlite,Sum,Aggregate,Misuse,我的原始问题 在SQLite中执行以下查询时,出现以下错误: 查询错误:滥用聚合:sum()无法执行语句 当我将“Loan”列的名称更改为类似于Loan\u amount的名称时,错误消失了,我的查询工作正常。为什么“贷款”有问题 中后期披露 我很确定我找到了出错的原因,是不是因为我在与连接的on子句中的“Loan”进行比较 如果是这样的话,我怎么还能在查询的输出中使用“Loan”这个词作为我的列名呢?我想你真正的问题是引用错误。SQL中的单引号用于引用字符串文字,双引号用于引用需要区分大小写

我的原始问题

在SQLite中执行以下查询时,出现以下错误:

查询错误:滥用聚合:sum()无法执行语句

当我将
“Loan”
列的名称更改为类似于
Loan\u amount
的名称时,错误消失了,我的查询工作正常。为什么“贷款”有问题


中后期披露

我很确定我找到了出错的原因,是不是因为我在与连接的on子句中的
“Loan”
进行比较


如果是这样的话,我怎么还能在查询的输出中使用“Loan”这个词作为我的列名呢?

我想你真正的问题是引用错误。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;
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;