I have created a summary view and using following query to get results
SELECT captain.Name AS Name,
rides.date AS date,
rides.tr AS tr,
rides.cr AS cr,
rides.cg AS cg,
rides.p AS p,
rides.oe AS oe,
(((rides.cr + rides.cg) - (rides.p +
rides.oe)) - rides.tih) AS short,
rides.tih AS tih,
ct.tr AS tr,
ct.ca AS ca
FROM (rides
JOIN captain ON captain.captainID = rides.captainID)
JOIN ct ON captain.captainID = ct.captainID
output like this
date tr cr cg p oe short tih tr ca
---------------------------------------------------
12/6/2017 20 10 10 2 3 4 43 1 88
13/6/2017 10 9 9 2 3 1 56 1 88
14/6/2017 20 10 10 2 3 4 43 1 88
15/6/2017 10 9 9 2 3 1 56 1 88
16/6/2017 20 10 10 2 3 4 43 1 88
17/6/2017 10 9 9 2 3 1 56 1 88
But it should be
date tr cr cg p oe short tih tr ca
---------------------------------------------------
12/6/2017 20 10 10 2 3 4 43 0 0
13/6/2017 10 9 9 2 3 1 56 0 0
14/6/2017 20 10 10 2 3 4 43 0 0
15/6/2017 10 9 9 2 3 1 56 0 0
16/6/2017 20 10 10 2 3 4 43 1 88
17/6/2017 10 9 9 2 3 1 56 1 88
because in ct table I have only two records for 16 and 17 June. How to force to show 0 when no records available.
Hope that I have explained properly what i need. Thanks in advance.
Use LEFT JOIN
(left outer join) on the last line.
SELECT captain.Name AS Name,
rides.date AS date,
rides.tr AS tr,
rides.cr AS cr,
rides.cg AS cg,
rides.p AS p,
rides.oe AS oe,
(((rides.cr + rides.cg) - (rides.p +
rides.oe)) - rides.tih) AS short,
rides.tih AS tih,
ct.tr AS tr,
ct.ca AS ca
FROM (rides
JOIN captain ON captain.captainID = rides.captainID)
LEFT JOIN ct ON (captain.captainID = ct.captainID and rides.date=ct.date)
Join on date
as well with ct table.
Use case when
to substitute blank values to 0
Try this:-
SELECT captain.Name AS Name,
rides.date AS date,
rides.tr AS tr,
rides.cr AS cr,
rides.cg AS cg,
rides.p AS p,
rides.oe AS oe,
(((rides.cr + rides.cg) - (rides.p +
rides.oe)) - rides.tih) AS short,
rides.tih AS tih,
case when ct.tr is NULL and ct.tr=' ' then 0 else ct.tr END AS tr,
case when ct.ca is NULL and ct.ca=' ' then 0 else ct.ca END AS ca,
FROM (rides
JOIN captain ON captain.captainID = rides.captainID)
LEFT JOIN ct ON rides.captainID = ct.captainID and rides.date=ct.date
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.