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)
 Stuck on Select Statement

Author  Topic 

nic
Posting Yak Master

209 Posts

Posted - 2002-08-15 : 14:50:41
Hi, I have a single to many relationship that I need to display in a single row. For example Joe has three types of accounts. (Short, Medium and Long). I need to return a result set that has the user information for John and the accounts he has in a single row. Something like this:

username, email, Account Long, Account Short, Account Medium
joe, joe@email.com, true, true, true
mary, mary@email.com, false, true, true

I redesigned our company's database structure (they used to add columns for each new account), but in one report they still want to view the data in one line I'm not really sure how to write the query. Any help would be appreciated.

CREATE TABLE tblUser (
user_key int,
user_name varchar(50),
user_email varchar(50)
)
GO

CREATE TABLE tblAccount (
account_key int,
account_name varchar(50)
)
GO

CREATE TABLE tblUserAccount (
user_key int,
account_key int
)
GO

INSERT INTO tblUser (user_key,user_name,user_email) VALUES (1,'Joe','joe@email.com')
GO
INSERT INTO tblUser (user_key,user_name,user_email) VALUES (2,'Mary','mary@email.com')
GO
INSERT INTO tblAccount (account_key,account_name) VALUES (1,'Long Term Account')
GO
INSERT INTO tblAccount (account_key,account_name) VALUES (2,'Medium Term Account')
GO
INSERT INTO tblAccount (account_key,account_name) VALUES (3,'Short Term Account')
GO
INSERT INTO tblUserAccount (user_key,account_key) VALUES (1,1)
GO
INSERT INTO tblUserAccount (user_key,account_key) VALUES (1,2)
GO
INSERT INTO tblUserAccount (user_key,account_key) VALUES (1,3)
GO
INSERT INTO tblUserAccount (user_key,account_key) VALUES (2,2)
GO
INSERT INTO tblUserAccount (user_key,account_key) VALUES (2,3)
GO

Nic

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-08-15 : 15:04:17

select
u.user_name,
u.user_email,
case
when max(long.account_key) is not null then 'true'
else 'false'
end as [Account Long],
case
when max(short.account_key) is not null then 'true'
else 'false'
end as [Account Short],
case
when max(med.account_key) is not null then 'true'
else 'false'
end as [Account Medium]
from
dbo.tblUser u
left join dbo.tblUserAccount ua
on u.user_key = ua.user_key
left join dbo.tblAccount long
on (ua.account_key = long.account_key and
long.account_name = 'Long Term Account')
left join dbo.tblAccount med
on (ua.account_key = med.account_key and
med.account_name = 'Medium Term Account')
left join dbo.tblAccount short
on (ua.account_key = short.account_key and
short.account_name = 'Short Term Account')
group by
u.user_name,
u.user_email


 
EDIT: if you have a constraint such that there is only one of an account type per user, then you can remove the aggregates

Jay White
{0}

Edited by - Page47 on 08/15/2002 15:08:11
Go to Top of Page

nic
Posting Yak Master

209 Posts

Posted - 2002-08-15 : 15:24:31
wow! thanks a lot.

Nic
Go to Top of Page
   

- Advertisement -