Redian新闻
>
T-SQL Row Concatenate with a Twist??
avatar
T-SQL Row Concatenate with a Twist??# Database - 数据库
i*a
1
have a table in the following data:
ContactID____Type____Value
1**********************[email protected]
123_________phone___555-555-5555
123_________fax_____888-888-8888
how do I do a select statment to get it into 1 result line like this:
ContactID, ContactEmail, ContactPhone, ContactFax
123, a*[email protected], 555-555-5555, 888-888-8888
I am using
select
case type
when 'email' then Value AS ContactEmail
end,
case type
when 'phone' then Value AS ContactPhone
end,
case type
when 'fax' then Value AS ContactFax
avatar
B*g
2
打到google。
I perfer the xml one.
http://www.projectdmx.com/tsql/rowconcatenate.aspx

【在 i****a 的大作中提到】
: have a table in the following data:
: ContactID____Type____Value
: 1**********************[email protected]
: 123_________phone___555-555-5555
: 123_________fax_____888-888-8888
: how do I do a select statment to get it into 1 result line like this:
: ContactID, ContactEmail, ContactPhone, ContactFax
: 123, a*[email protected], 555-555-5555, 888-888-8888
: I am using
: select

avatar
j*n
3
Use PIVOT in SQL Server 2005 or upper versions...
;WITH VTable (ContactID, [Type], [Value])
AS
(
SELECT 123, 'email', 'a*[email protected]'
UNION ALL
SELECT 123,'phone', '555-555-5555'
UNION ALL
SELECT 123,'fax', '888-888-8888'
UNION ALL
SELECT 456, 'email', 'c*[email protected]'
UNION ALL
SELECT 456,'phone', '444-555-5555'
UNION ALL
SELECT 456,'fax', '444-888-8888'
)
SELECT ContactID, [email] AS Email, [phone] AS Phone, [fax] AS Fax
FROM ( SELECT ContactID, [Type], [Value]
FROM VTable ) P
PIVOT
( MAX([

【在 B*****g 的大作中提到】
: 打到google。
: I perfer the xml one.
: http://www.projectdmx.com/tsql/rowconcatenate.aspx

avatar
c*y
4
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Play_test](
[ContactID] [varchar](50) NULL,
[type] [varchar](50) NULL,
[Value] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
avatar
c*y
5
ContactID email
fax
phone
avatar
c*y
6
2 lou, 3 lou shi da niu!
avatar
i*a
7
indeed thank you big cows
I googled and read the projectdmx article, just didn't understand how to
apply it so I can get seperate columns.
avatar
i*a
8
that's a smart way of using case and max. thx

【在 c*****y 的大作中提到】
: SET ANSI_NULLS ON
: GO
: SET QUOTED_IDENTIFIER ON
: GO
: SET ANSI_PADDING ON
: GO
: CREATE TABLE [dbo].[Play_test](
: [ContactID] [varchar](50) NULL,
: [type] [varchar](50) NULL,
: [Value] [varchar](50) NULL

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。