sql 的查询问题 - MS-SQL Server / 疑难问题
表t1
列1 列2
a 2
b 1
c 3
我希望能根据列2的值来决定最后的显示
a 2
a 2
b 1
c 3
c 3
c 3
有没有什么好的办法。能通过select直接解决的
借助master..spt_values
用sql2005的with 递归
SQL code:
select k.*
from kof k join master..spt_values s
on k.col2>=s.number
where s.type='p' and s.number between 1 and (select MAX(col2) from kof)
order by col1
/*
col1 col2
---------- -----------
a 2
a 2
b 1
c 3
c 3
c 3
*/
SQL code:
select k.*
from kof k join master..spt_values s
on k.col2>=s.number
where s.type='p' and s.number>0
order by col1
/*
col1 col2
---------- -----------
a 2
a 2
b 1
c 3
c 3
c 3
*/
这样也可以
SQL code:
if not object_id('tb') is null
drop table tb
Go
Create table tb([列1] nvarchar(1),[列2] int)
Insert tb
select N'a',2 union all
select N'b',1 union all
select N'c',3
Go
Select a.*
from tb a ,master..spt_values b
相关问答:
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
请教高手:
以下是数据库中的三条记录,英文为字段名称
id planname TaskBeginTime Status
329 2010年03 ......
我觉得mysql和sqlserver有共同的地方:
有个问题是关于表的锁问题:
进程A 进程B
select * from user where id in lock share mode(共享锁)
&nb ......
有这样一条SQL
Select Get_Costtaxrate(col1), Get_Tcostvalue(col1) from a
其中Get_Costtaxrate、Get_Tcostvalue都是函数,这两个函数里面都是查找一个大表,Get_Tcostvalue还需要调用Get_C ......
现在有一个部门表dept(部门名称,部门号。。)有一个人员表emp(姓名,人员编号,职位,薪资,部门)
emp表中的内容是这样的:
a 1 工程师 3000 软件部
b 2 普通员工 2000 硬件部
c 3 工程师 4000 硬件部
d ......