SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SQL Query against parent-child tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

greenstone90
Starting Member

8 Posts

Posted - 01/14/2013 :  17:07:00  Show Profile  Reply with Quote
Hi,

I have two tables:

EMPLOYEE_SERVICE table
ID
FROM_DATE
TO_DATE


EMPLOYEE_SERVICE_DETAILS table
ID
EMPLOYEE_SERVICE_ID (fk to EMPLOYEE_SERVICE.ID)
FROM_DATE
TO_DATE


EMPLOYEE_SERVICE:EMPLOYEE_SERVICE_DETAILS is 1:m relationship


My program expects, if there is at one or more EMPLOYEE_SERVICE_DETAIL records for an EMPLOYEE_SERVICE record, then one EMPLOYEE_SERVICE_DETAIL record will

have the same FROM_DATE as its parent EMPLOYEE_SERVICE.FROM_DATE.


I think I have some bad data: I need to get a list of all EMPLOYEE_SERVICE records that have EMPLOYEE_SERVICE_DETAILS records, but don't have a

EMPLOYEE_SERVICE_DETAILS record with a matching FROM_DATE (note: an EMPLOYEE_SERVICE record doesn't have to have any EMPLOYEE_SERVICE_DETAILS).


This didn't seem to work:

select ES.ID, ES.EMPLOYEE_ID, ES.FROM_DATE, ESD.FROM_DATE from GRB_ASSIST.EMP_SERVICE_DETAILS ESD INNER JOIN GRB_ASSIST.EMP_SERVICE ES on ESD.EMPLOYEE_SERVICE_ID = ES.ID
GROUP BY ESD.EMPLOYEE_SERVICE_ID, ESD.FROM_DATE, ES.FROM_DATE, ES.EMPLOYEE_ID, ES.ID
HAVING ESD.FROM_DATE <> ES.FROM_DATE AND COUNT(ESD.EMPLOYEE_SERVICE_ID) > 1


thoughts how to make this query?

Thanks!


jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 01/14/2013 :  17:42:16  Show Profile  Reply with Quote
SELECT ID,FROM_DATE,TO_DATE
FROM EMPLOYEE_SERVICE es
WHERE NOT EXISTS (select *
from EMPLOYEE_SERVICE_DETAILS esd
where es.id = esd.EMPLOYEE_SERVICE_ID and es.FROM_DATE = esd.FROM_DATE
)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/15/2013 :  00:05:16  Show Profile  Reply with Quote
quote:
Originally posted by jimf

SELECT ID,FROM_DATE,TO_DATE
FROM EMPLOYEE_SERVICE es
WHERE NOT EXISTS (select *
from EMPLOYEE_SERVICE_DETAILS esd
where es.id = esd.EMPLOYEE_SERVICE_ID and es.FROM_DATE = esd.FROM_DATE
)

Jim

Everyday I learn something that somebody else already knew


This will even return employees who dont have a detail record at all
see

declare @EMPLOYEE_SERVICE table
(
ID int,
FROM_DATE date,
TO_DATE date
)


declare @EMPLOYEE_SERVICE_DETAILS table
(
ID int,
EMPLOYEE_SERVICE_ID int ,
FROM_DATE date,
TO_DATE date
)

INSERT @EMPLOYEE_SERVICE
VALUES (1, GETDATE()-23,GETDATE()-16),
 (7, GETDATE()-123,GETDATE()-96),
  (9, GETDATE()-55,GETDATE()-43)--this doesnt have detail record

  INSERT @EMPLOYEE_SERVICE_DETAILS
VALUES (1,1, GETDATE()-23,GETDATE()-10),
 (2,7, GETDATE()-116,GETDATE()-33)-- this has wront from date

 SELECT ID,FROM_DATE,TO_DATE
FROM @EMPLOYEE_SERVICE es
WHERE NOT EXISTS (select *
from @EMPLOYEE_SERVICE_DETAILS esd
where es.id = esd.EMPLOYEE_SERVICE_ID and es.FROM_DATE = esd.FROM_DATE
)

output
-----------------------------------
ID	FROM_DATE	TO_DATE
-----------------------------------
7	2012-09-14	2012-10-11
9	2012-11-21	2012-12-03



Reading this

I think I have some bad data: I need to get a list of all EMPLOYEE_SERVICE records that have EMPLOYEE_SERVICE_DETAILS records, but don't have a

EMPLOYEE_SERVICE_DETAILS record with a matching FROM_DATE (note: an EMPLOYEE_SERVICE record doesn't have to have any EMPLOYEE_SERVICE_DETAILS).

I feel OP is looking only for cases where employee has a service detail record but with a mismatched FROM_DATE
That being case i think it should be

SELECT ID,FROM_DATE,TO_DATE
FROM(
SELECT es.*,COUNT(CASE WHEN es.FROM_DATE = esd.FROM_DATE THEN 1 END) OVER (PARTITION BY es.ID) AS Cnt
FROM @EMPLOYEE_SERVICE es
INNER JOIN @EMPLOYEE_SERVICE_DETAILS esd
ON  es.id = esd.EMPLOYEE_SERVICE_ID
)t
WHERE Cnt=0

Output
-------------------------------------
ID	FROM_DATE	TO_DATE
-------------------------------------
7	2012-09-14	2012-10-11



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Edited by - visakh16 on 01/15/2013 00:10:45
Go to Top of Page

greenstone90
Starting Member

8 Posts

Posted - 01/15/2013 :  14:56:22  Show Profile  Reply with Quote
Hi Jim and Visakh16,

Thanks for the help!

Visakh, you were exactly right...and your script worked well.

Regards,

Greenstone
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/16/2013 :  22:47:47  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000