Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-03
4
Medium Priority
?
334 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 1000 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 1000 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 93

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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 …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

715 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