‘壹’ sql怎么把多条数据合并成一条数据
把多条数据合并成一条数据的代码:
select sum(case when wgrp_id='2' then quota end) w2, sum(case when wgrp_id='3' ;then quota end) w3, mm;
from table;
group by mm。
SQL语言,是结构化查询语言(Structured Query Language)的简称。SQL语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
SQL语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统可以使用相同的结构化查询语言作为数据输入与管理的接口。SQL语言语句可以嵌套,这使他具有极大的灵活性和强大的功能。
应用信息:
结构化查询语言SQL(STRUCTURED QUERY LANGUAGE)是最重要的关系数据库操作语言,并且它的影响已经超出数据库领域,得到其他领域的重视和采用,如人工智能领域的数据检索,第四代软件开发工具中嵌入SQL的语言等。
支持标准:
SQL 是1986年10 月由美国国家标准局(ANSI)通过的数据库语言美国标准,接着,国际标准化组织(ISO)颁布了SQL正式国际标准。1989年4月,ISO提出了具有完整性特征的SQL89标准,1992年11月又公布了SQL92标准,在此标准中,把数据库分为三个级别:基本集、标准集和完全集。
‘贰’ sql统计问题,如果将如下sql合并成一条执行
selectf_date,
count(distinctuser_ip)asdayMount
fromm_webservice
wheref_date>='20170208'
andf_date<'20170215'
groupbyf_date
unionall
select'总数',count(distinctuser_ip)asweekMount
fromm_webservice
wheref_date>='20170208'
andf_date<'20170215';
‘叁’ SQL同一列同一行中的数据合并
----写了个函数如下:
GO
CREATE FUNCTION funTest(@Value nvarchar(1000))
RETURNS nvarchar(1000)
AS
BEGIN
DECLARE @rtn nvarchar(1000)
DECLARE @temp table(a varchar(1000))
declare @i int, @StrSeprate nvarchar(10)
set @strSeprate =';'
set @Value=rtrim(ltrim(@Value))
set @i=charindex(@StrSeprate,@Value)
while @i>=1
begin
insert @temp values(left(@Value,@i-1))
set @Value=substring(@Value,@i+1,len(@Value)-@i)
set @i=charindex(@StrSeprate,@Value)
end
if @Value<>''
insert @temp values(@Value)
declare @cur cursor,@a nvarchar(1000)
set @cur = cursor for
select a from @temp group by a ;
set @rtn='';
open @cur;
fetch next from @cur into @a;
while @@FETCH_STATUS =0
begin
if LEN(@rtn)>0 set @rtn = @rtn +';'
set @rtn =@rtn+@a
fetch next from @cur into @a;
end
RETURN @rtn
END
GO
--使用方法如下:
declare @V nvarchar(1000)
set @V = '5000000009;5000000009;5000000010;5000000011'
select dbo.funTest(@V)
-----------
5000000009;5000000010;5000000011
你可以select db.funTest(你的字段) as 输出 from 你的表。
‘肆’ SQL语句 表内数据合并
distinct 在 select 后
select distinct * from data;
‘伍’ SQL中如何合并数据库
我认为最好找软件供应商来做此项目服务,毕竟里面的表什么的不一定只是简单的数据合并就可以的,
‘陆’ SQL中怎么把多查询结果合并成一条数据
oralce 10g 函数 wm_concat
select k_n1,k_n2,k_n3,k_n4,wm_concat(k_n5) from k_t where k_n1=1 and k_n2=1 and k_n3=1 and k_n4=1
运行结果如下:
1 1 1 1 5,4,3
‘柒’ sql 表数据合并
我这里创建2个测试表 aa 与 bb, 还特地造了些其他的字段,用于模拟 楼主的 “有很多字段”
1> select * from aa;
2> select * from bb;
3> go
a b c
----------- ----------- -----------
1010 5 1
1011 6 2
1012 7 3
(3 行受影响)
a b c d
----------- ----------- ----------- -----------
1011 6 4 7
1012 9 5 8
1013 8 6 9
(3 行受影响)
1> SELECT
2> isnull(aa.a, bb.a) AS a,
3> isnull(aa.b, bb.b) AS b,
4> isnull(aa.c, bb.c) AS 其他数据C,
5> bb.d AS 其他数据D
6> FROM
7> aa full join bb on (aa.a = bb.a AND aa.b = bb.b)
8> go
a b 其他数据C 其他数据D
----------- ----------- ----------- -----------
1010 5 1 NULL
1011 6 2 7
1012 7 3 NULL
1012 9 5 8
1013 8 6 9
(5 行受影响)
‘捌’ sql中数据条目合并,自动运行,求平均值,if语句
decalre @RecordCount interger
declare @Date99 smalldatetime
declare @avg1 numeric(18,3)
declare @avg2 numeric(18,3)
if day(getdate())=1
begin
select @recordcount=count(*) from table1
if @recordcount >200
begin
select @avg1=avg(col1),@avg2=avg(col2) from table where id in (select top 100 ID from table1)
select @date99=date1 from table1 where id in (select top 1 id from table1 where id not in (select top 98 id from table1 order by id) order by id)
end
end
--比较蛋疼的要求
‘玖’ SQL中如何按照条件合并多行的数据
写一个函数,运行sql调用函数,不知道你数据库字段名只是设前两个字段为id1,id2,这是我的思路 在我的数据库测试过好用
create function pmerg(@id1 int ,@id2 int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s+';'+name from y where id1=@id1 and id2=@id2
set @s=right(@s,len(@s)-1)
return(@s)
End
上面是建立的函数,下面是要运行的sql
select distinct id1, id2, dbo.pmerg(id1,id2) from y