January 2, 2008 at 2:07 pm
Hello All. Happy New Year.
I a table which lists changes that have taken place over the past week. I would like to list records for the past two business days, but only if there is a record for each business day, the 31st of December, 2007 and the 2nd of January, 2008. I do not want to see the records from the 31st of December, 2007 if they do not have a corresponding January record.
I originally wrote this, without the #Retrieval table:
--Get results from the most recent two days and place in staging table
SELECT DateReported, OWN, Name, EmployeeStatus, FullorPartTime,
GradeLevel, Title, JobEntryDate, OriginalHireDate, CurrentHireDate,
TermDate1, LOAStartDate1, LOAReturnDate1, DepartmentDesc, DepartmentEntryDate,
MgrOWN, ManagersName, isnull(OTC,0) as OTC, TypeCode, isnull(JobCode, 'na') as JobCode
into #Retrieval
from smcsarchive..CEM_PersonnelChanges2008
where datepart(dd,DateReported) = 2 --**Set most recent two days
or datepart(dd,DateReported) = 31
group by DateReported, OWN, Name, EmployeeStatus, FullorPartTime,
GradeLevel, Title, JobEntryDate, OriginalHireDate, CurrentHireDate,
TermDate1, LOAStartDate1, LOAReturnDate1, DepartmentDesc, DepartmentEntryDate,
MgrOWN, ManagersName, OTC, TypeCode, JobCode
having count(OWN) >1
ORDER BY Name, OWN, DateReported
I received all of the 12/31/2007 records and all of the 01/02/2008 records. I had hoped the clause " having count(OWN) >1" would eliminate some if not all of the unnecessary records. But it did not.
I then tried removing the count clause, and inserting into #Retrieval and doing this:
--Get data from #Retrieval table for past two days only (not previous day compared to the day before it)
SELECT DateReported, OWN, Name, EmployeeStatus, FullorPartTime,
GradeLevel, Title, JobEntryDate, OriginalHireDate, CurrentHireDate,
TermDate1, LOAStartDate1, LOAReturnDate1, DepartmentDesc, DepartmentEntryDate,
MgrOWN, ManagersName, OTC, TypeCode, JobCode
from #Retrieval
group by DateReported, OWN, Name, EmployeeStatus, FullorPartTime,
GradeLevel, Title, JobEntryDate, OriginalHireDate, CurrentHireDate,
TermDate1, LOAStartDate1, LOAReturnDate1, DepartmentDesc, DepartmentEntryDate,
MgrOWN, ManagersName, OTC, TypeCode, JobCode
having count(OWN) > 1
ORDER BY Name, OWN, DateReported
At which time I receive no data. (Corrected to say no data.)
Am I going about this all wrong? Suggestions? Advice?
Thanks for reading.
January 2, 2008 at 2:24 pm
It would help if you could provide some test data, the DDL for the table(s), and the expected results. Just looking at the code, its hare to tell what the problem could be.
😎
January 2, 2008 at 11:58 pm
http://www.sqlservercentral.com/articles/Best+Practices/61537/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply