473,748 Members | 8,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to calculate from most recent of duplicate records in MSSQL

2 New Member
I'm working with a SQL query for a Human Resources database. Its intended purpose is to find all the paycheck records with a check date (prckhist.chkda te ) more recent than eleven days past the employee's last raise (hrpersnl.p_pay date). It then totals the hours the employee worked from those records (prdehist.units ), and if the total is within 40 hours of a threshold (hrpersnl.p_jmi sc3) then the employee's name and information is returned, and the employee is eligible for another raise.
The problem is this: the database has been corrupted, and there are a few duplicate records in the hrpersnl table. Because of this, in some cases the query will use an incorrect last raise date (hrpersnl.p_pay date) and find the wrong number of paycheck records, throwing the total off.
My question is, how do I make the query calculate from the most recent hrpersnl record, in the event that there are duplicates? I've tried changing the 6th line in the WHERE section to the following:
Expand|Select|Wrap|Line Numbers
  1. AND (MAX(hrpersnl.p_paydate)+11) <= prckhist.chkdate 
I've also tried
Expand|Select|Wrap|Line Numbers
  1. AND (SELECT MAX(hrpersnl.p_paydate)+11) <= prckhist.chkdate 
and
Expand|Select|Wrap|Line Numbers
  1. AND ((SELECT MAX(hrpersnl.p_paydate)FROM hrpersnl)+11) <= prckhist.chkdate 
and a few other variants. I feel like I'm missing something really basic. Anyone have any ideas?
Thanks in advance. Full code is as follows:
Expand|Select|Wrap|Line Numbers
  1.  SELECT        hrpersnl.p_empno AS Employee_Number, 
  2.         hrpersnl.p_fname AS First_Name, 
  3.         hrpersnl.p_lname AS Last_Name, 
  4.         hrpersnl.p_paydate AS Effective_Date,
  5.         SUM(prdehist.units) +hrtkpers.u_hrswrk AS Total, 
  6.         hrpersnl.p_jmisc3 AS Threshold
  7. FROM    prdehist prdehist, hrpersnl hrpersnl, prckhist prckhist,hrtkpers
  8. WHERE        prdehist.empno = hrpersnl.p_empno 
  9.         AND prdehist.empno = prckhist.empno 
  10.         AND prdehist.chknumber = prckhist.chknumber 
  11.         AND prdehist.empno=hrtkpers.u_empno 
  12.         AND hrpersnl.p_active <>"T" 
  13.         AND hrpersnl.p_paydate+11 <= prckhist.chkdate 
  14.         AND (prdehist.code = '0001')
  15. GROUP BY    hrpersnl.p_empno
  16. HAVING    (SUM(prdehist.units)+hrtkpers.u_hrswrk>= (hrpersnl.p_jmisc3 - 40)) AND (hrpersnl.p_jmisc3 >0) 
  17. ORDER BY     hrpersnl.p_empno 
Jan 11 '07 #1
4 2748
iburyak
1,017 Recognized Expert Top Contributor
Try this:

[PHP]SELECT hrpersnl.p_empn o AS Employee_Number ,
hrpersnl.p_fnam e AS First_Name,
hrpersnl.p_lnam e AS Last_Name,
hrpersnl.p_payd ate AS Effective_Date,
SUM(prdehist.un its) +hrtkpers.u_hrs wrk AS Total,
hrpersnl.p_jmis c3 AS Threshold
FROM prdehist prdehist, (select distinct * from hrpersnl) hrpersnl, prckhist prckhist,hrtkpe rs
WHERE prdehist.empno = hrpersnl.p_empn o
AND prdehist.empno = prckhist.empno
AND prdehist.chknum ber = prckhist.chknum ber
AND prdehist.empno= hrtkpers.u_empn o
AND hrpersnl.p_acti ve <>"T"
AND hrpersnl.p_payd ate+11 <= prckhist.chkdat e
AND (prdehist.code = '0001')
GROUP BY hrpersnl.p_empn o
HAVING (SUM(prdehist.u nits)+hrtkpers. u_hrswrk>= (hrpersnl.p_jmi sc3 - 40)) AND (hrpersnl.p_jmi sc3 >0)
ORDER BY hrpersnl.p_empn o [/PHP]
Jan 11 '07 #2
iburyak
1,017 Recognized Expert Top Contributor
[PHP]SELECT hrpersnl.p_empn o AS Employee_Number ,
hrpersnl.p_fnam e AS First_Name,
hrpersnl.p_lnam e AS Last_Name,
hrpersnl.p_payd ate AS Effective_Date,
SUM(prdehist.un its) +hrtkpers.u_hrs wrk AS Total,
hrpersnl.p_jmis c3 AS Threshold
FROM prdehist prdehist,
(select distinct * from hrpersnl) hrpersnl,
prckhist prckhist,
hrtkpers
WHERE prdehist.empno = hrpersnl.p_empn o
AND prdehist.empno = prckhist.empno
AND prdehist.chknum ber = prckhist.chknum ber
AND prdehist.empno= hrtkpers.u_empn o
AND hrpersnl.p_acti ve <>"T"
AND hrpersnl.p_payd ate+11 <= prckhist.chkdat e
AND (prdehist.code = '0001')
GROUP BY hrpersnl.p_empn o
HAVING (SUM(prdehist.u nits)+hrtkpers. u_hrswrk>= (hrpersnl.p_jmi sc3 - 40)) AND (hrpersnl.p_jmi sc3 >0)
ORDER BY hrpersnl.p_empn o [/PHP]



For some reason it didn't make my change bold but you will figure.
Jan 11 '07 #3
Sector 7G
2 New Member
Thanks for your reply.
Unfortunately that returned the following error:
Expand|Select|Wrap|Line Numbers
  1. 3700: [Microsoft][ODBC Visual FoxPro Driver]Command contains unrecognizable phrase/keyword.
Jan 11 '07 #4
iburyak
1,017 Recognized Expert Top Contributor
Sorry, I wasn't aware it is a Fox Pro.

But still you can create a view to select distinct records from this table and use a view in this query instead of a table name.

Good Luck.
Jan 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2589
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID), username, date, and time field like this: 1234 williamstim 01AUG03 7:44:31 2348 williamstim 02AUG03 14:11:20
6
10315
by: Pawel Banys | last post by:
Hello, There is a program which performs some scripted actions via ODBC on tables in some database on mssql 2000. Sometimes that program tries to insert record with key that is already present in the database. The error comes up and the program stops. Is there any way to globally configure the database or the whole mssql server to ignore such attempts and let the script continue without any error when the script tries to insert...
1
3433
by: Tim Graichen | last post by:
Good morning, I have a sub-form that displays records from a table as a continuous form. The table has several hundred records, but the subform only displays five or six records. The records do include a date field. How can I force the sub form to display the most recent dates by default, verses forcing the user to scroll through all of the records to the bottom of the form in order to view the most recent entry or to add a new entry...
2
3875
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2 records (and also show the above 3 fields) So suppose the table looks like this:
0
2728
by: Frances | last post by:
Hi All, I'm having a problem trying to add a record to a simple Access 2000 db (db is very similar to an address book but with more info than the usual address, phone, etc.). The database is one table, 36 fields. The record_id field is an autonumber field (long int) and primary key. The rest of the fields comprise of 30 text fields, 3 memo fields, 1 date field and 1 currency field. Text fields vary in number of characters allowed. ...
6
1904
by: syvman | last post by:
Hi all... I have a quick question, if anyone knows the answer. I've built a query which looks at a set of records in a table (tblFilename) and I've set it up so that it displays the following fields: filename, filedate, origin Let me first explain the data - filename contains filenames, of which there could be multiple items... filedate is the respective date stamp from the filename listed... origin is the folder name where the file...
8
14318
by: Jason H | last post by:
Hi, I am sure I am just overlooking the obvious, but I am having a little trouble with this one... I am setting up an inventory database that tracks company tools and their location. This database has a table tblToolInfo and another table tblTransferHistory which are linked by a Tool ID field. As tools are checked out they are recorded in tbl TransferHistory based on ID, location taken from, location taken to, authorizing person, and date....
3
5262
by: erikcw | last post by:
Hi, I'm getting the following error when I try to pass a list into a function. My List: crea = Traceback (most recent call last): File "wa.py", line 118, in ? curHandler.walkData()
2
3289
by: robert.waters | last post by:
I need to perform the following: - select the most recent X number of records in a table (there is a timestamp field) - select the Nth occurrence of X number of records ex: - most recent 10 records (N is 1) - most recent records 10-20 (N would be 2) - most recent records 10-30 (etc.)
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.