sql >> Database >  >> RDS >> Mysql

Twee geaggregeerde waarden uit een SELECT-subquery gebruiken in een enkele buitenste SELECT-query

U kunt de 2 subquery's samenvoegen tot 1:

SELECT MAX(c.cust_id) AS max_nh_cust_id,
       MAX(a.avail_balance) AS max_nh_avail_balance 
FROM account a INNER JOIN customer c 
ON a.cust_id = c.cust_id 
WHERE c.state = 'NH'

en doe als volgt mee:

SELECT a.cust_id
FROM account a 
INNER JOIN customer c ON a.cust_id = c.cust_id
INNER JOIN (
  SELECT MAX(c.cust_id) AS max_nh_cust_id,
         MAX(a.avail_balance) AS max_nh_avail_balance 
  FROM account a INNER JOIN customer c 
  ON a.cust_id = c.cust_id 
  WHERE c.state = 'NH'
) t ON c.cust_id > t.max_nh_cust_id AND a.avail_balance > t.max_nh_avail_balance
WHERE c.state = 'MA'

Bekijk de demo .
Resultaten:

> | cust_id |
> | ------: |
> |      13 |



  1. Spring Mvc Hibernate Encoding/Multi-line import sql

  2. Vreemde karakters in mysql dbase

  3. PHP-tekenreeksverschillen en dynamische beperkingen

  4. Android Sqlite-database openen in fragment