sql中我们偶尔会用到对数据进行合并,但其中的某一列数据要进行合并的操作: 如下图,一个用户有多个角色ID,如果我们想要统计一个用户有哪些角色,并且以单列的展现形式,单纯的用DISTINCT去掉肯定是不行的 我们可以用下面的sql语句进行拼接,对数据进行合并: SELECT g.UserID, (),f.RoleID) +'-' FROM ( SELECT UserID,RoleID FROM dbo.SC_User_Role) f WHERE f.UserID=g.UserID FOR XML
把一列数据拼接成一个字符串比较简单: declare @test varchar(500) set @test=''; select @test=@test+name+',' from person select @test 但是如果数据中有重复选项,想去掉重复的就比较绕弯了. ) set @test=''; select distinct @test=@test+name+',' from person select @test 加distinct是不行的,我在sql server 2008
SQL:数据库合并列数据:遇到一个更新的问题 想要把查询到的数据某一列拼接成字符串形式返回用的是SQL数据库中的STUFF函数比如 查询到的表(u_College)如下Id Name Age Classify1 张一 18 一班2 张二 17 二班3 张三 19 三班->SQL语句SELECT distinct Classify=STUFF((SELECT distinct ','+ Classify FROM u_College FOR XML PATH('')),1,1,'') FROM u
问题:test_table 表中有 a,b,c 三个字段,求根据字段a 去除重复数据,得到去重后的整行数据 根据mysql的经验尝试以下方法均失败 1.使用 distinct 关键字 (oracle查询数据中,不允许非 distinct 标注字段 ) select count(distinct a),a,b,c from test_table; 2.使用 group by (oracle查询数据中不允许非分组字段) select a,b,c from test_table group by a;
SQL中只有两列数据(字段1,字段2),将其相同字段1的行转列 转换前: 转换后: --测试数据 if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([MDF_LOT_NO] int,[ERP_MODE_CD] int) Insert #T , union all , union all , union all , Go --测试数据结束 DECLARE @name VARCHAR(max),@sql
Expression构建DataTable to Entity 映射委托 1 namespace Echofool.Utility.Common { 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Linq.Expressions; 6 using System.Reflection; 7 using System.Reflection.Emit; 8 9 publ
行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_center WITH ( NOLOCK ) WHERE CE_PROVINCE = '浙江省' ORDER BY ce_code , , '') AS jzhw FROM ap_center t) SELECT @center_JZHW 运行结果如下图,列CE_CODE用分隔符','输出 Function写