Solved

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

Posted on 2007-04-03
4
284 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

914 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

16 Experts available now in Live!

Get 1:1 Help Now