Having a problem adding a group-by selection to an existing query.
This works for me:
[code]
$q = “select a.wine_num, a.date_posted, a.activity, a.my_comments, a.bottle_cnt,
w.label, b.activity_desc, b.activity_abbrev
from mywines_activity a, mywines w, mywines_activity_codes b
where a.wine_num = w.wine_num and
a.activity = b.activity_code
order by a.date_posted desc, a.wine_num”;
Basically table 1 is a table of activity recs, table b is a lookup table, w is the main inventory table
and what the above is doing is getting all the activity records and showing their number, name, their activity recs and the activity description in wine_number order.
What I am trying to do is to sequence the output so that the wines with the most recent activity show up first, but for each wine the activities will show in their date order. So I tried to join a grouped-by query to get the most recent date for each of my wines. Here is the new query:
[code]
$q = “select a.wine_num, a.date_posted, a.activity, a.my_comments, a.bottle_cnt,
w.label, b.activity_desc, b.activity_abbrev, x.most_recent
from mywines_activity a, mywines w, mywines_activity_codes b
left outer join (select wine_num, max(date_posted) as most_recent
from mywines_activity
group by wine_num) x
on x.wine_num = a.wine_num
where a.wine_num = w.wine_num and
a.activity = b.activity_code
order by x.most_recent desc, a.wine_num, a.date_posted desc”;
As you can see I have a sub-query to select each wine num’s most recent date and produce a single record from the activity recs for each wine_num to be joined to the primary query in order to add the ‘most_recent’ field from the sub-query. The error I get is “Unknown column ‘a.wine_num’ in ‘on clause”.
Why doesn’t it like the join on “a.wine_num” when it is the first column selected in the main query?