Click here to Skip to main content
16,016,693 members
Home / Discussions / Database
   

Database

 
AnswerRe: Help is needed in Triggers and indexes in sql server 2000 Pin
Wendelius15-Feb-09 20:16
mentorWendelius15-Feb-09 20:16 
QuestionHow will we stop the duplicating entries while we entering in Excel database? Pin
hitesh.kalra15-Feb-09 18:54
hitesh.kalra15-Feb-09 18:54 
Questionhow to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing15-Feb-09 15:10
Golden Jing15-Feb-09 15:10 
AnswerRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
_Maxxx_15-Feb-09 16:34
professional_Maxxx_15-Feb-09 16:34 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing15-Feb-09 17:21
Golden Jing15-Feb-09 17:21 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
_Maxxx_15-Feb-09 18:31
professional_Maxxx_15-Feb-09 18:31 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing15-Feb-09 20:45
Golden Jing15-Feb-09 20:45 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
_Maxxx_16-Feb-09 12:50
professional_Maxxx_16-Feb-09 12:50 
Sovann wrote:
I want to write some sql using for loop.


If that is what you want to do (i.e. you want to write loop rather than you want to achive the results most efficiently) then take a look here[^] or an example -but read the highlighted bit abut this being inefficient!

Sovann wrote:
Because i do not know that about number of employee. It will increase when we add new employee info.


The SQL I wrote will handle any number of employees..



select c.empid, c.dt as in1, 

(select c1.dt from clock as c1 where c1.empid = c.empid and status = 'O'
and not exists (select 1 from clock as c2 where c2.empid = c1.empid and c2.status = 'O' and c2.dt < c1.dt)) as out1,

(select c1.dt from clock as c1 where c1.empid = c.empid and status = 'I'
and c1.dt > c.dt) as in2,

(select c1.dt from clock as c1 where c1.empid = c.empid and status = 'O'
and c1.dt > (select c1.dt from clock as c1 where c1.empid = c.empid and status = 'I'
and c1.dt > c.dt)) as out2

from clock as c
where c.status = 'I'
and not exists (select 1 from clock as c1 where c1.empid = c.empid and c1.status = 'I' and c1.dt < c.dt)


the bit not crossed out selects all employees records with an 'In' status where there is not an earlier I status record - i.e. it selects the first In record for the employee. Obviously, if you need to look at multiple days, you will need to change this slightly so that it tests the date and time separately (I wasn't sure from your question whether these were stored in separate columns or not)

So - the SQL will work as it stands with any number of employees, for a single date. Simply expand it a bit to allow multiple dates and it will give you what you want.

OK - I couldn't resist, so I expanded it.


SELECT     EmpId, Dt AS in1,
                          (SELECT     Dt
                            FROM          clock AS c1
                            WHERE      (EmpId = c.EmpId) AND (status = 'O') AND (NOT EXISTS
                                                       (SELECT     1 AS Expr1
                                                         FROM          clock AS c2
                                                         WHERE      (EmpId = c1.EmpId) AND (status = 'O') AND (Dt < c1.Dt) AND (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c1.Dt AS Float))))) AND 
                                                   (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c.Dt AS Float)))) AS out1,
                          (SELECT     Dt
                            FROM          clock AS c1
                            WHERE      (EmpId = c.EmpId) AND (status = 'I') AND (Dt > c.Dt) AND (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c.Dt AS Float)))) AS in2,
                          (SELECT     Dt
                            FROM          clock AS c1
                            WHERE      (EmpId = c.EmpId) AND (status = 'O') AND (Dt >
                                                       (SELECT     Dt
                                                         FROM          clock AS c1
                                                         WHERE      (EmpId = c.EmpId) AND (status = 'I') AND (Dt > c.Dt) AND (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c.Dt AS Float))))) AND 
                                                   (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c.Dt AS Float)))) AS out2
FROM         clock AS c
WHERE     (status = 'I') AND (NOT EXISTS
                          (SELECT     1 AS Expr1
                            FROM          clock AS c1
                            WHERE      (EmpId = c.EmpId) AND (status = 'I') AND (Dt < c.Dt) AND (FLOOR(CAST(Dt AS Float)) = FLOOR(CAST(c.Dt AS Float)))))


That should work, I think, for all dates for any number of employees, where there is at least in 'I' record for any one employee on a date (i.e. if the employee doesn't turn up at all on one day there will be no output for that employee.

___________________________________________
.\\axxx
(That's an 'M')

GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing17-Feb-09 20:58
Golden Jing17-Feb-09 20:58 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
_Maxxx_17-Feb-09 21:53
professional_Maxxx_17-Feb-09 21:53 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing17-Feb-09 22:27
Golden Jing17-Feb-09 22:27 
AnswerRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Wendelius15-Feb-09 18:09
mentorWendelius15-Feb-09 18:09 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing15-Feb-09 20:35
Golden Jing15-Feb-09 20:35 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Wendelius15-Feb-09 21:23
mentorWendelius15-Feb-09 21:23 
GeneralRe: how to create a query by using store procedure in SQL Server 2000 ? Pin
Golden Jing15-Feb-09 22:41
Golden Jing15-Feb-09 22:41 
QuestionAb Inito conundrums.... Pin
PoisonCreed14-Feb-09 15:04
PoisonCreed14-Feb-09 15:04 
AnswerRe: Ab Inito conundrums.... Pin
Ashfield15-Feb-09 8:16
Ashfield15-Feb-09 8:16 
GeneralRe: Ab Inito conundrums.... Pin
PoisonCreed15-Feb-09 12:48
PoisonCreed15-Feb-09 12:48 
GeneralRe: Ab Inito conundrums.... Pin
Ashfield15-Feb-09 20:57
Ashfield15-Feb-09 20:57 
QuestionSQL SERVER 2005 INSTALLATION Pin
Member 465028714-Feb-09 5:16
Member 465028714-Feb-09 5:16 
AnswerRe: SQL SERVER 2005 INSTALLATION Pin
Wendelius14-Feb-09 6:29
mentorWendelius14-Feb-09 6:29 
GeneralRe: SQL SERVER 2005 INSTALLATION Pin
Member 465028714-Feb-09 8:00
Member 465028714-Feb-09 8:00 
GeneralRe: SQL SERVER 2005 INSTALLATION Pin
Wendelius14-Feb-09 9:12
mentorWendelius14-Feb-09 9:12 
Questioncan't find AdventureWorks.dbml Pin
TerRO_GirL14-Feb-09 1:25
TerRO_GirL14-Feb-09 1:25 
AnswerRe: can't find AdventureWorks.dbml Pin
Wendelius14-Feb-09 1:36
mentorWendelius14-Feb-09 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.