㈠ sql 将一个字段的数据分列显示
SQL2000不支持开窗函数row_number() ,实现这种效果可以借助存储过程。
CREATE PROCEDURE 存储过程2
AS
set nocount on
/* 创建一个临时表,利用identity 添加一个从1开始的连续标识列 */
/* x字段我设置为变长字符串型,请更改为与原始字段类型相同 */
create table #temp (id int identity,X varchar(50))
/* 将原始表中数据插入临时表 */
insert into #temp(x) select colName from tableName order by colName
/* 输出希望得到列表效果 */
/* 思路:id除4求模数根据结果将X值分别放到字段a,b,c */
/* 用(case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)将每4条记录标识为一组 */
/* 最后通过求分组最大值得办法得到最终列表 */
select
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 1 then x else null end) as a,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 2 then x else null end) as b,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 3 then x else null end) as c,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 0 then x else null end) as d
from #temp
group by (case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)
set nocount off
上面代码源自下面存储过程,相对容易理解,但是由于过程里使用多一次操作查询(update)其效率也许会慢一些(但是本人未证实)
CREATE PROCEDURE 存储过程1
AS
set nocount on
create table #temp (id int identity,X nvarchar(50),idd int, flag smallint)
insert into #temp(x) select colName from tableName order by colName
update #temp set flag=case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end,
idd=case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end
select
max(case flag when 1 then x else null end) as a,
max(case flag when 2 then x else null end) as b,
max(case flag when 3 then x else null end) as c,
max(case flag when 0 then x else null end) as d
from #temp group by idd
set nocount off
㈡ sql 分列多行
参考以下sql
--测试一
--'abc|efg|hij|klm'参数一:截取的字符串str
--'|'参数二:在截取的字符串str中查询目标字符x
--1参数三:出现的位次n
--截取str中x出现n次之前的所有字符
--截取'abc|efg|hij|klm'中第一个'|'之前的所有字符abc
SELECTSUBSTRING_INDEX('abc|efg|hij|klm','|',1)
--测试二
--'abc|efg|hij|klm'参数一:截取的字符串str
--'|'参数二:在截取的字符串str中查询目标字符x
---1参数三:当为负数时,则表示倒序的位次
--截取str后中x出现从后往前数n次之后的所有字符
--截取'abc|efg|hij|klm'中从后往前数第一个'|'之后的所有字符klm
SELECTSUBSTRING_INDEX('abc|efg|hij|klm','|',-1)
㈢ SQL语句怎样使查询同一表同一列多条数据分列显示(在线等)
select编码,名称,sum(金额),sum(数量)from表1groupby编码,名称
㈣ sql查询中,如何将某列 分成 两列。
SELECT PAccM33g02,
CASE PAccM33g02
WHEN 0 THEN PAccM33g02 END PAccM33g02_J,
CASE PAccM33g02
WHEN 1 THEN PAccM33g02 END PAccM33g02_C
FROM PAccM3307
㈤ sql语句怎么样一次性查询多个条件,并分列显示
方法一,分别查询出来,结果再关联
selectfnum1,fnum2from
(selectcount(*)asfnum1from表名wherea=2andb=3)t1,
(selectcount(*)asfnum2from表名wherea=3andb=5)t2
方法二
selectsum(casewhena=2andb=3then1else0end)asfnum1,
sum(casewhena=3andb=5then1else0end)asfnum2
from表名
wherea=2andb=3
ora=3andb=5
有问题请追问
㈥ sql 如何批量分列
你参考下:
--模拟数据
create table #yourtable(ID int,Content varchar(4000))
insert into #yourtable(ID,Content)
select 1,'22|5000|3000'
union all select 2,'1|35|200|2'
union all select 3,'802|22'
union all select 4,'213|354|2002|22|500'
--实际操作SQL
declare @sql nvarchar(4000),@i int
set @i=1
while exists(select 1 from #yourtable where Content<>'')
begin
set @sql='alter table #yourtable add Data'+convert(varchar,@i)+' int'
exec(@sql)
set @sql='declare @loc int update #yourtable set @loc=charindex(''|'',Content),Data'
+convert(varchar,@i)+'=convert(int,case @loc when 0 then Content else '
+'substring(Content,1,@loc-1) end),Content=case @loc when 0 then '''' else '
+'substring(Content,@loc+1,len(Content)-@loc) end where Content<>'''''
exec(@sql)
set @i=@i+1
end
select * from #yourtable
--删除演示数据
drop table #yourtable
--结果
ID Data1 Data2 Data3 Data4 Data5
----------- ----------- ----------- ----------- ----------- -----------
1 22 5000 3000 NULL NULL
2 1 35 200 2 NULL
3 802 22 NULL NULL NULL
4 213 354 2002 22 500
㈦ SQL根据字段内容进行分列,sql语句怎么写
这种不好弄。。
--
select id,replace(col,'2015-','|2015-') from table_name;
用得到的这个结果,根据|这个分隔符重新导入一个新表,之后就好弄了
㈧ SQL语句怎样使查询同一表同一列多条数据分列显示
declare @sql varchar(8000)
set @sql = 'select 编码,名称'
select @sql = @sql+', max(case [项目序号] when '''+cast(项目序号 as varchar)+''' then 数额 else 0 end ) AS [项目序号'+cast(项目序号 as varchar)+']'
from (select distinct 项目序号 from 表名) as a
set @sql = @sql +' from 表名 group by 编码,名称'
select @sql
exec (@sql)
㈨ SQL 如何分列
declare @表1 table (id int,数据 varchar(10))
insert into @表1
select 1,'a b c' union all
select 2,'c a n' union all
select 3,'s c v'
select id,数据1=parsename(replace(数据,' ','.'),3),
数据2=parsename(replace(数据,' ','.'),2),
数据3=parsename(replace(数据,' ','.'),1) from @表1
/*
id 数据1 数据2 数据3
------------------
1 a b c
2 c a n
3 s c v
*/
㈩ sql语句如何实现实现查询多列的数据
可以,我看Id列应该是唯一的吧。根据proctID分组的时候,取max(id),然后通过此id关联,就可以把其他列数据也查询出来了