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)
 sql server query puzzle

Author  Topic 

Trent
Starting Member

39 Posts

Posted - 2004-08-02 : 14:35:12
I need to create a query where I pass it 'Person' and I create a column called 'There'. I'm using SQL Server 2000.

------------------ -----------
Table X Table Y
------------------ -----------
ID Desc Person ID
------------------ -----------
1 Glasses 856 1
2 Red Hair 856 3
3 Blue Eyes 900 1
900 2
900 3

-------------------------------
Needed Result when I pass '856'
-------------------------------
Desc There
-----------------------
Glasses True
Red Hair False
Blue Eyes True

This means that I must always output every possible 'Desc' and set 'There' to 'True' or 'False' depending on if there is a match for the passed 'Person'.
Thank You in advance for your help!

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-08-02 : 15:15:46
Here are a couple of ways:


select x.[desc],case when count(d.id) > 1 then 'True' else 'False' end as There
from
(
select id
from x
union all
select id from y
where person = 856
) d
join x on x.id = d.id
group by d.id,x.[desc]

drop table dbo.x,dbo.y
drop function dbo.IsThere

OR

create function dbo.IsThere
(
@person int,
@id int
)
returns varchar(5)
as
begin
declare @result varchar(5)
set @result = 'False'

if exists(select 1 from dbo.y where person = @person and id = @id)
set @result = 'True'

return @result
end
go

select
[desc],
dbo.IsThere(856,id) There
from dbo.x
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2004-08-02 : 15:37:45
Thank You!
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2004-08-02 : 17:22:55
Still can't quite get it. Here's more detail.

tTesterUsage Table
------------------
TesterID | UsageID
------------------
856 | 97
856 | 98
900 | 97
900 | 102


tLuUsage Table
--------------------
UsageID | UsageIndex
--------------------
97 | 60001
98 | 60002
99 | 60003
100 | 60004
101 | 60005
102 | 60006

tUConsumer Table
---------------------------
CMCode | GeneralDescription
---------------------------
60001 | Reads Books
60002 | Reads Magazines
60003 | Reads Newspaper
60004 | Watches TV
60005 | Rents Movies
60006 | Movie Theatre

******************************************
* tLuUsage.UsageIndex = tUConsumer.CMCode*
******************************************

Desired Result Below when TesterID = '856':
-----------------------------
GeneralDescription | There
-----------------------------
Reads Books | True
Reads Magazines | True
Reads NewsPaper | False
Watches TV | False
Rents Movies | False
Movie Theatre | False


I've tried this below, still doesn't quite do it.

CREATE PROCEDURE usp_tp_GetConsUsage
@TesterID char(7)
AS
declare @Out table (There varchar(5), TesterID char(7))
insert into @Out
SELECT tUConsumer.CMCode as There, tTesterUsage.TesterID
FROM tTesterUsage inner JOIN
tLuUsage ON tTesterUsage.UsageID = tLuUsage.UsageID inner JOIN
tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode
where tTesterUsage.TesterID = @TesterID

SELECT distinct tUConsumer.GeneralDescription,
case when Gonz.There is null then 'False' else 'True' end as There
FROM tLuUsage inner JOIN
tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode inner join
tTesterUsage on tLuUsage.UsageID = tTesterUsage.UsageID left join
@Out Gonz on tTesterUsage.TesterID = Gonz.TesterID
GO
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2004-08-02 : 17:51:11
scratch this: sorry be back with better results...

- RoLY roLLs
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2004-08-02 : 18:23:17
keeping ehorns code, i'd think this would work...no?

keeping his function:
create function dbo.IsThere
(
@TesterID int,
@CMCode int
)
returns varchar(5)
as
begin
declare @result varchar(5)
set @result = 'False'

if exists(select 1 from dbo.tTesterUsage inner join tTesterUsage on tLuUsage.UsageID = tTesterUsage.UsageID where tTesterUsage.TesterID = @TesterID and tLuUsage.UsageIndex = @CMCode)
set @result = 'True'

return @result
end
go


and your sproc would be:
create procedure usp_tp_GetConsUsage
@TesterID char(7)
AS
select
GeneralDescription,
dbo.IsThere(@TesterID,CMCode) as There
from
dbo.tUConsumer
GO


Not tested, but if you provide DLL, and DML. testing before posting can help us help you better

- RoLY roLLs
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2004-08-03 : 10:51:07
Tried it, it gives me this error when I try to create the UDF.

Tables or functions 'dbo.tTesterUsage' and 'tTesterUsage' have the same exposed names. Use correlation names to distinguish them.

Having trouble with the correlation names. Not even sure why I would need them here.
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2004-08-03 : 11:06:05
Ok, I got the details worked out. Thank you very much for pointing me in the right direction.
Let me know if this is one of those places that awards points.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-08-03 : 11:22:16
Don't use a UDF for this -- use a join:


select GeneralDescription,
case when a.UsageIndex is null then 'False' else 'True' end as There
from
tUConsumer
left outer join
(select distinct tLuUsage.UsageIndex
from
tLuUsage
inner join
tTesterUsage
on
tLuUsage.UsageID = tLuUsage.UsageID
where
tTesterUsage.TesterID = @TesterID
) a
on
tUConsumer.UsageIndex = a.UsageIndex



- Jeff
Go to Top of Page
   

- Advertisement -