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 2005 Forums
 Transact-SQL (2005)
 Problem with distinct rows on a databse.

Author  Topic 

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-29 : 23:58:46
Hello.I have 3 tables : animals,doctors and event.
animals FK to doctors and event is a stand alone table that connects to doctors by a username column.Now events also has an animalid column that simulates the id of the animal table and 0 equals to no animal id connection.I am showing every event according to animal id like this and works fine:


select animals.id as anid,[event].[name], animals.name as animalsname,
doctors.email
from [event] inner join doctors on [event].username = doctors.username
inner join animals on doctors.id =animals.doctorid
where [event].username=lower(@username) and doctors.username =lower([event].username)
and [event].animalid= animals.id


This will bring me any animal that has an even.Now my problem is the 0 value on event.Zero is to specifically connected to any animal id so it will bring all the animals of the doctor:


select animals.id as anid,[event].[name], animals.name as animalsname,
doctors.email
from [event] inner join doctors on [event].username = doctors.username
inner join animals on doctors.id =animals.doctorid
where [event].username=lower(@username) and doctors.username =lower([event].username)
and [event].animalid= animals.id or [event].animalid=0

What i want is to only bring one row for the zero value.Like if i could use: (select top 1 from blah where [event].animalid=0). I thought i could set all values in a temp table and the try to select distinct id because p.e. in the above i get the animals.id columns(7,8,8,8,8,8,8,8) so as you see 8 (that is all the animals that came because of the zero value thus no animal id found for one to one relation) must be unique, so it should show simply 7,8.The problem here is that i would bring unnecessary animal columns(all the animals of the doctor).
I know that a FK for the event table would probably solve this but it's not my design + There are actually 2 databases united with some collation so i just merged it for you in a more simple SQL but the same would have happened anyhow.Event is on another database.
Any ideas?
Thanks.

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-10-30 : 06:03:51
Can you provide sample input data and also expected output?? Then we can able to find solution easily

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 09:53:41
One thing though unrelated

why do you have where condition like this?

where [event].username=lower(@username) and doctors.username =lower([event].username)


unless you use a case sensitive collation there's no need of using lower.


As for your requirement we dont get the exact issue you're explaining so would need some sample data and output as requested to understand what exactly you're trying to achieve

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

Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-30 : 21:59:15
I wasn't sure about the collation so i lowered.
Sample data.
As i've said 3 Tables Animals, Doctors, Event.
Sample:
anid id name animaln user mail [event.animalid]
1 8 junior little skent skent@test.com 1
1 9 TOK little skent skent@test.com 0
2 9 TOK tsalok skent skent@test.com 0
5 9 TOK livarna skent skent@test.com 0
7 9 TOK kolilki skent skent@test.com 0
173 9 TOK adservo skent skent@test.com 0

id is [event.id], just forgot to put it before,[event.animalid] is as i've said if the animal has an id or a zero.
That was what will show up with zero.

This is what i want:
anid id name animaln user mail [event.animalid]
1 8 junior little skent skent@test.com 1
1 9 TOK little skent skent@test.com 0

second row does not matter, could by any row that has zero event.id with id =9 but we don't need replicates for id 9.
Basically now that i see it.I just need to have a unique event.id's , that's the requirement.

Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-30 : 22:01:39
Sorry people, i am not sure how will make the data line correctly.
So first line is anid=1 id=8 name=junior animaln=little user=skent mail = skent@test.com [event.animalid]=1
Thanks.
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-30 : 22:24:02
hmm.I think i got it:


select animals.id as anid,[event].[name], animals.name as animalsname,
doctors.email
from [event] inner join doctors on [event].username = doctors.username
--inner join animals on doctors.id =animals.doctorid
left join animals on [event].animalid = animals.id
where [event].username=lower(@username) and doctors.username =lower([event].username)
and [event].animalid= animals.id or [event].animalid=0
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-30 : 23:23:54
Hi something additional to wrap this up.Now my animalname (if [event.animalid]=0) will display NULL.I want to change NULL to a value, so i am doing this:

select animals.id as anid,[event].id,[event].[name], animalname=
case animals.name
when null then 'No animal name'
else animals.name end,
[event].username,doctors.username ---etc

But i always get the else contition even if the animals.name is null.What am i doing wrong?
Thanks.
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-10-31 : 06:27:30

Try with this,

animalname= case when [event.animalid]=0 then 'No animal name'
else animals.name end,



--
Chandu
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-10-31 : 21:22:46
Got it, and thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-01 : 09:56:01
quote:
Originally posted by bandi


Try with this,

animalname= case when [event.animalid]=0 then 'No animal name'
else animals.name end,



--
Chandu


it should be [event].[animalid]

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

Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2012-11-02 : 20:59:36
Yes, no worries i've fixed it.
Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-03 : 22:40:37
Ok..Good

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

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-05 : 01:51:54
quote:
Originally posted by sapator

Got it, and thanks.




Welcome

--
Chandu
Go to Top of Page
   

- Advertisement -