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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Eliminate users on condition.

Author  Topic 

p.shaw3@ukonline.co.uk
Posting Yak Master

103 Posts

Posted - 2006-09-07 : 04:27:15
I am using:
SELECT (CASE WHEN LM.dbo.TimeSheets.ActivityCode = 'Leaver' THEN '1' ELSE '0' end) AS 'Leaver'

which grabs all users in the table between 2 dates using a BETWEEN clause. The CASE returns a user twice (once with a zero and once with a 1) if they have had 'Leaver' as their Activity Code at any day in the BETWEEN clause. It always returns a zero as a user has to have an activity code for every day. This is not really what I want.

I actually need to eliminate any users from selection that show as a 'Leaver' at any point in the BETWEEN clause, but I cannot figure out how to do this. Can anyone please help.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-07 : 04:31:35
I am not clear what you want. Please provide some example data, table structures & expected result.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-07 : 04:32:33
Use MAX function and GROUP BY the user id.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-07 : 04:34:03
[code]
SELECT Sum(CASE WHEN LM.dbo.TimeSheets.ActivityCode = 'Leaver' THEN 1 ELSE 0 end) AS 'Leaver'
[/code]

Chirag
Go to Top of Page

p.shaw3@ukonline.co.uk
Posting Yak Master

103 Posts

Posted - 2006-09-07 : 07:38:20
Thanks Guys, but these still give me 2 rows where the person has had 'Leaver'. I want to eliminate a user that appears twice.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-07 : 07:40:46
[code]SELECT UserID AS [Leaver]
FROM LM.dbo.TimeSheets
WHERE ActivityCode = 'Leaver'
group by UserID -- see my previous post
having COUNT(*) > 0 -- Or 1. Try yourself with the right value[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-07 : 07:45:09
quote:
Originally posted by p.shaw3@ukonline.co.uk

Thanks Guys, but these still give me 2 rows where the person has had 'Leaver'. I want to eliminate a user that appears twice.



Do you want to eliminate the same user appearing twice or you want only one user having 'Leaver'?



Chirag
Go to Top of Page

p.shaw3@ukonline.co.uk
Posting Yak Master

103 Posts

Posted - 2006-09-07 : 08:28:51
I want to eliminate ALL users who appear twice.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-09 : 01:54:25
Generally this is done..

Select * From <LM.dbo.TimeSheets> Tbl1 Where PK = (Select Max(PK) From <LM.dbo.TimeSheets> Tbl2 Where Tbl1.User = Tbl2.User)

Replace PK with the primary key of that table..

Chirag
Go to Top of Page
   

- Advertisement -