【问题标题】:derby SQL syntax using union使用联合的 derby SQL 语法
【发布时间】:2018-11-28 21:17:12
【问题描述】:

我正在尝试以下方法:

select sum(a.shares*a.cps) - sum(b.shares*b.cps) from
   (select * from transactions
        where usr  = 1
          and type = "C"
           or type = "S") as a
union
   (select * from transactions
                 where usr = 1
                   and type = "W"
                   or  type = "B") as b

然后回来:

Error: Syntax error: Encountered "as" at line 10, column 40.

【问题讨论】:

  • 另外,删除as aas b,因为它们没有任何作用。

标签: sql derby


【解决方案1】:

UNION 中的每个 SELECT 语句必须具有相同数量的列。 这些列还必须具有相似的数据类型。 每个 SELECT 语句中的列的顺序也必须相同。

select col1,col2 from a
union 
select 1,2 from b

但在您的查询中,两个查询中的列数不同,您的查询可能如下所示

select sum(col) from
   ( select shares*cps as col from transactions
    where usr  = 1
     and type = "C"
     or type = "S"        
        union
       select shares*cps*(-1) from transactions
        where usr = 1
        and type = "W"
        or  type = "B"
    ) t

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多