0
I’m having trouble assembling a select with the 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 in every gig whether they have a supply or not on the day. In case I don’t have a supply, I want the beak to appear, but come zeroed, in my select
the beak 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;
select should start with the spout table, and then use left Join with the other tables
– Rovann Linhalis
I tried, but it didn’t help.
– Diego Aquino
tries to put Where filters in Join
– Camilo Santos