oracle128
March 8th, 2007, 12:59 AM
Ok, in an Access database, I have the following tables:
tbl_staff
staff_id
first_name
surname
tbl_booking
booking_id
booking_date
cost
staff_id (FK)
tbl_weekdates
weekdate_id
weeknum
weekending
year (follows financial year)
Now what I need to do is create a report that will group each staff member, where each member has a list all the weeks of a particular year from the weekdates datable, and their total sales for that week.
Before anyone asks, yes, tbl_weekdates is needed - not only does it follow the financial year, but it also needs to excludes weeks that weren't worked (eg Christmas) from any calculations, and it's unpredictable.
So far, I'm trying to construct the base query for it, but access doesn't seem to like any of the joins or grouping I'm doing, especially Right Joins. The number of records returned by the query should be the product of staff and weekdates, with each of these weeks also summing the cost. Grouping by staff member will be done in the report.
So, if there are 2 staff, and 50 weekdates in the 2006 financial year, and let's say each staff member has made two sales each, in the first and second weeks, a query where tbl_weekdates.year=2006 should return 100 records like follows:
staff_id, weeknum, total_sales
1, 1, $100
1, 2, $300
1, 3, 0
...
1, 50, 0
2, 1, $157
2, 2, $250
2, 3, 0
...
2, 50, 0
Just how in the hell could I construct that query? Everything I've tried using RIGHT JOINs has either produced Access errors ("Join expression not supported"), or an odd result. The closest I can get is:SELECT s.staff_id,w.weeknum FROM tbl_staff s,tbl_weekdates w
WHERE w.year=2006Which returns the right number of records, but as soon as I try to introduce the cost it screws up one way or another (I'm guessing because the two tables are selected seperately, it can't form a relationship between them via tbl_bookings).
I know the join for staff->booking is tbl_staff s INNER JOIN tbl_booking b ON s.staff_id=b.staff_idAnd I know the join for booking->weekdate is tbl_bookings b RIGHT JOIN tbl_weekdates w ON b.booking_date<=w.weekending AND b.booking_date>DateAdd("d",-7,w.weekending)(with RIGHT JOIN questionable, it may need an INNER when everything's put together). Those are the easy parts, and individually they work fine, but putting them all into one query, I can't figure out how it should go.
tbl_staff
staff_id
first_name
surname
tbl_booking
booking_id
booking_date
cost
staff_id (FK)
tbl_weekdates
weekdate_id
weeknum
weekending
year (follows financial year)
Now what I need to do is create a report that will group each staff member, where each member has a list all the weeks of a particular year from the weekdates datable, and their total sales for that week.
Before anyone asks, yes, tbl_weekdates is needed - not only does it follow the financial year, but it also needs to excludes weeks that weren't worked (eg Christmas) from any calculations, and it's unpredictable.
So far, I'm trying to construct the base query for it, but access doesn't seem to like any of the joins or grouping I'm doing, especially Right Joins. The number of records returned by the query should be the product of staff and weekdates, with each of these weeks also summing the cost. Grouping by staff member will be done in the report.
So, if there are 2 staff, and 50 weekdates in the 2006 financial year, and let's say each staff member has made two sales each, in the first and second weeks, a query where tbl_weekdates.year=2006 should return 100 records like follows:
staff_id, weeknum, total_sales
1, 1, $100
1, 2, $300
1, 3, 0
...
1, 50, 0
2, 1, $157
2, 2, $250
2, 3, 0
...
2, 50, 0
Just how in the hell could I construct that query? Everything I've tried using RIGHT JOINs has either produced Access errors ("Join expression not supported"), or an odd result. The closest I can get is:SELECT s.staff_id,w.weeknum FROM tbl_staff s,tbl_weekdates w
WHERE w.year=2006Which returns the right number of records, but as soon as I try to introduce the cost it screws up one way or another (I'm guessing because the two tables are selected seperately, it can't form a relationship between them via tbl_bookings).
I know the join for staff->booking is tbl_staff s INNER JOIN tbl_booking b ON s.staff_id=b.staff_idAnd I know the join for booking->weekdate is tbl_bookings b RIGHT JOIN tbl_weekdates w ON b.booking_date<=w.weekending AND b.booking_date>DateAdd("d",-7,w.weekending)(with RIGHT JOIN questionable, it may need an INNER when everything's put together). Those are the easy parts, and individually they work fine, but putting them all into one query, I can't figure out how it should go.