SELECT *, sum(work_order_item.quantity) from work_order
left join work_order_item ON work_order_item.local_work_order_id = work_order.local_id
where work_order.local_id = 36
It will only ever return one record. How can I write this query without having to list all the fields in the work_order table?
Microsoft Access
Last Comment
HLRosenberger
8/22/2022 - Mon
Ryan Chong
How can I write this query without having to list all the fields in the work_order table?
remove the Select *
SELECT sum(work_order_item.quantity) from work_orderleft join work_order_item ON work_order_item.local_work_order_id = work_order.local_idwhere work_order.local_id = 36
So, if I have say 20 columns to pull back from the main table, and then 2 column from a JOINed table that I want to SUM, I have to list all the 20 columns and GROUP BY all of them?
HLRosenberger
ASKER
Also what I really want to do is have a grand total cost. I believe I know how to do this using SQL server, but not Access SQL. I have a main orders table and a related orders item, which has a quantity and cost. I want a grand total for the order. Trying to do it all in in SELECT statement, where I pull into from the main orders table, and then calculate the total cost. In SQL server, I think a Sub-query select would work.
inner join work_order_item ON work_order_item.local_work_order_id = work_order.local_id
where work_order.id = 75567
group by work_order.id, work_order.service_order_id, work_order.equipment_id, work_order.description
PatHartman
It is always better to select specific columns than to use Select * anyway but when you are aggregating, EVERY column in the Select clause must be accounted for. If you are not summing, counting, etc, then you must specifically group by.
remove the Select *
Open in new window
?or select yourfield1, youfield2, sum(work_order_item.quanti
...
with a group by of your fields
Open in new window