由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - T-SQL Row Concatenate with a Twist??
相关主题
PIVOT question问个SQL问题
better solution for cross table query in sql?问一个SQL Server的问题
请问sql语句能不能实现这样的功能求教个MS SQL的问题
error of executing SQL query of string concatenation (转载SQL 请教
请帮忙读懂这个sql script请教一个问题
One Q for DB expertPIVOT TABLE
Like 和 equal 啥区别?PIVOT, 请大拿,帮我debug
Help on Oracle Query请帮我查一段pivot code 错在哪里?
相关话题的讨论汇总
话题: contactid话题: value话题: select话题: fax话题: type
进入Database版参与讨论
1 (共1页)
i****a
发帖数: 36252
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
B*****g
发帖数: 34098
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

j*****n
发帖数: 1781
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

c*****y
发帖数: 75
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
c*****y
发帖数: 75
5
ContactID email
fax
phone
c*****y
发帖数: 75
6
2 lou, 3 lou shi da niu!
i****a
发帖数: 36252
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.
i****a
发帖数: 36252
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

1 (共1页)
进入Database版参与讨论
相关主题
请帮我查一段pivot code 错在哪里?请帮忙读懂这个sql script
question on JOIN on OracleOne Q for DB expert
一个看似简单的sql语句Like 和 equal 啥区别?
求教:Help on Oracle Query
PIVOT question问个SQL问题
better solution for cross table query in sql?问一个SQL Server的问题
请问sql语句能不能实现这样的功能求教个MS SQL的问题
error of executing SQL query of string concatenation (转载SQL 请教
相关话题的讨论汇总
话题: contactid话题: value话题: select话题: fax话题: type