I have two tables named Purchase and PurchaseOrder and each of the tables have the following fields:
Purchase
---------
id
date_shipped
id_po -> FK of Purchase Order
PurchaseOrder
---------
id
date_po
I need to group them based on PurchaseOrder's date_po year. So far, I've been using the date_shipped field of the Purchase table with the following query:
select extract(year from date_shipped), count(*) from purchase
group by(extract(year from date_shipped));
Now, I need to join the two tables and group them by the year of PurchaseOrder's date_po instead of Purchase's date_shipped field. I've tried various queries but can't quite achieve the results. One of the queries I tried is:
select extract(year from po.date_shipped), count(*) from purchase p
inner join purchase_order po on p.id_po = po.id
group by(extract(year from po.date_po));
Any help would be appreciated!
purchase_ordertable has no columndate_shipped. And the resulting obvious error message should be included in the question, and if that isn't the actual problem, then you need to revise your question to include the actual query and explain why it doesn't work (include sample data, expected result and actual result and why you think that is wrong).date_poand notdate_shipped. Updated the question.YEAR,extract(year from <timestamp/date>)is the correct function (which is defined in the SQL standard)