sql server怎么把多行数据合并成一行

2024-12-17 23:45:23
推荐回答(1个)
回答1:

group by 前面相同的字段,后面不同的,用max取。
SELECT m.member_id,
prefix_desc 'Prefix:',
UPPER(first_name) 'First Name:',
UPPER(last_name) 'Last Name:',
UPPER(email) 'Email:',
LOWER(user_id) 'User ID:',
REPLICATE('*',LEN(password)) 'Password:',
('****'+SUBSTRING(license_no,5,4)) 'Drivers Licese Number:',
REPLACE(email_ind,'Y','Accept') 'Email notification:',
REPLACE(return_ind,'Y','Accept') 'HERIZ E-RETURN',
max(CASE WHEN tel_type= 'B' THEN tel_number END + ' '+REPLACE(pref_phone_ind,'P','PREF')) 'Business Phone',
max('('+ p.country_code + ')'+ CASE WHEN tel_type= 'M' THEN tel_number END) 'Mobile Number',
max(CASE WHEN tel_type= 'F' THEN tel_number END) 'Fax'
FROM MEMBER m, license l, phone_fax p
WHERE m.member_id = l.member_id
AND m.member_id = p.member_id
AND m.member_id = 'A000001'
group by
m.member_id,
prefix_desc 'Prefix:',
UPPER(first_name) 'First Name:',
UPPER(last_name) 'Last Name:',
UPPER(email) 'Email:',
LOWER(user_id) 'User ID:',
REPLICATE('*',LEN(password)) 'Password:',
('****'+SUBSTRING(license_no,5,4)) 'Drivers Licese Number:',
REPLACE(email_ind,'Y','Accept') 'Email notification:',
REPLACE(return_ind,'Y','Accept') 'HERIZ E-RETURN'