简体   繁体   English

两个查询分开工作。 组合时出错。 如何在 Libreoffice Base 中合并两个 SELECT 语句?

[英]Two queries work separately. Errors when combined. How do I combine two SELECT statements in Libreoffice Base?

I've reached a point with a spreadsheet where it is no longer viable to keep data in that format.我已经使用电子表格达到了以该格式保存数据不再可行的地步。 I've created a table in Libreoffice Base with the relevant information and I'm trying to put together some queries.我已经在 Libreoffice Base 中创建了一个包含相关信息的表格,并且我正在尝试将一些查询放在一起。 Unfortunately, my attempts to create a SQL query are so far being met with syntax errors.不幸的是,到目前为止,我尝试创建 SQL 查询时遇到了语法错误。 To be expected, given it's all new to me.可以预料,考虑到这对我来说都是新的。

Here's my example table:这是我的示例表:

TINYINT-A TINYINT-A TINYINT-B TINYINT-B NUMERIC-A数字-A NUMERIC-B数字-B BOOLEAN-A布尔值-A BOOLEAN-B布尔值-B
1 1 2 2 100 100 200 200 1 1 0 0
9 9 8 8 900 900 800 800 0 0 1 1

I have the following query running fine:我有以下查询运行良好:

SELECT 
  SUM("TINYINT-A") AS "First Column", 
  SUM("TINYINT-B") AS "Second Column", 
  SUM("NUMERIC-A") AS "Third Column", 
  SUM("NUMERIC-B") AS "Fourth Column"
FROM 
  "Table-A"

Output would be: Output 将是:

First Column第一栏 Second Column第二栏 Third Column第三栏 Fourth Column第四栏
10 10 10 10 1000 1000 1000 1000

I would like to add a fifth column which sums up the rows in one of the previous four column when the boolean value is equal to 1 or 0. As a separate query, I can do this:我想添加第五列,当 boolean 值等于 1 或 0 时,它总结了前四列之一中的行。作为单独的查询,我可以这样做:

SELECT 
  SUM("NUMERIC-A") AS "BOOLEAN-A-NUMERIC-A", 
  SUM("NUMERIC-B") AS "BOOLEAN-A-NUMERIC-B"
FROM 
  "Table-A" 
WHERE 
  "BOOLEAN-A" = 1

Expected output:预期 output:

BOOLEAN-A-NUMERIC-A布尔-A-数字-A BOOLEAN-A-NUMERIC-B布尔-A-数字-B
100 100 200 200

However, if I try to put the two into one query so that the output above is tacked on to the end of the first output, I get a syntax error.但是,如果我尝试将两者放在一个查询中,以便将上面的 output 附加到第一个 output 的末尾,则会出现语法错误。 This is my attempt at combining the two:这是我将两者结合起来的尝试:

SELECT 
  (
    SELECT 
      SUM("TINYINT-A") AS "First Column", 
      SUM("TINYINT-B") AS "Second Column", 
      SUM("NUMERIC-A") AS "Third Column", 
      SUM("NUMERIC-B") AS "Fourth Column"
    FROM 
      "Table-A"
  ), 
  (
    SELECT 
      SUM("NUMERIC-A") AS "BOOLEAN-A-NUMERIC-A", 
      SUM("NUMERIC-B") AS "BOOLEAN-A-NUMERIC-B"
    FROM 
      "Table-A" 
    WHERE 
      "BOOLEAN-A" = 1
  ) 
FROM 
  "Table-A"

I forgot which SO question I tried to derive the structure of the above from, but it clearly didn't work, so either I didn't understand it correctly, or I have left out a character somewhere.我忘记了我试图从中得出上述结构的哪个 SO 问题,但它显然不起作用,所以要么我没有正确理解它,要么我在某处遗漏了一个字符。

I also attempted to take the two separate queries exactly as they are, and put a new line between them with just UNION .我还尝试完全按原样处理这两个单独的查询,并用UNION在它们之间添加一条新线。 This results in an error stating that the given command is not a SELECT statement.这会导致错误指出给定命令不是 SELECT 语句。 I'm guessing because the two statements don't have the same output structure.我猜是因为这两个语句没有相同的 output 结构。

I'm not even sure if the commands are the same in Base, and whether things vary significantly enough between other databases such as MySQL.我什至不确定 Base 中的命令是否相同,以及其他数据库(例如 MySQL)之间的差异是否足够大。 I'm sure they are, and that I'm probably just doing something comparable to attempting to execute Python using HTML tags/syntax or something.我确定他们是,而且我可能只是在做一些类似于尝试使用 HTML 标签/语法或其他东西来执行 Python 的事情。

I don't know libreoffice and use Postgres, but maybe it works the same way and you can get an idea of it.我不知道 libreoffice 和使用 Postgres,但也许它的工作方式相同,您可以了解它。

Given:鉴于:

CREATE TABLE Table_A (
TINYINT_A SMALLINT,
TINYINT_B SMALLINT,
NUMERIC_A NUMERIC,
NUMERIC_B NUMERIC,
BOOLEAN_A BOOLEAN,
BOOLEAN_B BOOLEAN
);

INSERT INTO Table_A (
    TINYINT_A,
    TINYINT_B,
    NUMERIC_A,
    NUMERIC_B,
    BOOLEAN_A,
    BOOLEAN_B
)
VALUES
    (1,2,100,200,true,false),
    (9,8,900,800,false,true);

in postgres it works with subqueries like this, although I'm sure, there are better solutions:在 postgres 中,它适用于这样的子查询,尽管我敢肯定,有更好的解决方案:

SELECT 
    SUM(TINYINT_A) AS "First Column", 
    SUM(TINYINT_B) AS "Second Column", 
    SUM(NUMERIC_A) AS "Third Column", 
    SUM(NUMERIC_B) AS "Fourth Column",
    (SELECT SUM(NUMERIC_A) FROM Table_A WHERE BOOLEAN_A is true) AS BOOLEAN_A_NUMERIC_A,
    (SELECT SUM(NUMERIC_B) FROM Table_A WHERE BOOLEAN_A is true) AS BOOLEAN_A_NUMERIC_B
FROM Table_A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM