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
相关问答:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jas ......
现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......
我想查询出每天数据的最大的一个值。表的格式如下
表名: hisdata
字段 编号 值 状态 时间
Id value state dattime
101 32.3 0 ......
原SQL语句SQL code:
SELECT t6.FName '操作工',t1.FDate '日期',t5.FName '制单人',t3.FName '设备',t4.FName '班制',
t7.FBillNo '工艺指令单号',t8.FName '岗位',t2. ......