I have this query that returns how many days a driver worked based on driver number. The way I check for days worked is just to count distinct order_date with their driver number on it. Let's call it days worked:
SELECT driver_no, count(distinct order_date)
FROM orders o INNER JOIN order_settlements os ON o.control_no =
os.control_no
WHERE os.company_no = '001' and o.service_type not in (17, 30, 31, 34, 35,
90, 94, 96, 97, 98, 99) and customer_reference != 'PARCEL ADJUSTMENT' and
order_date between (date '2017-6-11' - integer '7') and '2017-6-11' and
posting_status <> '9' and settlement_period_end_date is null
GROUP BY driver_no
And I have this query that calculates how much the driver earned, how much he delivered, etc. Let's call it main:
SELECT Driver_Number, Driver_Name, Branch, Driver_Type, sum(Revenue) AS Revenue, sum(Booking) as Booking, CASE WHEN round(sum(Support_Pay * Settlement_Per/100), 2) != 0 THEN round(sum(Support_Pay * Settlement_Per/100), 2) END as Support_Pay, round(sum(fuel * Settlement_Per/100), 2) as Fuel, round(sum(Booking * Settlement_Per/100), 2) as Settlement, sum(Stops) As Stops, sum(Pieces) As Pieces
FROM
( SELECT os.driver_no as Driver_Number, d.driver_name as Driver_Name, d.report_sort_key as Branch, (CASE WHEN d.driver_type = '0' THEN 'Contractor' WHEN d.driver_type = '1' THEN 'Employee' END) as Driver_Type,
sum(o.rate_bucket1+o.rate_bucket2+o.rate_bucket3+o.rate_bucket4+o.rate_bucket5+o.rate_bucket6+
o.rate_bucket7+o.rate_bucket8+o.rate_bucket9+o.rate_bucket10+o.rate_bucket11) as Revenue,
sum(os.charge1+os.charge2+os.charge3+os.charge4+os.charge5+os.charge6) as Booking, CASE WHEN (o.service_type = '35') THEN sum(os.charge1+os.charge2+os.charge3+os.charge4+os.charge5+os.charge6) END AS Support_Pay, CASE WHEN (o.service_type = '34') THEN sum(os.charge1+os.charge2+os.charge3+os.charge4+os.charge5+os.charge6) END AS Fuel,
os.settlement_percent as Settlement_Per, CASE WHEN (o.service_type != '17' or o.service_type != '30' or o.service_type != '31' or o.service_type != '34' or o.service_type != '35' or o.service_type != '90' or o.service_type != '94' or o.service_type != '96' or o.service_type != '97' or o.service_type != '98' or o.service_type != '99') THEN count(os.control_no) END as Stops, CASE WHEN (o.service_type != '17' or o.service_type != '30' or o.service_type != '31' or o.service_type != '34' or o.service_type != '35' or o.service_type != '90' or o.service_type != '94' or o.service_type != '96' or o.service_type != '97' or o.service_type != '98' or o.service_type != '99') THEN sum(o.pieces) END as Pieces
FROM
orders o INNER JOIN order_settlements os ON o.control_no = os.control_no INNER JOIN drivers d ON os.driver_no = d.driver_no
WHERE
d.company_no = '001' and
order_date BETWEEN '2017-4-9' AND '2017-6-11' AND
os.company_no = '001' and o.company_no = '001' AND posting_status <> '9' AND
settlement_period_end_date is NULL AND os.driver_no is not null and os.driver_no !=0 and d.driver_no between '1' and '7999'
GROUP BY
o.service_type, order_date, Settlement_Per, o.customer_no, os.driver_no, d.driver_name, d.driver_type, d.report_sort_key) Sub
GROUP BY
Branch, Driver_Number, Driver_Name, Driver_Type
ORDER BY
Driver_Number
Now I want to put the days worked as a column in the main query, but I need to keep the date ranges the same (days worked should only look back at the last week, whereas the main query needs to look back for a couple months for any orders that got entered retroactively).
I tried putting the days worked query into a "CASE WHEN" statement at the end of the main query, and it returned incorrect data (I think it was excluding any days where those service types registered instead of just passing over them). I tried putting an inner select statement at the end of the Main query where it would match driver_no from days_worked to main query and it took forever to run. I tried creating two different queries in the main "FROM" statement, one looking at the Sub and one looking at days_worked, and it returned way too many records and not how I wanted at all.
What's the best way to get this information returned into one query? This is on Postgresql 8.1 by the way. Thanks for all your help.