Solved

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

Posted on 2007-04-03
4
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 250 total points
ID: 18849199
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
ID: 18849506
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
ID: 18880394
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
ID: 18881762
Rob,

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

Good luck with the project,

Patrick
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

710 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