问题1:
我要实现的一个存储过程需要接收一个这样的参数:
@PrjList varchar(1000)
正常情况下,参数保存的应该是类似下面的格式:
'项目1,项目2,项目3,项目5'这个参数在是在WHERE条件里被使用的,如:
WHERE 项目 IN (@belongProjcet)即,在外面决定查询的范围,可能属于一个项目,也可能属于多个项目,动态组合成字符串传进来。我遇到的问题是,如果传入的是 '项目1,项目2,项目3'格式的字符串,执行存储过程后,根本查不出数据,应该是逗号的问题:
即我最终需要的是:WHERE 项目 IN ('项目1','项目2','项目3')
而象我那么做得到的却是:WHERE 项目 IN ('项目1,项目2,项目3')所以查不到东西啊,大家给看看这个参数怎么组合能得到需要的结果。
问题2:
写存储过程的时候,遇到这样的问题,例如
Select A,
Count(*),--合计
(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
[X]-[Y],--这列需要两个子查询的差
[X]-[Y]/[Z],这列是多个子查询的更复杂的表达式
From Test t
Group by A我的问题是:可不可以声明一些变量,在第一次求得[X][Y][Z]值的时候,就把值赋给这些变量,然后在后边需要[X][Y][Z]组合计算的时候,就不需要再重新写一遍相同的子查询,不仅效率低,看着也乱。
类似这样:
Declare X,Y,Z
Select A,
Count(*),--合计
X=(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
Y=(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
Z=(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
X-Y,--这列使用记录了子查询结果的变量来计算
X-Y/Z,--这列使用记录了子查询结果的变量来计算
From Test t
Group by A我这样实验了,但是没有成功,可能是语法或别的什么地方不对,大家给看看吧。
我要实现的一个存储过程需要接收一个这样的参数:
@PrjList varchar(1000)
正常情况下,参数保存的应该是类似下面的格式:
'项目1,项目2,项目3,项目5'这个参数在是在WHERE条件里被使用的,如:
WHERE 项目 IN (@belongProjcet)即,在外面决定查询的范围,可能属于一个项目,也可能属于多个项目,动态组合成字符串传进来。我遇到的问题是,如果传入的是 '项目1,项目2,项目3'格式的字符串,执行存储过程后,根本查不出数据,应该是逗号的问题:
即我最终需要的是:WHERE 项目 IN ('项目1','项目2','项目3')
而象我那么做得到的却是:WHERE 项目 IN ('项目1,项目2,项目3')所以查不到东西啊,大家给看看这个参数怎么组合能得到需要的结果。
问题2:
写存储过程的时候,遇到这样的问题,例如
Select A,
Count(*),--合计
(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
[X]-[Y],--这列需要两个子查询的差
[X]-[Y]/[Z],这列是多个子查询的更复杂的表达式
From Test t
Group by A我的问题是:可不可以声明一些变量,在第一次求得[X][Y][Z]值的时候,就把值赋给这些变量,然后在后边需要[X][Y][Z]组合计算的时候,就不需要再重新写一遍相同的子查询,不仅效率低,看着也乱。
类似这样:
Declare X,Y,Z
Select A,
Count(*),--合计
X=(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
Y=(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
Z=(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
X-Y,--这列使用记录了子查询结果的变量来计算
X-Y/Z,--这列使用记录了子查询结果的变量来计算
From Test t
Group by A我这样实验了,但是没有成功,可能是语法或别的什么地方不对,大家给看看吧。
嘿,最近做报表,比较郁闷
replace(''''+@PrjList+'''',',',''',''')
可以把
项目1,项目2,项目3,项目5
变成
'项目1','项目2','项目3','项目5'
先这样简化
Select A,
Count(*),--合计
sum(case when xxx='某值' then 1 else 0 end) as [X],
sum(case when yyy='某值' then 1 else 0 end) as [Y],
sum(case when zzz='某值' then 1 else 0 end) as [Z],
sum(case when xxx='某值' then 1 else 0 end)-
sum(case when yyy='某值' then 1 else 0 end) as [Y-Y],
sum(case when xxx='某值' then 1 else 0 end)-
sum(case when yyy='某值' then 1 else 0 end)/
sum(case when zzz='某值' then 1 else 0 end) as [Y-Y/Z]
From Test t
Group by A
比较全的写法
declare @sql varchar(8000)
set @sql='select * from test where 项目 IN ('+replace(''''+@PrjList+'''',',',''',''')+')'print @sql
exec(@sql)问题2
用变量是不可能的
我的写法看起来好像很好理解(沾沾自喜中.....)
就是某组 sum(case when xxx='某值' then 1 else 0 end) 语句,要反复出现多次了?
Select A,
Count(*),--合计
X=(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
Y=(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
Z=(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
X-Y,--这列使用记录了子查询结果的变量来计算
X-Y/Z,--这列使用记录了子查询结果的变量来计算
From Test t
Group by A
=========
这样写:
Select B.A ,
B.Cnt,
B.X,B.Y,B.Z, B.X- B.Y , B.X - B.Y /B.Z
From
(
Select A,
Count(*) As Cnt
X=(Select count(*) from test where xxx='某值' and A=t.A),
Y=(Select count(*) from test where yyy='某值' and A=t.A),
Z=(Select count(*) from test where zzz='某值' and A=t.A)
From Test t
Group by A
) B;)
WHERE CHARINDEX(项目,@belongProjcet)>0
to tx1icenhe(冒牌马可 V0.3) :
就是某组 sum(case when xxx='某值' then 1 else 0 end) 语句,要反复出现多次了?
---------------------------------------------------------
不是子查询出现多次不怕的,对性能影响不大
(
项目 varchar(20),
资金 int
)
insert into tt
select '项目1',1000 union all
select '项目2',1000 union all
select '项目3',1000 union all
select '项目4',1000select * from tt
declare @PrjList varchar(800),
@temp varchar(500)
set @PrjList = '项目1,项目2,项目3'
set @temp = '('''+replace(@PrjList,',',''',''')+''')'
print @temp
exec('select * from tt where 项目 in '+@temp)create table tt
(
xxx varchar(10),
yyy varchar(10),
zzz varchar(10)
)
insert into tt
select 'a','aa','aaa' union all
select 'b','bb','bbb' union all
select 'c','cc','ccc' union all
select 'c','dd','ccc'
select * from tt-用临时表
select X=(select count(*) from tt where xxx='c'),
Y=(select count(*) from tt where xxx='b'),
Z=(select count(*) from tt where yyy='bb')
into #t from tt
select X,Y,Z,X-Y as [X-Y],X-Y/Z as [X-Y/Z] from #t
DROP TABLE #T
declare @PrjList varchar(800),
@temp varchar(500)
set @PrjList = '项目1,项目2,项目3'
set @temp = '('''+replace(@PrjList,',',''',''')+''')'
print @temp
exec('select * from tt where 项目 in '+@temp)
这可以查出来呀
declare @t table(x varchar(20),id int identity(1,1))
insert @t
select '项目1' union all
select '项目2' union all
select '项目3' union all
select '项目4' union all
select '项目5' union all
select '项目6'declare @Prjlist varchar(100)
set @Prjlist = '项目1,项目2,项目3,项目5'
select * from @t where charindex(','+ x + ',',',' + @Prjlist + ',') > 0
--注释:
--在x列以及@Prjlist前后各加一个逗号,使二者[格式]完全相同,即类似:
--charindex(',项目1,',',项目1,项目2,项目3,项目5,')
我的网站:http://www.obai.net 卖首饰的
http://www.booksoo.com 卖书的
多谢关注.分先攒着,等到二个星再说.^^
to:xyxfly(小虾米 -_- 何去何从)
多谢关注.分先攒着,等到二个星再说.^^
到时多给点啊,先预定了 ^_^ 0_0
@str varchar(500)
set @prjList = '项目1,项目2,项目3'
set @str = '('''+replace(@prjList,',',''',''')+''')'
exec('select * from 表 where 项目 in '+@str )