troubleshooting Question

Nested Select on two tables where one table has many rows for one unique item

Avatar of PROJHOPE
PROJHOPE asked on
Microsoft SQL ServerSQL
8 Comments1 Solution330 ViewsLast Modified:
Need help on a joining of data on two tables.  I tried a nested select but didn't produce the results I require.

Business need:
To report on payroll timesheets by employee

There are two tables employees and timecard_raw.

Now the problem.  It seems the vendor used the employee table as an audit table as well.  Each employee is record more than one for any changes to their profile.

For example:

Joe NULL Smith
Joseph NULL Smith
Joseph P Smith

I created this query to retrieve just one row of the data, I used the audit_timestamp and ISNULL to determine the correct unique row.

select
first_name,
middle_name,
last_name,
MAX(DATEADD (mi, audit_timestamp,'01/01/1930')) AS audit_timestamp
from employee
where serial_number = '000002074'
and (first_name IS NOT NULL AND
middle_name IS NOT NULL AND
last_name IS NOT NULL)
group by first_name,middle_name,last_name

My second query is the employee (based by serial number) timecard input with a calculated column to convert VARCHAR into a monetary integer figure

SELECT
A.serial_number,
CONVERT(VARCHAR(10),CONVERT(datetime,A.pay_period),101) AS 'Pay Period',
A.detail_date,
A.sequence,
A.timecode_id,
A.ld_code3,
A.hours,
SUM(CONVERT(decimal(9, 4), LEFT(A.hours, 2)) + (CONVERT(decimal(9, 4), RIGHT(A.hours, 2)) / 60)) AS 'Hours Converted'
FROM
Ceridian.dbo.timecard_raw AS A
WHERE
A.serial_number =  '000002074'
GROUP BY
A.serial_number,A.pay_period,A.detail_date,A.sequence,A.timecode_id,A.ld_code3,A.hours

the 'Hours Converted' calculated column works well...

If I do an INNER JOIN with the employees table, then the 'Hours Converted' is summed up by the amount of rows listed on the employee table for each employee.

I need to be able to combine query #1 and #2

Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros