| 订阅 | 在线投稿
分享
 
 
 

SQL Server中多行多列连接成为单行单列

2008-07-22 05:34:16 编辑來源:互联网 国际版 评论
 
 
本文为【SQL Server中多行多列连接成为单行单列】的汉字拼音对照版显示拼音
  yuanshijiegou

  Column1 Column2

  ----------- ----------

  1 A

  1 B

  2 C

  2 D

  2 E

  3 F

  chaxunxiaoguo

  Column1 Column2

  ----------- ------------------

  1 A,B

  2 C,D,E

  3 F

  jijiang Column1 xiangtongdexingde Column2 lianchengyilie

  buzhiruhemiaoshucizhongyongfashifoujuyouxiangjiaochabiaoxiangguande Cross-Table he Pivot zhileideyuedingchengshudezhuanyechengwei

  shifouyekeyichengweilingyizhong Cross-Table ?

  cixuqiuyinggaishichangjiandewangshangyeyouxuduoDEMOzhishi CSDN zhongpinfanyouxinshoutiwenxianjiandanshixianyigeDEMOyi便bianshenkao

  -- duoxingduolielianjiechengweidanxingdanlieshilixuyaoyigezidingyihanshu

  -- http://community.csdn.net/Expert/TopicView3.asp?id=5603231

  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[VertToHorzSample]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

  drop table [dbo].[VertToHorzSample]

  GO

  -- jianliceshishuju

  CREATE TABLE VertToHorzSample(

  Column1 int,

  Column2 varchar(100)

  )

  GO

  INSERT INTO VertToHorzSample(Column1, Column2)

  SELECT 1, 'A'

  UNION ALL

  SELECT 1, 'B'

  UNION ALL

  SELECT 2, 'C'

  UNION ALL

  SELECT 2, 'D'

  UNION ALL

  SELECT 2, 'E'

  UNION ALL

  SELECT 3, 'F'

  GO

  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ConvertVertToHorz]') and xtype in (N'FN', N'IF', N'TF'))

  drop function [dbo].[ConvertVertToHorz]

  GO

  -- jianlifuzhuhanshu

  CREATE FUNCTION ConvertVertToHorz(@Col1Val int)

  RETURNS VARCHAR(8000)

  AS

  BEGIN

  -- shijixiangmuzhongyinggaikaolv @RetVal shifouhuichaoguo 8000 gezifu

  DECLARE @RetVal varchar(8000)

  SET @RetVal = ''

  -- tongguodigui SELECT lianjiezhidingliecunchudaolinshibianliangzhong

  SELECT @RetVal = Column2 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val

  -- lianjieduolie

  -- SELECT @RetVal = Column2 + ',' + Column3 + ',' + Column4 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val

  -- qudiaoyibade , (douhao)

  IF LEN(@RetVal) > 0

  SET @RetVal = LEFT(@RetVal, LEN(@RetVal) - 1)

  --PRINT @RetVal

  RETURN @RetVal

  END

  GO

  -- ceshi

  SELECT Column1, dbo.ConvertVertToHorz(Column1) Column2 FROM (SELECT DISTINCT Column1 FROM VertToHorzSample) t

  /**//*

  Column1 Column2

  ----------- ------------------

  1 A,B

  2 C,D,E

  3 F

  */

  GO

  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[VertToHorzView]') and OBJECTPROPERTY(id, N'IsView') = 1)

  drop view [dbo].[VertToHorzView]

  GO

  -- keyijianliyigeshitu

  CREATE VIEW dbo.VertToHorzView

  AS

  SELECT Column1, dbo.ConvertVertToHorz(Column1) Column2

  FROM (SELECT DISTINCT Column1 FROM dbo.VertToHorzSample) t

  GO

  -- ceshishitu

  SELECT * FROM VertToHorzView

  /**//*

  Column1 Column2s

  ----------- -----------------

  1 A,B

  2 C,D,E

  3 F

  */原文
 
 
原始结构: Column1 Column2 ----------- ---------- 1 A 1 B 2 C 2 D 2 E 3 F 查询效果: Column1 Column2 ----------- ------------------ 1 A,B 2 C,D,E 3 F 即将 Column1 相同的行的 Column2 连成一列。 不知如何描述此种用法,是否具有像交叉表相关的 Cross-Table 和 Pivot 之类的约定成熟的专业称谓? 是否也可以称为另一种 Cross-Table ? 此需求应该是常见的,网上也有许多DEMO,只是 CSDN 中频繁有新手提问,现简单实现一个DEMO,以便参考。 -- 多行多列连接成为单行单列示例:需要一个自定义函数 -- [url=http://community.csdn.net/Expert/TopicView3.asp?id=5603231]http://community.csdn.net/Expert/TopicView3.asp?id=5603231[/url] if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[VertToHorzSample]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[VertToHorzSample] GO -- 建立测试数据 CREATE TABLE VertToHorzSample( Column1 int, Column2 varchar(100) ) GO INSERT INTO VertToHorzSample(Column1, Column2) SELECT 1, 'A' UNION ALL SELECT 1, 'B' UNION ALL SELECT 2, 'C' UNION ALL SELECT 2, 'D' UNION ALL SELECT 2, 'E' UNION ALL SELECT 3, 'F' GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ConvertVertToHorz]') and xtype in (N'FN', N'IF', N'TF')) drop function [dbo].[ConvertVertToHorz] GO -- 建立辅助函数 CREATE FUNCTION ConvertVertToHorz(@Col1Val int) RETURNS VARCHAR(8000) AS BEGIN -- 实际项目中,应该考虑 @RetVal 是否会超过 8000 个字符 DECLARE @RetVal varchar(8000) SET @RetVal = '' -- 通过递归 SELECT 连接指定列存储到临时变量中 SELECT @RetVal = Column2 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val -- 连接多列 -- SELECT @RetVal = Column2 + ',' + Column3 + ',' + Column4 + ',' + @RetVal FROM VertToHorzSample WHERE Column1 = @Col1Val -- 去掉尾巴的 , (逗号) IF LEN(@RetVal) > 0 SET @RetVal = LEFT(@RetVal, LEN(@RetVal) - 1) --PRINT @RetVal RETURN @RetVal END GO -- 测试 SELECT Column1, dbo.ConvertVertToHorz(Column1) Column2 FROM (SELECT DISTINCT Column1 FROM VertToHorzSample) t /**//* Column1 Column2 ----------- ------------------ 1 A,B 2 C,D,E 3 F */ GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[VertToHorzView]') and OBJECTPROPERTY(id, N'IsView') = 1) drop view [dbo].[VertToHorzView] GO -- 可以建立一个视图 CREATE VIEW dbo.VertToHorzView AS SELECT Column1, dbo.ConvertVertToHorz(Column1) Column2 FROM (SELECT DISTINCT Column1 FROM dbo.VertToHorzSample) t GO -- 测试视图 SELECT * FROM VertToHorzView /**//* Column1 Column2s ----------- ----------------- 1 A,B 2 C,D,E 3 F */
󰈣󰈤
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有