简体   繁体   中英

How to Select columns from tow different table(A,B) where sum(column tableA) less then columntableB

I can select price where sum(tax) less than price, I am executing the Query but I am getting an error

select    b.price
from tableA a
inner join tableB b on b.idprice = a.idprice where sum(tax)<price
group by a.idprice,b.price

so something like this:

SELECT t.*,s.*
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
           FROM TableB
           GROUP BY idprice) s
ON(t.idprice = s.idprice and t.price < s.sum_tax)

Or if price table should be summed too :

SELECT t.idprice,sum(t.price),max(s.sum_tax)
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
           FROM TableB
           GROUP BY idprice) s
ON(t.idprice = s.idprice)
GROUP BY t.idprice
having sum(t.price) < max(s.sum_tax)

I think your original approach will work by using a having clause rather than a where :

select b.price
from tableA a inner join
     tableB b
     on b.idprice = a.idprice
group by a.idprice, b.price
having sum(tax) < b.price;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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