[英]Computing Difference between two count(*)
我有兩個對同一個表進行操作並返回 avg(*) 結果的查詢。 我想選擇兩個值之間的差異,但我無法做到。 請幫忙。
我試過使用
Select t1.l1 - t2.l2
from (select avg(col) as l1 from...) as t1,
(select avg(col) as l2 from...) as t2;
但它不斷給我錯誤信息。 說缺少右括號等。
在特定列上使用AVG
而不是*
並且不需要CROSS JOIN
:
CREATE TABLE ta1(col INT);
INSERT INTO ta1 VALUES(5);
CREATE TABLE ta2(col INT);
INSERT INTO ta2 VALUES(3);
SELECT
(select avg(col) as l1 from ta1) - (select avg(col) as l2 from ta2) AS result
FROM DUAL;
另一種可能:
SELECT SUM(l1) AS result
FROM(
select avg(col) as l1 from ta1
UNION ALL
select - avg(col) as l1 from ta2
) t;
我會在 sql 中執行以下操作:
SELECT SUM(l1) - sum(l2) from
(
SELECT avg(fieldname) as l1 , 0 as l2
FROM table as t1
UNION
select 0, avg(fieldname) as l2 from table as t2
)as t1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.