[英]Invalid COLUMN in the select list because it is not contained in either an aggregate function or the GROUP BY clause
[英]column 'PostedVoucher.stat_flag' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
大家好,我想選擇帳戶名稱(stat_leger),從stat_leger列中的總借方(其中stat_flag列= d)和從stat_leger中的總貸方,其中stat_flag列= c,並按stat_leger分組,我需要像這樣的列
Account Name(stat_leger) | total debit | total credit
| |
acc1 | 20 | 0.00
acc2 | 30 | 15
acc3 | 40 | 20
我試過了
select DISTINCT stat_leger , sum(stat_amount) as deb
from PostedVoucher
where branch='1' and stat_flag ='c' group by stat_leger
union
select DISTINCT stat_leger , sum(stat_amount) as cred
from PostedVoucher
where branch='1' and stat_flag ='d' group by stat_leger
但它會像這樣在同一列deb中返回總借方和貸方
Account Name(stat_leger) | deb
acc1 | 0.00
acc1 | 20
acc2 | 15
acc2 | 30
acc3 | 40
acc3 | 20
當我嘗試
select stat_leger ,
( select sum(stat_amount) as deb where stat_flag ='c' )
from PostedVoucher
where branch='1' group by (stat_leger);
但返回錯誤列'PostedVoucher.stat_flag'在選擇列表中無效,因為它不包含在聚合函數或GROUP BY子句中。
我認為不需要子查詢。 您可以在主WHERE
子句中添加where條件。 檢查以下查詢:
嘗試這個:
SELECT stat_leger, SUM(stat_amount)
FROM PostedVoucher
WHERE branch='1' AND stat_flag ='c'
GROUP BY (stat_leger);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.