sql - Performing an aggregate operation (SUM) with the results of another query -


i have 2 tables:

bookings (uid integer, in_date date, paxname text, acnumber integer)  

and

accounts (uid integer, uidbooking integer, acnumber integer, amount_charged double) 

i sum charges accounts table belong subset of bookings.

the first select chooses bookings in perdiod of time...

select bookings.uid, bookings.acnumber bookings.status = 'checkedin'  , bookings.indate > '2017-07-01' 

now second select should charges of bookings , sum amounts of accounts belonging selected bookings...

select sum(amount_charged) accounts acnumber = bookings.acnumber 

so, how can results bookings uid, account number , in same row, sum of account charges... ?

thnks

using join plus group syntax or query follows:

select b.uid, b.acnumber,sum(a.amount_charged)  bookings b, accounts  b.acnumber = a.acnumber  , b.status = 'checkedin'  , b.indate > '2017-07-01' group b.uid,b.acnumber; 

in table bookings there no acnumber, maybe have exchange b.acnumber b.account.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -