0

having 句で問題が発生しています。これは、次のエラーです。

tbl_quotes.taxの不明な列having clause

と私のコード:

SELECT tbl_quotes.docnumber                                      AS docNumber, 
       tbl_quotes.date                                           AS date, 
       Sum(tbl_quotesitems.price) * ( ( tax + tax2 / 100 ) + 1 ) AS total, 
       ( Sum(tbl_quotesitems.price) * ( ( tax + tax2 / 100 ) + 1 ) ) - 
           (SELECT Sum(tbl_payments.amount) 
            FROM tbl_payments 
            WHERE tbl_payments.quoteid = tbl_quotes.id
           )                                                     AS amtOwing 

FROM   tbl_quotes 
       INNER JOIN tbl_quotesitems 
               ON tbl_quotesitems.quoteid = tbl_quotes.id 
GROUP  BY tbl_quotes.id 
HAVING ( Sum(tbl_quotesitems.price) * ( ( ( tbl_quotes.tax + tbl_quotes.tax2 ) / 
                                          100 ) 
                                               + 
                                               1 ) ) < (SELECT Sum( 
                                                       tbl_payments.amount) 
                                                        FROM   tbl_payments 
                                                        WHERE 
              tbl_payments.quoteid = tbl_quotes.id) 

ありがとう

4

1 に答える 1

0

結合を使用して書き直し、コードと計算の重複を回避する可能性:
(仮定: テーブルtbl_quotesid主キーとして持っています)

SELECT q.docnumber  AS docNumber, 
       q.date       AS date, 
       qi.sum_price * ( ( q.tax + q.tax2 / 100 ) + 1 )
                    AS total, 
       qi.sum_price * ( ( q.tax + q.tax2 / 100 ) + 1 ) - p.sum_amount 
                    AS amtOwing 
FROM   tbl_quotes AS q 
       INNER JOIN 
                  ( SELECT   quoteid,
                             SUM(price) AS sum_price
                    FROM     tbl_quotesitems
                    GROUP BY quoteid
                  ) AS qi 
               ON qi.quoteid = q.id 
       INNER JOIN 
                  ( SELECT   quoteid,
                             SUM(amount) AS sum_amount
                    FROM     tbl_payments 
                    GROUP BY quoteid
                  ) AS p  
               ON p.quoteid = q.id
WHERE  qi.sum_price * ( ( ( q.tax + q.tax2 ) / 100 ) + 1 ) 
       < p.sum_amount ;
于 2013-02-01T22:34:47.230 に答える