I'm having a hard time putting together a select with GROUP BY
. What I want to do is this:
I have three tables:
bicos
idbico, idempresa, idbomba
abastecimentos
idabastec, idbico, idempresa, valorabastecim
bomba
idbomba, idempresa
I want to bring all the nozzles regardless of whether or not they have water in the day. If I do not have a supply, I want the nozzle to appear, but it will be zeroed, in my select
the nozzle only comes with supply.
SELECT p.number
,h.number
,p.physicalnumber
,t.number AS tanknumber
,g.name AS fuelname
,SUM(CASE
WHEN d.type = 6 THEN
volume
ELSE
0
END) AS testdelivery
,FIRST(oldvolumeetot ORDER BY completeddate) AS startvolumeetot
,LAST(newvolumeetot ORDER BY completeddate) AS endvolumeetot
,SUM(CASE
WHEN d.type NOT IN (6) THEN
volume
ELSE
0
END) AS volume
,SUM(CASE
WHEN d.type NOT IN (6) THEN
VALUE
ELSE
0
END) AS VALUE
FROM hoses h
JOIN pumps AS p
ON h.stationid = p.stationid
AND h.pumpid = p.pumpid
AND h.number = p.number
JOIN tanks AS t
ON h.stationid = t.stationid
AND h.tankid = t.tankid
JOIN grades AS g
ON t.stationid = g.stationid
AND t.gradeid = g.gradeid
LEFT JOIN deliveries AS d
ON h.stationid = d.stationid
AND h.hoseid = d.hoseid
AND h.tankid = d.tankid
WHERE h.stationid = 130
AND completeddate >= '2018/05/17'
GROUP BY h.stationid
,p.number
,h.number
,p.physicalnumber
,t.number
,g.name
ORDER BY p.physicalnumber;