一个sql行转列的问题

lala133 2013-07-23 03:20:32
表A的数据如图下
怎么把他转成行,如下面的图,storemoney是每天详细收入。下图最左边那排序号是日期。
...全文
188 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lala133 2013-08-02
  • 打赏
  • 举报
回复
引用 9 楼 wwwwgou 的回复:
如果行转列时,字段个数不固定。SORRY,无法静态写一个SQL;只能动态,写成存储过程。
能解释一下代码的意思吗?
Shawn 2013-08-02
  • 打赏
  • 举报
回复
如果行转列时,字段个数不固定。SORRY,无法静态写一个SQL;只能动态,写成存储过程。
lala133 2013-08-02
  • 打赏
  • 举报
回复
引用 7 楼 htl258 的回复:
[quote=引用 6 楼 lala133 的回复:] [quote=引用 5 楼 ap0405140 的回复:] 店名已经是动态的了,见以下测试.

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)
 
insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994 union all
 select '店名1',220,'2013-06-26',101 union all  --> 新增 店名1 店名2
 select '店名1',220,'2013-06-27',102 union all
 select '店名2',220,'2013-06-26',201 union all
 select '店名2',220,'2013-06-27',202
 
-- SQL没变
declare @tsql varchar(6000)
 
select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t
 
select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '
 
exec(@tsql) 

/*
kdsj         5号停机坪       店名1         店名2         万达
------------ ----------- ----------- ----------- -----------
2013-06-26   2522        101         201         4047
2013-06-27   2595        102         202         1994

(2 row(s) affected)
*/
你这里的nsert into lala select '5号停机坪',231,'2013-06-26',2522 union all select '5号停机坪',231,'2013-06-27',2595 union all select '万达',220,'2013-06-26',4047 union all select '万达',220,'2013-06-27',1994 union all 的店名是手动添加上去的,能不能是表里有的店名都自动显示上去。[/quote] 是动态加,不是手动加的。[/quote]在吗?你这是存储过程的诶,我想要的不是存储过程。我要sql语句就可以了。
htl258_Tony 2013-07-23
  • 打赏
  • 举报
回复
引用 6 楼 lala133 的回复:
[quote=引用 5 楼 ap0405140 的回复:] 店名已经是动态的了,见以下测试.

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)
 
insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994 union all
 select '店名1',220,'2013-06-26',101 union all  --> 新增 店名1 店名2
 select '店名1',220,'2013-06-27',102 union all
 select '店名2',220,'2013-06-26',201 union all
 select '店名2',220,'2013-06-27',202
 
-- SQL没变
declare @tsql varchar(6000)
 
select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t
 
select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '
 
exec(@tsql) 

/*
kdsj         5号停机坪       店名1         店名2         万达
------------ ----------- ----------- ----------- -----------
2013-06-26   2522        101         201         4047
2013-06-27   2595        102         202         1994

(2 row(s) affected)
*/
你这里的nsert into lala select '5号停机坪',231,'2013-06-26',2522 union all select '5号停机坪',231,'2013-06-27',2595 union all select '万达',220,'2013-06-26',4047 union all select '万达',220,'2013-06-27',1994 union all 的店名是手动添加上去的,能不能是表里有的店名都自动显示上去。[/quote] 是动态加,不是手动加的。
lala133 2013-07-23
  • 打赏
  • 举报
回复
引用 5 楼 ap0405140 的回复:
店名已经是动态的了,见以下测试.

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)
 
insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994 union all
 select '店名1',220,'2013-06-26',101 union all  --> 新增 店名1 店名2
 select '店名1',220,'2013-06-27',102 union all
 select '店名2',220,'2013-06-26',201 union all
 select '店名2',220,'2013-06-27',202
 
-- SQL没变
declare @tsql varchar(6000)
 
select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t
 
select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '
 
exec(@tsql) 

/*
kdsj         5号停机坪       店名1         店名2         万达
------------ ----------- ----------- ----------- -----------
2013-06-26   2522        101         201         4047
2013-06-27   2595        102         202         1994

(2 row(s) affected)
*/
你这里的nsert into lala select '5号停机坪',231,'2013-06-26',2522 union all select '5号停机坪',231,'2013-06-27',2595 union all select '万达',220,'2013-06-26',4047 union all select '万达',220,'2013-06-27',1994 union all 的店名是手动添加上去的,能不能是表里有的店名都自动显示上去。
唐诗三百首 2013-07-23
  • 打赏
  • 举报
回复
店名已经是动态的了,见以下测试.

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)
 
insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994 union all
 select '店名1',220,'2013-06-26',101 union all  --> 新增 店名1 店名2
 select '店名1',220,'2013-06-27',102 union all
 select '店名2',220,'2013-06-26',201 union all
 select '店名2',220,'2013-06-27',202
 
-- SQL没变
declare @tsql varchar(6000)
 
select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t
 
select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '
 
exec(@tsql) 

/*
kdsj         5号停机坪       店名1         店名2         万达
------------ ----------- ----------- ----------- -----------
2013-06-26   2522        101         201         4047
2013-06-27   2595        102         202         1994

(2 row(s) affected)
*/
lala133 2013-07-23
  • 打赏
  • 举报
回复
引用 3 楼 ap0405140 的回复:

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)

insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994


declare @tsql varchar(6000)

select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t

select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '

exec(@tsql) 

/*
kdsj         5号停机坪       万达
------------ ----------- -----------
2013-06-26   2522        4047
2013-06-27   2595        1994

(2 row(s) affected)
*/
我的店名不是固定的,是从数据库里来的,不能写固定的啊,日期可以固定,因为一个月也就最多31天
唐诗三百首 2013-07-23
  • 打赏
  • 举报
回复

create table lala
(xsmd varchar(15),
 bmbm int,
 kdsj varchar(12),
 storemoney int
)

insert into lala
 select '5号停机坪',231,'2013-06-26',2522 union all
 select '5号停机坪',231,'2013-06-27',2595 union all
 select '万达',220,'2013-06-26',4047 union all
 select '万达',220,'2013-06-27',1994


declare @tsql varchar(6000)

select @tsql=isnull(@tsql,'')+',['+xsmd+']'
 from (select distinct xsmd from lala) t

select @tsql='select kdsj '+@tsql
            +' from (select xsmd,kdsj,storemoney from lala) a '
            +' pivot(max(storemoney) for xsmd in('+stuff(@tsql,1,1,'')+')) p '

exec(@tsql) 

/*
kdsj         5号停机坪       万达
------------ ----------- -----------
2013-06-26   2522        4047
2013-06-27   2595        1994

(2 row(s) affected)
*/
lala133 2013-07-23
  • 打赏
  • 举报
回复
引用 1 楼 hdhai9451 的回复:
行列转换,请参考: http://blog.csdn.net/hdhai9451/article/details/5026933
看不懂。
Andy__Huang 2013-07-23
  • 打赏
  • 举报
回复
行列转换,请参考: http://blog.csdn.net/hdhai9451/article/details/5026933

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧