sql 多行转换为一行】的更多相关文章

select 字段1, [val]=( select 字段2 +',' from 表名 as b where b.字段1 = a.字段1  for xml path('')) from 表名 as a group by a.字段1…
sql多行合并成一行 sql server SELECT [activityId], --STUFF( (SELECT ',' + Cast(A.phone AS varchar) FROM aaa A WHERE A.activityId=B.activityId FOR XML PATH('')) --,1,1,'') AS phonesFROM aaa BGROUP BY [activityId] mysql SELECTactivityId, group_concat(DISTINCT…
原文:http://www.cnblogs.com/sammon/archive/2012/05/10/2494362.html 测试表与测试数据 CREATE TABLE TestTitle ( name   VARCHAR(10), titleVARCHAR(10) ); INSERT INTO TestTitle VALUES ('张三', '程序员'); INSERT INTO TestTitle VALUES ('张三', '系统管理员'); INSERT INTO TestTitle…
有两个导数据的需求,1.一张表里面每一个订单号可能对应多条数据,每个单号返回时间最早的那条. 2.根据条件查询某个字段并按照逗号,合并在一起. 表类似结构如下: 第一条sql:select c.Id,c.OrderNum, c.CreateTime FROM (select t.*,row_number() over(partition by t.OrderNum order by t.CreateTime ) rn  from Payinfo t WHERE t.OrderNum IN('20…
--SELECT ff= stuff((select ','+cast(WorkOrderNo as varchar)-- FROM dbo.TB_WorkOrder c -- where tpl.ProjectID=c.ProjectID -- WHERE c.WorkOrderType=1-- for xml path('')),1,1,'')…
select stuff((select ',' + CONVERT(VARCHAR(50),id)+'' from tab_menu  group by id for xml  path('')),1,1,'')…
SELECT * FROM tbiz_ProjectRelation 目标统计每个项目有几条申请记录 Step1 SELECT ProjectID , RelationIDs , , '') FROM tbiz_ProjectRelation t1 Step2 SELECT ProjectID , RelationIDs , , '') FROM tbiz_ProjectRelation t1 GROUP BY ProjectID Step3 SELECT ProjectID , Relatio…
原始数据: 期望数据: IF OBJECT_ID('temp_20170701','u') IS NOT NULL DROP TABLE temp_20170701 CREATE TABLE temp_20170701 ( ID ,), NAME ), Subjectname ), Score INT ) INSERT dbo.temp_20170701( Name, subjectname, Score ) ' UNION ' UNION ' UNION ' UNION ' UNION ' U…
TDstats.udfConcat为Teradata自带UDF,定义如下: show function tdstats.udfconcat; REPLACE FUNCTION tdstats.UDFCONCAT (aVarchar ) CHARACTER SET UNICODE) ) CHARACTER SET UNICODE CLASS AGGREGATE () SPECIFIC udfConcat LANGUAGE C NO SQL NO EXTERNAL DATA PARAMETER ST…
ORACLE纯SQL实现多行合并一行[转] 项目中遇到一个需求,需要将多行合并为一行.表结构如下:NAME                            Null           Type------------------------ ---------   -----N_SEC_CODE                 NOT NULL CHAR(6)C_RESEARCHER_CODE    NOT NULL VARCHAR2(20) 此表保存了“股票”与“研究员”的对应关系数据…