簡體   English   中英

有誰知道獲取多列 SUM 的 SQL 查詢?

[英]Does anyone know the SQL query to get a SUM for more than one column?

我正在編寫一個應用程序,人們可以在其中對雜貨店進行審核。

答案(答案是“1”或“0”)在幾列中(見屏幕截圖),所有這些答案都連接到audit_ID 有誰知道 SQL 查詢來獲取audit id = x 的多個列的SUM

數據庫截圖

查詢應返回的答案(根據屏幕截圖)應為9

您需要手動添加所有列,例如:

SELECT
    antwoord
    + antwoord2
    + antwoord3
    + antwoord4
    + antwoord5
    + antwoord6
    + antwoord7
    + antwoord8
    + antwoord9
    + antwoord10
FROM table 
WHERE audit_id = ?

但是,這里最好的方法是更改數據庫設計,使其更適合您的用例。 實際上,您需要的是一個包含 3 列的表:

audit_id
question_id
answer

前兩列組合起來是表的主鍵。 每個答案都會生成一個新行。

然后你可以去:

select sum(answer) from table where audit_id = ?

這將起作用:

SELECT * FROM Table1;
1   1   1   1   1   1
2   3   3   3   3   3

select a ,sum(b+c+d+e+f) over (partition by a ) from
Table1;
1   5
2   15

從你的表中,查詢是:

select  audit_id,sum(antwoord+antwoord2+antwoord3+antwoord4+antwoord5+
antwoord6+antwoord7+antwoord8+antwoord9+antwoord10) over (partition by 
audit_id ) from
Tablename;

只需使用+運算符。

SELECT antwoord
       + antwoord2
       ...
       + antwoord10
       FROM answers
       WHERE audit_id = x;

您可以嘗試簡單的SUM

SELECT antwoord+antwoord2+..+antwoord10 FROM [TABLE] where audi_ID=1

這個查詢求和你所有的 antwoord 值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM