php


COALESCE(SUM(), 0) JOINED table column with GROUP BY not showing all results


I am trying to build a query that retrieves all reservations within a date timeframe and joins a payment table to calculate total payments, and few other things. The query seems to work just fine except it is leaving out reservations that have no payments. Any help would be greatly appreciated.
SELECT
reservations.reservation_id ,
properties. NAME ,
properties.address1 AS prop_address ,
reservations.last_name ,
reservations.arrival_date ,
reservations.departure_date ,
reservations.date_created ,
reservations.contract_filename ,
reservations.contract_signed ,
reservation_payments.date_of_payment ,
(
SUM(
reservations.cleaning_fee + reservations.processing_fee + reservations.pool_heat_fee + reservations.special_fees + reservations.hoa_fees + reservations.rental_fee + reservations.taxes
) - COALESCE(
SUM(
reservation_payments.amount_paid
) ,
0
)
) AS sub_total ,
COALESCE(
SUM(
reservation_payments.amount_paid
) ,
0
) AS total_paid_to_date ,
reservations.balance_due_date ,
(
SELECT
GROUP_CONCAT(
DISTINCT user_meta.first_name
ORDER BY
associated_sales_staff.display_order ASC SEPARATOR ", "
)
FROM
associated_sales_staff
JOIN user_meta ON user_meta.user_id = associated_sales_staff.user_id
WHERE
(
associated_sales_staff.reservation_id = reservations.reservation_id
)
) AS sales_agents
FROM
reservations
JOIN properties ON properties.prop_id = reservations.property_id
JOIN reservation_payments ON reservation_payments.reservation_id = reservations.reservation_id
WHERE
properties.active_for_cleaning_schedule = 1
AND reservations.arrival_date >= "2017-05-15"
AND reservations.departure_date <= "2017-05-30"
GROUP BY
reservations.reservation_id
HAVING
(sub_total >= 1)
ORDER BY
total_paid_to_date ASC

Related Links

Get Custom fields from PAGE in POSTs
how to change query string parameters with name using .htaccess?
Codeigniter - variables with tags like {title}
php mysql search engine with commas and different tables
WAMP Not showing project index page
Match specific area code followed by numbers
error_reporting(E_ALL); Says $key undefined
How to safe store password in database? [closed]
Offer file download
How to get all selected values from multiple select option?
pagination not fetching pages With where condition
which is good php_self or phpfilename.php
URL path with universal header is not working
Creating directories with regex same in url structure
Why I can`t access a protected property from its own class directly?
Php calculator printing out numbers instead of result

Categories

HOME
office365
powerbi
weblogic12c
typelite
freeradius
paypal-ipn
bots
slider
iverilog
mongoid
yql
jint
visualforce
tfs2010
clish
sslhandshakeexception
freertos
cuba-platform
l20n
opam
maven-surefire-plugin
software-packaging
gravity-forms-plugin
javax.persistence
matlab-app-designer
location-services
plantuml
universal
mongoengine
amazon-kms
grails-2.5
forms-authentication
praat
resampling
opal-framework
word-cloud
elasticsearch-aggregation
wysihtml5
android-5.0-lollipop
amazon-elastic-beanstalk
webvtt
deedle
dynamics-crm-4
zend-server
dymola
qtextedit
spring-mongo
wolfram-language
meld
powerpoint-2013
codecharge
enunciate
ajax4jsf
python-cffi
astropy
mixins
canopy
featuretoggle
css-paged-media
wso2ml
360-degrees
cache-manifest
algebraixlib
physicsjs
rfc5545
symja
wicket-1.5
tabris
angular-ui-select
xulrunner
jack
accountmanager
jomsocial
strcmp
compiler-flags
progress-db
xsd2code
indesign-server
jquery-lazyload
tridion-2011
word-frequency
image-scanner
quartz-core
sigar
data-dump
botnet
sql-parametrized-query
kernel32
jquery-1.4
ognl
nsconnection
bll
pyjamas
downgrade
cassini
aggregator
jquery-effects
subtext

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App