有表tb, 如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳
描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
create function dbo.f_str(@id varchar(10)) returns varchar(1000)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
return @str
end
go
--调用函数
select id , value = dbo.f_str(id) from tb group by id
drop function dbo.f_str
drop table tb
--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id
drop table tb
--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor
select * from @t
drop table tb
问题: 最近因为项目原因,本来用的是SQL server 2017,需要降级到SQL server 2014. 我的电脑是surface book2 ,由于高分辨率的原因,导致SSMS的显示存在bug,这里给出解决的办法。 这张图片是参考链接里面的,因为...
题目:找出没有销售GEO POSITIONING SYSTEM的部门 标准解: SELECT deptname FROM qdept where deptname not in (select deptname from qsale where itemname = 'Geo Positioning System') ...
1. 修改注册表,添加以下键值(编辑文本文件,填入以下内容,修改后缀为reg,双击执行)Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SideBySide] ...
Java图书管理系统 设计人:wangyunpeng_bio 项目需求 随着计算机的普及和应用水平的提高,经过考察比较,决定利用...该系统要解决的图书管理所要解决的问题,可以满足图书管理基本要求,包括添加、管理等功能。...
要求查询各个课程的最高分、第二高分、最高分人数,查询的结果有5个属性,分别为cid, name, max_score, max_score2, max_score_count。此题主要有3种思想,逐条陈述之。 (1)第一种方式查询代码如下: select cid,...
求解决方案。
现在买到的电脑多是高分屏电脑,但很多软件都是不适配高分屏的,因而软件界面会变得相当模糊,解决方法如下: 捕获.PNG 右键点击显示设置; 调整大小.PNG 调整软件大小为100%;(原为125%,显然不...
convert(varchar,a.TF_CK_TIME,120) as "date1", convert(varchar(120),a.TF_CK_TIME,120) as "date1", convert(datetime,a.TF_CK_TIME) as "date1", 等都试过了 无效 ... 球球大神 帮忙解决高分相赠
SQL Server Management Studio (SSMS) is getting better and better with each new release but Microsoft seems to ignore one big issue affecting a group of users that grows rapidly as the technology m...
SQL codehttp://blog.csdn.net/picture_1230/archive/2008/02/25/2118217.aspx PL/SQL can do it, it might look like:create or replace function dump_csv( p_query in var...
大家好,我是 Rocky0429,一个最近...几天过去了,我有点懵逼,我发现身为蒟蒻的我不知道我写的 SQL 是不是丝滑,这种练习让我产生了一种没有丝毫提升的迷茫,问题是还得自己建个表,想想数据集,真的是太麻烦了。...
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver())... connection1=DriverManager.getConnection(url1,user,passwor
例表(user_test_result)如下: id uid score 1 user1 80 2 user1 70 3 user2 100 4 user1 80 ...SQL语句 select t.* from `user_test_result` t where score=( ...
如题,直接上代码 CREATE VIEW [dbo].[View_GEnterMonitoringSummary] AS SELECT xx,xx,xx,xx,xx FROM(SELECT bi.DirectID AS xx, bi.BID AS xx, bi.MYear AS xx, sd.MMonth AS xx, bi.Province AS xx, ...
其中,WikiSQL、Spider等大规模标注数据集进一步指出了该技术目前面临的挑战:泛化性(跨领域)、复杂性(SQL语法)、正确性(问题和表格的对齐关系),也促进了一系列后续算法的研究与系统的开发。 在这里,我们给...
实验2 使用T-SQL编写程序 【实验目的】 1)掌握常用函数的使用方法。 2)掌握流程控制语句的使用方法。 【实验环境】 SQL Server 2012 Express(或SQL Server 2017 Express) 【实验重点及难点】 1)应用...
SQL牛逼函数走起来2.1 Step12.2 SQL实现12.3 Step22.4 SQL实现22.5 补充-collect函数2.5.1 SQL语句3 头条面试SQL题3.1&nbs...
C#入门必看含有100个例字,每个例子都是针对C#的学习关键知识点设计的,是学习C#必须知道的一些程序例子,分享给大家,需要的可以下载
十个c语言案例 (1)贪吃蛇 (2)五子棋游戏 (3)电话薄管理系统 (4)计算器 (5)万年历 (6)电子表 (7)客户端和服务器通信 (8)潜艇大战游戏 (9)鼠标器程序 (10)手机通讯录系统
非常漂亮的100个前端案例,包含幻灯片切换、图片缩放、相册、放大镜、图片拖着滚动等等,只有你想不到。