求一个表的sql查询语句

Ianlan 2003-08-25 11:15:22
设计一个表的sql查询,想达到这样的效果,
要求按照三种不同方式排序好的查询结果,如(
按照时间排序1:a1,a2,a3, ... an.
按照代号排序2:b1,b2,b3, ... bn.
按照出生年月排序3:c1,c2,c3, ... cn.

再连接成这样的集:
a1,b1,c1,a2,b2,c2,a3,b3,c3,...an,bn,cn.
用一个sql语句完成,希望高手给于解答,要求用一个select 语句完成,
用指针的我已实现,用用一个select 语句我想了好久,解决不了,请教了,
...全文
35 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ianlan 2003-08-26
  • 打赏
  • 举报
回复
问题没有解决,怎么结贴啊
Ianlan 2003-08-26
  • 打赏
  • 举报
回复
补充:记录有30000条,前面的三条select费时9s
Ianlan 2003-08-26
  • 打赏
  • 举报
回复
错误提示:
服务器: 消息 8156,级别 16,状态 1,行 9
多次为 'tem' 指定了列 'flag1'。

最主要的时间太长了,42s呢,用指针一秒够了啊
过一会儿再来看看,再解决不了就结贴了,谢谢大家
pengdali 2003-08-26
  • 打赏
  • 举报
回复
select identity(int,1,1) flag1,cast(userid as int) userid,[name],psword,[regitime],[phoneNO],[dwname],[address],[company] into #1 from xx order by 时间

select identity(int,1,1) flag1,cast(userid as int) userid,[name],psword,[regitime],[phoneNO],[dwname],[address],[company] into #2 from xx order by 代号

select identity(int,1,1) flag1,cast(userid as int) userid,[name],psword,[regitime],[phoneNO],[dwname],[address],[company] into #3 from xx order by 出生年月



select * from (
select *,flag1,1 flag2 from #1
union all
select *,flag1,2 from #2
union all
select *,flag1,3 from #3
) tem order by flag1,flag2
Ianlan 2003-08-26
  • 打赏
  • 举报
回复
昨天下午开了一下午会,没能及时来看看。可是问题还没解决,
下面我给出实际的表
CREATE TABLE [dbo].[users] (
[userID] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[psword] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[regitime] [smalldatetime] NULL ,
[phoneNO] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[dwname] [nvarchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[address] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[company] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

分别以name,regitime,company排序,得到前面要求的sql语句
Ianlan 2003-08-25
  • 打赏
  • 举报
回复
to大力:无法使用 SELECT INTO 语句向表 '#1' 中添加标识列,该表中已有继承了标识属性的列 'Zid'。
而且希望用的是一个sql 查询
lynx1111 2003-08-25
  • 打赏
  • 举报
回复
lynx1111 2003-08-25
  • 打赏
  • 举报
回复
UP
pengdali 2003-08-25
  • 打赏
  • 举报
回复
select identity(int,1,1) flag1,* into #1 from xx order by 时间

select identity(int,1,1) flag1,* into #2 from xx order by 代号

select identity(int,1,1) flag1,* into #3 from xx order by 出生年月



select * from (
select *,flag1,1 flag2 from #1
union all
select *,flag1,2 from #2
union all
select *,flag1,3 from #3
) tem order by flag1,flag2
Ianlan 2003-08-25
  • 打赏
  • 举报
回复
up
pengdali 2003-08-25
  • 打赏
  • 举报
回复
select * from (
select * from (select top 9999999 *,编号 flag1,1 flag2 from xx order by 时间) a
union all
select * from (select top 9999999 *,编号,2 from xx order by 代号) a
union all
select * from (select top 9999999 *,编号,3 from xx order by 出生年月) a
) tem order by flag1,flag2
Ianlan 2003-08-25
  • 打赏
  • 举报
回复
使用union 相同的记录就只保留一个了,要求不是这样的,要用union all的
nboys 2003-08-25
  • 打赏
  • 举报
回复

使用union连接连接多个数据集
Ianlan 2003-08-25
  • 打赏
  • 举报
回复
to大力:我试过了,排出来的结果顺序是a1,a2,a3,...an,b1,b2,b3,...bn,c1,c2,c3,...cn
to无为:union all语句只有最后的select 能加order by子句
friendliu 2003-08-25
  • 打赏
  • 举报
回复
select top N * from tablename order by time
union all
select top N * from tablename order by num
union all
select top N * from tablename order by birthday
pengdali 2003-08-25
  • 打赏
  • 举报
回复

select * from (select top 9999999 * from xx order by 时间) a
union all
select * from (select top 9999999 * from xx order by 代号) a
union all
select * from (select top 9999999 * from xx order by 出生年月) a
lynx1111 2003-08-25
  • 打赏
  • 举报
回复
select identity(int,1,1) flag1,除标识以外的字段 into #1 from xx order by 时间

select identity(int,1,1) flag1,除标识以外的字段 into #2 from xx order by 代号

select identity(int,1,1) flag1,除标识以外的字段 into #3 from xx order by 出生年月



select * from (
select *,flag1,1 flag2 from #1
union all
select *,flag1,2 from #2
union all
select *,flag1,3 from #3
) tem order by flag1,flag2

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧