I'm a VERY novice SQL user. I've got an Access ADP project connected to an SQL 2000 Server.
My dilemma is that I have a single table that stores all data for my particular project, with links to other data tables, of course. I need to get a set of data for every client that has an entry in this table. The condition is, if an invoice entry exists, I only want records where the WorkDate is the same, or newer than the newest invoice ( invoice is WorkType of 3); If the client doesn't have an invoice yet, I need to get everything for that particular client that has a billable work type, ie. WorkType <= 4. This query is being used in a subform, and filtered based on a clientID from the parent form.
I've tried several solutions and none of them seems to give me this differentiation . I started with a subquery, I can get the appropriate information, but only for the clients who already have an invoice.
I can't seem to get the appropriate information for clients who DON'T have an invoice, yet. I ended up trying a union, but that just ends up returning ALL records... I think the first query in this union needs some work.
This is what I've ended up with...
As I said, this seems to send back all records, not just the ones I want. At this point, I'm just not sure what I even need to be looking for. Any help will be greatly appreciated.
My dilemma is that I have a single table that stores all data for my particular project, with links to other data tables, of course. I need to get a set of data for every client that has an entry in this table. The condition is, if an invoice entry exists, I only want records where the WorkDate is the same, or newer than the newest invoice ( invoice is WorkType of 3); If the client doesn't have an invoice yet, I need to get everything for that particular client that has a billable work type, ie. WorkType <= 4. This query is being used in a subform, and filtered based on a clientID from the parent form.
I've tried several solutions and none of them seems to give me this differentiation . I started with a subquery, I can get the appropriate information, but only for the clients who already have an invoice.
Code:
SELECT TOP 100 PERCENT *, DATEDIFF(Minute, StartTime, EndTime) / 60.0 AS Hours, DATEDIFF(Minute, StartTime, EndTime)
/ 60.0 - CreditHours AS TotalLeft
FROM TSheet ts1
WHERE (Approved = 1) AND (WorkDate >=
(SELECT TOP 1 WorkDate
FROM TSheet ts2
WHERE ((ts2.WorkType = 3) AND (ts1.udfID = ts2.udfID))
ORDER BY ts2.WorkDate DESC))
ORDER BY WorkDate
This is what I've ended up with...
Code:
SELECT TOP 100 PERCENT *
FROM (SELECT TOP 100 PERCENT *, DATEDIFF(MINute, StartTime, EndTime) / 60.0 AS Hours, DATEDIFF(MINute, StartTime, EndTime)
/ 60.0 - CreditHours AS TotalLeft
FROM TSheet ts1
WHERE (Approved = 1) AND (WorkDate >=
(SELECT TOP 1 WorkDate
FROM TSheet ts2
WHERE WorkType <= 4 AND WorkType != 3 AND ts1.udfID = ts2.udfID
ORDER BY WorkDate ASC))
UNION
SELECT TOP 100 PERCENT *, DATEDIFF(MINute, StartTime, EndTime) / 60.0 AS Hours, DATEDIFF(MINute, StartTime, EndTime)
/ 60.0 - CreditHours AS TotalLeft
FROM TSheet ts1
WHERE (Approved = 1) AND (WorkDate >=
(SELECT TOP 1 WorkDate
FROM TSheet ts2
WHERE WorkType = 3 AND ts1.udfID = ts2.udfID
ORDER BY WorkDate DESC))) tUnion
ORDER BY WorkDate
Comment