Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
san79
Starting Member
42 Posts |
Posted - 2008-04-22 : 02:43:26
|
hi i have a table containing three fieldsempid, ddate and statusthe status has three values P(present),A(Absent), and NAi am trying to retrieve data for those who are continuously absent between dates which i will give as input, i can do it in hard coded way but i wish to know that is there some query to achieve this. regards |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 03:44:02
|
SELECT EmpIDFROM TableWHERE dDate >= '20080101'AND dDate < '20080201'GROUP BY EmpIDHAVING MIN(CASE WHEN Status = 'A' THEN 1 ELSE 0 END) = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-04-22 : 10:05:26
|
hello mr.pesoThanks for the query, it works really good, now if i add ddate in the selection criteria the result is bizarre i.e. it also take the other employee who are absent for a day or two in the given criteria. my query is SELECT empid,ddateFROM dbo.tbltestWHERE (ddate >= '20070401') AND (ddate < '20070405')GROUP BY empid,ddateHAVING (MIN(CASE WHEN Status = 'A' THEN 1 ELSE 0 END) = 1)the out put is 1 20070401 2 20070401 1 20070402 2 20070402 1 20070403 1 20070404 2 20070404but i want my output only represent those employees who are continuously absent like this1 200704011 200704021 200704031 200704041 20070405how to obtain the result, sorry if it is silly, i am just started |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 10:23:05
|
[code]-- Mimic user supplied parameters to stored procedure or functionDECLARE @FirstDate DATETIME, @LastDate DATETIMESELECT @FirstDate = '20070401', @LastDate = '20070405'-- Here is the code needed to accomplish your requirementsSELECT @FirstDate = DATEDIFF(DAY, '19000101', @FirstDate), @LastDate = DATEDIFF(DAY, '18991231', @LastDate)SELECT EmpIDFROM tblTestWHERE dDate >= @FirstDate AND dDate < @LastDate AND Status = 'A'GROUP BY EmpIDHAVING COUNT(DISTINCT DATEDIFF(DAY, '19000101', dDate)) = DATEDIFF(DAY, @FirstDate, @LastDate)[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-04-22 : 10:26:19
|
understood will do the needfulThanks for the pointer :-) |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-04-22 : 10:28:46
|
to the second postingwill try and get back to you sir, |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 10:31:48
|
If all dDate do not have time information other than '00:00:00', you can use this-- Mimic user supplied parameters to stored procedure or functionDECLARE @FirstDate DATETIME, @LastDate DATETIMESELECT @FirstDate = '20070401', @LastDate = '20070404'-- Here is the code needed to accomplish your requirementsSELECT EmpIDFROM tblTestWHERE dDate BETWEEN @FirstDate AND LastDate AND Status = 'A'GROUP BY EmpIDHAVING COUNT(DISTINCT dDate) = DATEDIFF(DAY, @FirstDate, @LastDate) + 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|