易截截图软件、单文件、免安装、纯绿色、仅160KB

一个SQL查询

2005-10-18---f
2006-10-18---a
2007-10-18---c
2008-10-18---a
2009-10-18---b
2010-10-18---b
2011-10-18---a
2012-10-18---a
我要排序成
2005-10-18---f
2006-10-18---a
2008-10-18---a
2011-10-18---a
2012-10-18---a
2007-10-18---c
2009-10-18---b
2010-10-18---b
前面的2010-10-18是时间,还有一个是NAME字段
首先第1是要按时间排序,然后相同的name要排在一起..搞了好久了..整不出来..求高手帮忙啊



SQL code:
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([dt] datetime,[name] varchar(1))
insert [tb]
select '2005-10-18','f' union all
select '2006-10-18','a' union all
select '2007-10-18','c' union all
select '2008-10-18','a' union all
select '2009-10-18','b' union all
select '2010-10-18','b' union all
select '2011-10-18','a' union all
select '2012-10-18','a'

---查询
select a.*
from tb a,
(select name,min(dt) as px from tb group by name) b
where a.name=b.name
order by b.px,a.name,a.dt
/**
dt name
----------------------- ----
2005-10-18 00:00:00.000 f
2006-10-18 00:00:00.000 a
2008-10-18 00:00:00.000 a
2011-10-18 00:00:00.000 a
2012-10-18 00:00:00.000 a
2007-10-18 00:00:00.000 c
2009-10-18 00:00:00.000 b
2010-10-18 00:00:00.000 b

(8 行受影响)
**/


SQL code



Code highlighting produced by Actip


相关问答:

csv文件转换成sql导入到数据库,没有数据为何?

执行的顺序:
1)文件浏览框(选择文件使用)
选择好文件后
点击一个导入按钮的时候 ,把上面上传框里的csv文件以一个ID为文件名,上传到**/**文件夹下
2)读取这个文件夹下的csv的文件,转换成sql
3 ......

数据以xml格式返回 - MS-SQL Server / 应用实例

从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
   <folder state="unchecked&qu ......

请教SQLSERVER的两个问题 - MS-SQL Server / 疑难问题

1。怎样使xp_cmdshell能完整输出超过255个字符的字符串。
2。select 时,检索速度是与from后的 TABLE顺序有关,还是与where条件的顺序有关(TABLE数据多少 )
在系统属性设定里有个选项,可以修改单字段输出字数限制. ......

高分求sqlserver写excel - MS-SQL Server / 疑难问题

1.怎么把数据从sqlserver里面的数据写入到指定路径的excel
比如
declare @公司名称 char(50)
set @公司名称 = (select 公司名称 from 公司信息)
EXEC [master]..xp_cmdshell 'bcp "select * from [master]. ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号