将多行(具有相同 ID)合并为一行?
Merge multiple rows (with same ID) into one?
我希望将多行合并为具有匹配 ID 的一行。我正在使用 Teradata SQL。
示例:
SELECT
id_nbr AS ID,
contact_type AS contype,
contact_first_name AS firstName,
contact_last_name AS lastName,
contact_phone_number AS phoneNumber,
contact_address AS address,
contact_email AS email
FROM database.account_info
WHERE (contact_type = 'AAA' OR contact_type = 'BBB' OR contact_type = 'CCC');
结果:
ID contype firstName lastName phoneNumber address email
111111111 AAA bob smith (999)999-9999 1 Main St null
111111111 BBB bob smith (888)888-8888 1 random rd bob@random.com
111111111 CCC bob smith null null bob@anotherrandom.com
我想要的:
ID contypeAAA firstNameAAA lastNameAAA phoneNumberAAA addressAAA emailAAA contypeBBB firstNameBBB lastNameBBB phoneNumberBBB addressBBB emailBBB contypeCCC firstNameCCC lastNameCCC phoneNumberCCC addressCCC emailCCC
111111111 AAA bob smith (999)999-9999 1 Main St null ......etc
这可能吗?这将极大地帮助我..!提前致谢!
编辑这个,ID 的contype 并不总是具有所有3 个contype。这使它变得更加棘手。例如,ID:99999999 可以有 contype:'AAA','BBB' 但是应该 return 所有 'CCC' 值的空值,因为它不存在。
我相信这会奏效(我在 Oracle 11g 而不是 Teradata 上测试过):
SELECT *
FROM (SELECT id_nbr AS ID,
contact_type AS contype,
contact_first_name AS firstName,
contact_last_name AS lastName,
contact_phone_number AS phoneNumber,
contact_address AS address,
contact_email AS email
FROM database.account_info
WHERE contact_type in ('AAA', 'BBB', 'CCC')
)
PIVOT (max(firstName) AS firstName,
max(lastName) AS lastName,
max(phoneNumber) AS phone,
max(email) AS email,
max(address) AS address
FOR contype IN ('AAA', 'BBB', 'CCC')
)
最后一行可能需要一个别名,其中之一:
) AS derived_pivot
) derived_pivot
我希望将多行合并为具有匹配 ID 的一行。我正在使用 Teradata SQL。 示例:
SELECT
id_nbr AS ID,
contact_type AS contype,
contact_first_name AS firstName,
contact_last_name AS lastName,
contact_phone_number AS phoneNumber,
contact_address AS address,
contact_email AS email
FROM database.account_info
WHERE (contact_type = 'AAA' OR contact_type = 'BBB' OR contact_type = 'CCC');
结果:
ID contype firstName lastName phoneNumber address email
111111111 AAA bob smith (999)999-9999 1 Main St null
111111111 BBB bob smith (888)888-8888 1 random rd bob@random.com
111111111 CCC bob smith null null bob@anotherrandom.com
我想要的:
ID contypeAAA firstNameAAA lastNameAAA phoneNumberAAA addressAAA emailAAA contypeBBB firstNameBBB lastNameBBB phoneNumberBBB addressBBB emailBBB contypeCCC firstNameCCC lastNameCCC phoneNumberCCC addressCCC emailCCC
111111111 AAA bob smith (999)999-9999 1 Main St null ......etc
这可能吗?这将极大地帮助我..!提前致谢!
编辑这个,ID 的contype 并不总是具有所有3 个contype。这使它变得更加棘手。例如,ID:99999999 可以有 contype:'AAA','BBB' 但是应该 return 所有 'CCC' 值的空值,因为它不存在。
我相信这会奏效(我在 Oracle 11g 而不是 Teradata 上测试过):
SELECT *
FROM (SELECT id_nbr AS ID,
contact_type AS contype,
contact_first_name AS firstName,
contact_last_name AS lastName,
contact_phone_number AS phoneNumber,
contact_address AS address,
contact_email AS email
FROM database.account_info
WHERE contact_type in ('AAA', 'BBB', 'CCC')
)
PIVOT (max(firstName) AS firstName,
max(lastName) AS lastName,
max(phoneNumber) AS phone,
max(email) AS email,
max(address) AS address
FOR contype IN ('AAA', 'BBB', 'CCC')
)
最后一行可能需要一个别名,其中之一:
) AS derived_pivot
) derived_pivot