简体   繁体   中英

Is it possible to select data from multiple rows for output in a single line?

I have 2 databases. I want to write a query that will pull data from both and tie them into the same result.

user database:

id  username  group
1   steve     group1
2   joe       group1
3   tom       group2

data database:

id  userid  fieldname   fieldresult
1   2       phone       867-5309
2   2       address     123 elm st
3   1       address     666 park avenue

If I just want steve's address, I could write:

select user.username, data.fieldresult from user, data where user.id = data.userid and data.fieldname = 'address' and user.username = 'steve';

The result would be:

username    fieldresult
steve       666 park avenue

But what if I want all of the fieldresults for joe in a single row? Is that possible to do from the query itself, or do I have to handle that in code?

Basically, I'd like to see:

username    phone       address
joe         867-5309    123 elm st

Is this wishful thinking? I wouldn't even need "phone" and "address" as the headers, as long as I could get their values in the same result.

NOTE: I'm working with an existing database and this is how the information is currently stored.

EDIT: Also, I need to do this on a much larger basis. Instead of querying by the username, can I get this information in the same way for each user in an entire group.

Try

select      a.username,
            b.fieldresult as phone,
            c.fieldresult as address
from        #user a
left join   #data b on a.id=b.userid and b.fieldname='phone'
left join   #data c on a.id=c.userid and c.fieldname='address'
where       a.id = 2

You can try pivoting contact info in datadb table and then join it with user

select * from userdb..[user] u join
 (
 select userid,phone, address from 
 (
 select userid,fieldname,fieldresult from datadb..data  
 ) as a pivot 
 (
  max(fieldresult) for fieldname in (phone, address)
 ) piv ) as a
on a.userid = u.id

this would result in something as follows.

id  username    group   userid  phone   address
1   steve       group1      1   NULL     park aven
2   joe         group1      2   867-5309    123 elm st

The two tables i used are user_ and data. Hope,this might help you

DECLARE
user_name user_.username%type:='joe';
phone_no data.fieldresult%type;
full_address data.fieldresult%type;
user_id user_.id%type;

CURSOR c_name IS select * from data;
result c_name%rowtype;

BEGIN

select id into user_id from user_ where username=user_name; 
open c_name;

LOOP

fetch c_name into result;

IF ( result.userid=user_id AND result.fieldname='phone') THEN
phone_no:=result.fieldresult;
END IF;  

IF(result.userid=user_id AND  result.fieldname='address') THEN
full_address:=result.fieldresult;
END IF;


EXIT when c_name%NOTFOUND;
END LOOP;
close c_name;

dbms_output.put_line(user_name||'       '||phone_no||'      '||full_address);

END;
/

OUTPUT: joe 867-5309 123 elm st

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM