有点难度的问题,请教大家。
table1这个表是动态建立的
select @cttable=@cttable+m.months+' numeric(18,2) ,' from
(
select distinct '月'+cast(datepart(month,jydate) as varchar(2))+'月' as months
From jyinfo
where machnum in (select machnum from contract_info where contract_num=@contractnum)
and jydate between @stdate and @eddate
) m
得到日期,然后exec()建立的。
table1
aa 月1月 月2月 月3月
---------------------------------------
aaaaa NULL NULL NULL
table2
counts months
----------- ---------
1 月1月
2 月2月
3 月3月
那么我现在想得到这样的结果
aa 月1月 月2月 月3月
---------------------------------------
aaaaa 1 2 3
要注意的是,月份不是固定的。是通过前面选出来的。
问题点数:20、回复次数:9Top
1 楼yesterday2000(一笑而过)回复于 2004-09-03 11:55:04 得分 20
------用邹老大的!!
declare @s varchar(8000)
set @s=''
select @s=@s+',['+cast(months as varchar)+']='''+counts+''''
from TB
set @s=stuff(@s,1,1,'')
exec('select '+@s)Top
2 楼delwin(delwin)回复于 2004-09-03 12:03:42 得分 0
多谢,
这个我知道,关键是那个UPDATE语句怎么写?Top
3 楼delwin(delwin)回复于 2004-09-03 12:04:16 得分 0
table1
aa 月1月 月2月 月3月
---------------------------------------
aaaaa NULL NULL NULL
table2
counts months
----------- ---------
1 月1月
2 月2月
3 月3月
那么我现在想得到这样的结果
aa 月1月 月2月 月3月
---------------------------------------
aaaaa 1 2 3
Top
4 楼yesterday2000(一笑而过)回复于 2004-09-03 12:21:52 得分 0
create table tt (aa char (10),月1月 int,月2月 int,月3月 int )
create table tz (counts int,months char (10))
insert into tt(aa)
select 'aaaaa'
insert into tz
select 1,'月1月'
union
select 2,'月2月'
union
select 3,'月3月'
update tt set 月1月=b.月1月,月2月=b.月2月,月3月=b.月1月
from tt a,
(
select 'aaaaa' as aa,a.*,b.*,c.*
from
(
select counts as 月1月 from tz where months='月1月') a,(
select counts as 月2月 from tz where months='月2月') b,(
select counts as 月3月 from tz where months='月3月') c ) b
where a.aa=b.aaTop
5 楼delwin(delwin)回复于 2004-09-03 12:31:56 得分 0
多谢 yesterday2000(一笑而过) :
你这个是对的,大家看看还有其它方法吗?Top
6 楼delwin(delwin)回复于 2004-09-03 12:58:35 得分 0
请教大家Top
7 楼yesterday2000(一笑而过)回复于 2004-09-03 12:59:36 得分 0
楼主
不好意思
我的基础比较低
只好用笨方法给你解答!!
我想一定有其它简单的方法!!Top
8 楼delwin(delwin)回复于 2004-09-03 13:08:53 得分 0
呵呵,谢谢 yesterday2000(一笑而过),因为我是想用前面那个表是动态建立的,所以用你的那个句子在处理
select 'aaaaa' as aa,a.*,b.*,c.*
from
(
select counts as 月1月 from tz where months='月1月') a,(
select counts as 月2月 from tz where months='月2月') b,(
select counts as 月3月 from tz where months='月3月') c )的时候我觉的我这边可能实现起来比较麻烦,所以就想看看还有没有其他方法,谢谢你。Top
9 楼delwin(delwin)回复于 2004-09-03 13:10:56 得分 0
都怪我刚才没有说明,向yesterday2000(一笑而过),致歉。呵呵Top




