SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problem with distinct rows on a databse.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sapator
Constraint Violating Yak Guru

Greece
362 Posts

Posted - 10/29/2012 :  23:58:46  Show Profile  Reply with Quote
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.

Edited by - sapator on 10/30/2012 00:01:47

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 10/30/2012 :  06:03:51  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 10/30/2012 :  09:53:41  Show Profile  Reply with Quote
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

Greece
362 Posts

Posted - 10/30/2012 :  21:59:15  Show Profile  Reply with Quote
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

Greece
362 Posts

Posted - 10/30/2012 :  22:01:39  Show Profile  Reply with Quote
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.

Edited by - sapator on 10/30/2012 22:02:37
Go to Top of Page

sapator
Constraint Violating Yak Guru

Greece
362 Posts

Posted - 10/30/2012 :  22:24:02  Show Profile  Reply with Quote
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

Greece
362 Posts

Posted - 10/30/2012 :  23:23:54  Show Profile  Reply with Quote
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.

Edited by - sapator on 10/30/2012 23:24:14
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 10/31/2012 :  06:27:30  Show Profile  Reply with Quote

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

Greece
362 Posts

Posted - 10/31/2012 :  21:22:46  Show Profile  Reply with Quote
Got it, and thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/01/2012 :  09:56:01  Show Profile  Reply with Quote
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

Greece
362 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/03/2012 :  22:40:37  Show Profile  Reply with Quote
Ok..Good

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

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 11/05/2012 :  01:51:54  Show Profile  Reply with Quote
quote:
Originally posted by sapator

Got it, and thanks.




Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000