Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type of query 11 42
ms access filter query with empty combobox 5 29
My SQL as Backend for Access 3 14
Access summarise unique values in tables 2 14
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

770 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