① 将sql里相同ID的数据放在一起用什么语句
create function pj(@id int)
returns varchar(200)
as
begin
declare @tfbm varchar(200)
select @tfbm isnull(@tfbm,'') + ',' from table
where id = @id
return left(@tfbm,len(@tfbm) - 1)
end
go
select k.id,dbo.pj(k.id) from (
select id from table
group by id) k
试试看行不行,具体思路就是:
1、创建函数,将一个ID的所有tfbm值拼接在一起,并返回
2、将数据按照ID分组,使每个ID都唯一,然后查询,在查询时,调用函数,将其当做列,传ID即可
② SQL怎么将2张表查询出来的相同字段合并显示在一个字段中.
条件:A表有a.1,a.2,a.3,c四个字段;B表有b.1,b.2,b.3,c四个字段。
要求:SQL将2张表查询出来的相同字段合并显示在一个字段中.
答案:select a.* from A as a,b.* from B as b where a.c=b.c
③ SQL 两张表合并 (两张表的列都相同)
可以参考下面的方法:
1、第一种,用一张临时表,把所有数据都放在一张表里
INSERT INTO QunList SELECT * FROM QunList89,90,91,92;
再在QunList中查询
2、第二种,用 UNION ALL方式查询
SELECT * FROM QunList89
UNION ALL
SELECT * FROM QunList90;
(3)sql将表里重复的类别放在一起扩展阅读:
SQL参考语句
删除表
drop table tabname--这是将表连同表中信息一起删除但是日志文件中会有记录
删除信息
delete from table_name-这是将表中信息删除但是会保留这个表
增加列
Alter table table_name add column_name column_type [default 默认值]--在表中增加一列,[]内的内容为可选项
④ 如何利用SQL语句将多个工作表中具有相同属性的数据汇总起来
将多个表中的数据汇总最常用的办法是使用union all,具体的要视表结构、“相同属性”的具体定义、汇总方式等因素而定,可能还要用到连接、筛选、子查询、分组等手段,当然聚合函数sum是少不了的。
下面例子实现将t1,t2,t3中的各个产品汇总起来,显示每种产品的总量:
select t.proct ,sum(t.qty) from
(select proct,qty from t1
union all
select proct,qty from t2
union all
select proct,qty from t3) t
group by t.proct;
⑤ 我想将sql server数据表里相同ID的值都放在一起,要怎么写代码
可以这样做:
select
*
into
tableclass0
from
table
where
班级=0
select
*
into
tableclass1
from
table
where
班级=1
select
*
into
tableclass2
from
table
where
班级=2
select
*
into
tableclass3
from
table
where
班级=3
...
select
*
into
tableclass9
from
table
where
班级=9
在把原先的表删除,就实现了1张表分成10张的,只要条件对就可以了,tableclass0-9是新表的名称,按你的表结构,可以不用导入id
⑥ SQL连接查询两表后需要知道其中一个字段重新的重复次数并将重复的行放在一起
你的state是放在一个字段,还是要放在很多字段?如果是放在一个字段,如果你的数据库是oracle的话,那么就比较容易了。
select A.name,A.id,C.state from A,
(select id,wmsys.wm_concat(state) state from B group by id) C where a.id=C.id
如果不是oracle,那么我就不清楚了,sqlserver可能要用到stuff函数来做。
⑦ SQL查询,如何把具有相同类别号的几个数据合并起来呢
select max(字段1) as 字段1,max(字段2) as 字段2,类别 from table group by 类别
这样不行吗? 亲
⑧ SQL 如何将一个表中的两条或多条拥有相同ID的记录合并为一条
一、创建表:
create table stuUnion
(
sid int identity primary key,
cid int,
id varchar(500)
)
二、添加数据:
insert into stuUnion
elect 1,'a' union
select 1,'b' union
select 2,'c' union
select 2,'d' union
select 3,'e' union
select 3,'f' union
select 3,'g'
三、用标量函数查询:
创建标量函数:
create function b(@cid int)
returns varchar(500)
as
begin
declare @s varchar(500)
select @s=isnull(@s+'','')+rtrim(id)+',' from stuUnion where cid=@cid
return @s
end;
用标量函数查询:
select cid,dbo.b(cid) as id from stuUnion group by cid
用sqlserver的xml:
select cid,ID=STUFF((select ' '+rtrim(id)+',' from stuUnion where st.cid=cid order by id for XML path('')),1,1,'') from stuUnion st group by cid
⑨ sql如何在多个表中的数据分类汇总到一个表里
create proc Mypro
as
begin
declare @t table(个人编号 varchar(10),工资 money,养老保险 money,年金 money,住房公积金 money)
insert into @t(个人编号,工资,养老保险,年金) select * from 表一
insert into @t select * from 表二
insert into @t select * from 表三
delete from @t where
exists(select * from @t where exists(select 个人编号,工资,养老保险,年金 from @t group by 个人编号,工资,养老保险,年金 having Count(*)>1) and 住房公积金 is null)
declare @t1 table(个人编号 varchar(10),工资 money,养老保险 money,年金 money,住房公积金 money)
insert into @t1 select distinct * from @t
select 个人编号,
sum(工资) as工资总和,avg(工资) as工资平均值,
sum(养老保险) as 养老保险总和,avg(养老保险) as 养老保险平均值,
sum(年金) as 年金总和,avg(年金) as 年金平均值,
sum(住房公积金) as 住房公积金总和,avg(住房公积金) as 住房公积金平均值
from @t1 group by 个人编号
end
--存储过程直接查询出你想要的数据,因为不知道你要总汇总和还是总汇平均值,你自己改下
--然后执行 insert into 表四 exec Mypro 插入你的表四吧