473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to calculate from most recent of duplicate records in MSSQL

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.chkdate ) more recent than eleven days past the employee's last raise (hrpersnl.p_paydate). 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_jmisc3) 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_paydate) 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 2730
iburyak
1,017 Expert 512MB
Try this:

[PHP]SELECT hrpersnl.p_empno AS Employee_Number,
hrpersnl.p_fname AS First_Name,
hrpersnl.p_lname AS Last_Name,
hrpersnl.p_paydate AS Effective_Date,
SUM(prdehist.units) +hrtkpers.u_hrswrk AS Total,
hrpersnl.p_jmisc3 AS Threshold
FROM prdehist prdehist, (select distinct * from hrpersnl) hrpersnl, prckhist prckhist,hrtkpers
WHERE prdehist.empno = hrpersnl.p_empno
AND prdehist.empno = prckhist.empno
AND prdehist.chknumber = prckhist.chknumber
AND prdehist.empno=hrtkpers.u_empno
AND hrpersnl.p_active <>"T"
AND hrpersnl.p_paydate+11 <= prckhist.chkdate
AND (prdehist.code = '0001')
GROUP BY hrpersnl.p_empno
HAVING (SUM(prdehist.units)+hrtkpers.u_hrswrk>= (hrpersnl.p_jmisc3 - 40)) AND (hrpersnl.p_jmisc3 >0)
ORDER BY hrpersnl.p_empno [/PHP]
Jan 11 '07 #2
iburyak
1,017 Expert 512MB
[PHP]SELECT hrpersnl.p_empno AS Employee_Number,
hrpersnl.p_fname AS First_Name,
hrpersnl.p_lname AS Last_Name,
hrpersnl.p_paydate AS Effective_Date,
SUM(prdehist.units) +hrtkpers.u_hrswrk AS Total,
hrpersnl.p_jmisc3 AS Threshold
FROM prdehist prdehist,
(select distinct * from hrpersnl) hrpersnl,
prckhist prckhist,
hrtkpers
WHERE prdehist.empno = hrpersnl.p_empno
AND prdehist.empno = prckhist.empno
AND prdehist.chknumber = prckhist.chknumber
AND prdehist.empno=hrtkpers.u_empno
AND hrpersnl.p_active <>"T"
AND hrpersnl.p_paydate+11 <= prckhist.chkdate
AND (prdehist.code = '0001')
GROUP BY hrpersnl.p_empno
HAVING (SUM(prdehist.units)+hrtkpers.u_hrswrk>= (hrpersnl.p_jmisc3 - 40)) AND (hrpersnl.p_jmisc3 >0)
ORDER BY hrpersnl.p_empno [/PHP]



For some reason it didn't make my change bold but you will figure.
Jan 11 '07 #3
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 Expert 512MB
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
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),...
6
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...
1
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...
2
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...
0
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...
6
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...
8
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...
3
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.