Solved

SQL Code to select the first item only in each recordset meeting specified criteria

Posted on 2007-04-03
4
270 Views
Last Modified: 2012-08-14
I need to create a query that will calculate the charge amount for each item of each Job based on the following criteria:
If FPInc = false or if the FPInc immediately preceding this one was false and this one is True then Charge = TotalInc
A sample of data is as follows:
FPInc   TotalInc     Charge
False     0.00           0.00
False     22.60        22.60
False     78.20        78.20
False     45.00        45.00
True       89.00        89.00
True       19.80          0.00
True        12.50         0.00
False      18.83        18.83

The Full SQL Query that goes part way to accomplishing what I need is as follows. Is there a way of doing what I need with SQL or do I need to write some code to process the recordset to achieve what I want?

SELECT Jobs.ClosedDate, Jobs.ClientID, JobItems.JobID, JobItems.Sequence, JobItems.FPInc, JobItems.TotalInc, IIf(Not [fpinc],[totalinc],0) AS Charge
FROM JobItems INNER JOIN Jobs ON JobItems.JobID = Jobs.JobID
ORDER BY Jobs.ClosedDate, JobItems.JobID, JobItems.Sequence, JobItems.FPInc;
0
Comment
Question by:Rob4077
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 250 total points
Comment Utility
Hi Rob,
The difficult part with this is that you are trying to identify a preceding record.
If the records(i.e. the sample data that you provided above) had a sequnce number field or if the database design could be modified to incorporate a sequence number field(1,2,3,4,5 etc.) it would make this more possible.
Then you could have a calculated field which gets the previous FPInc

SELECT Jobs.ClosedDate, Jobs.ClientID, JobItems.JobID, JobItems.Sequence, JobItems.FPInc, JobItems.TotalInc, IIf(Not [fpinc],[totalinc],0) AS Charge, SELECT FPInc FROM Jobs j where j.Sequence = Jobs.Sequence - 1 As PreviousFPInc
FROM JobItems INNER JOIN Jobs ON JobItems.JobID = Jobs.JobID
ORDER BY Jobs.ClosedDate, JobItems.JobID, JobItems.Sequence, JobItems.FPInc;

With a sequence field you would have a more definite way of getting the previous record
If you cannot use a Sequnce Number you may be able to do someting usign your CloseDate field
Select Top1 FROM FPInc FROM Jobs j WHERE j.CloseDate < Jobs.CloseDate


Cheers

B Cunney  

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
Comment Utility
Try:

SELECT j.ClosedDate, j.ClientID, ji.JobID, ji.Sequence, ji.FPInc, ji.TotalInc,
IIf((Not ji.[fpinc]) OR (Not
    (SELECT TOP 1 ji2.fpinc
    FROM JobItems AS ji2 INNER JOIN Jobs AS j2 ON ji.JobID = j.JobID
    WHERE j2.ClosedDate = j.ClosedDate AND ji2.JobID = ji.JobID AND
        ji2.Sequence < ji.Sequence
    ORDER BY j2.ClosedDate ASC, ji2.JobID ASC, ji2.Sequence DESC))
, ji.[totalinc], 0) AS Charge
FROM JobItems AS ji INNER JOIN Jobs AS j ON ji.JobID = j.JobID
ORDER BY j.ClosedDate, ji.JobID, ji.Sequence, ji.FPInc
0
 

Author Comment

by:Rob4077
Comment Utility
Sorry for the delay in getting back to you on this one - my laptop was damaged and had to go in for major repairs. I am now working on a backup machine but in the meantime, while offline, I found an alternative solution so it turns out I don't need the code.

Ideally I would like to be able to change the table but I can't because I am reading tables from someone else's system so that wouldn't have been the perfect solution. And after re-examining the table structure there would have been instances where matthewspatrick's solution would have stumbled too.  Nevertheless I really appreciate you taking the time to look at it and I have awarded points  because you made the effort for which I thank you very much.

Regards,
Rob
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Rob,

Glad to help, and sorry to hear you had some equipment mishaps.

Good luck with the project,

Patrick
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now