读书人

怎么获得一个表中的奇数行的数据和偶数

发布时间: 2012-12-17 09:31:40 作者: rapoo

如何获得一个表中的奇数行的数据和偶数行的数据
就是有个表。
比如
talbe_1
words
你好
hello
你好1
hello1
你好2
hello2
你好3
hello3
你好4
hello4
我想将table_1中的奇数行和偶数行分别获得并放到两个DataTable里面
求sql 语句,谢谢。
[最优解释]

alter tb add id int identity(1,1)
go
select col into tb1 from tb where id%2=1
select col into tb2 from tb where id%2=0

[其他解释]

with sel as(
select words,rn=row_number()over(order by getdate()) from tb
)
insert into tb1
select words from sel where rn%2=1
with sel as(
select words,rn=row_number()over(order by getdate()) from tb
)
insert into tb2
select words from sel where rn%2=2

[其他解释]
用row_number()来加一个虚拟id,然后再计算奇偶
[其他解释]
create table #table_1(words nvarchar(20))
insert into #table_1
(words) values('你好'),('hello'),('你好1'),('hello1'),('你好2'),('hello2'),('你好3'),('hello3'),('你好4'),('hello4')

create table #test1(words nvarchar(20))
create table #test2(words nvarchar(20))

declare @news varchar(20)
declare @s varchar(20)
declare youbiao1 cursor for
select words from #table_1 where(PATINDEX('%[0-9]%',words)>0 )
OPEN youbiao1
FETCH NEXT FROM youbiao1 INTO @s
WHILE(@@FETCH_STATUS = 0)
begin
set @news=@s
WHILE PATINDEX('%[^0-9]%',@news) > 0
BEGIN
set @news=stuff(@news,patindex('%[^0-9]%',@news),1,'')
END
if @news%2=1
insert into #test1(words)values(@S)
else if @news%2=0
insert into #test2(words) values(@S)
FETCH NEXT FROM youbiao1 INTO @s
END
CLOSE youbiao1
DEALLOCATE youbiao1


select * from #test1
select * from #test2


比较菜,搞了半天别见笑
[其他解释]

if OBJECT_ID('table1','u') is not null
drop table table1
create table table1
(
Name nvarchar(20) ,
)

if OBJECT_ID('table2','u') is not null
drop table table2
create table table2
(
Name nvarchar(20) ,
)

go
with T as
(select Name,ROW_NUMBER() over (order by getdate()) RowID from Course)
insert into Table1 select Name From T where RowID%2=0

go
with T as
(select Name,ROW_NUMBER() over (order by getdate()) RowID from Course)
insert into table2 select Name From T where RowID%2=1

go

select *From Course
select *From table1
select *From table2

--Course
--1001数据结构10001
--1002计算机网络10001


--1003C++程序设计10002
--1004计算机算法10002

--table1
--计算机网络
--计算机算法

--table2
--数据结构
--C++程序设计


[其他解释]

--测试数据
if OBJECT_ID('Test_20121206') is not null drop table Test_20121206
go
create table Test_20121206(value nvarchar(20))
insert into Test_20121206
select 'talbe' union
select '你好' union
select 'talbe1' union
select '再见'

--查询
--新增id标示放入一张新增的表中(临时使用)
select * into table_0 from
(select ROW_NUMBER() over(Order by getdate())as id,value from Test_20121206)t
--奇偶数据分别插入不同表
select * into table_1 from table_0 where id%2=1--奇数行
select * into table_2 from table_0 where id%2=0--偶数行
--删除临时使用的表table_0
drop table table_0

/*查看新增结果
select * from table_1
select * from table_2
id value
-------------------- --------------------
1 talbe
3 你好

(2 行受影响)

id value
-------------------- --------------------
2 talbe1
4 再见

(2 行受影响)
*/


[其他解释]
select name ,time from A ,(select @rownum :=0) tmp_table where (@rownum :=@rownum+1)%2=1; 

提取奇数行
[其他解释]
句中 select 内容为我测试数据
[其他解释]
慢了一步
[其他解释]
不过3楼那个会报错。汇报完毕
[其他解释]
这个其实好解决,主要是加一个唯一标识,然后区分奇偶除2取余为1的就是奇数项,除2取余为0的偶数项
[其他解释]
你的意思是要把英文和中文分离吧 。。 看看能不能考虑编码的不同
[其他解释]
可以不要alter吗?
[其他解释]
我这个是access,貌似没有临时表吧。
我是新手,我不知道怎么把一个sql查询得到的数据成为了一个临时表,然后在从临时表里面查询

或是弄成一个别名什么的。
我不会搞。
比如
select * from tb_1
这样子 我就得到了tb_1里面的数据了。然后我想把这里面的数据当成另外一个别名比如 tb_2
这样子的做法主要是为了 将tb_2再后面使用。
select * from tb_2 where 一些条件
求指教谢谢。
[其他解释]

Dim dt As DataTable = GetKehuanliWord("14,客户案例")
Dim m_iPageCount = dt.Rows.Count


If m_iPageCount <= 0 Then
Exit Function
End If
Dim dtJishu As DataTable
Dim dtOushu As DataTable
dtJishu = dt.Copy
dtOushu = dt.Copy
Dim iIndex As Integer = 0
For iIndex = m_iPageCount - 1 To 0 Step -1
If iIndex Mod 2 = 0 Then
dtJishu.Rows.RemoveAt(iIndex)
Else
dtOushu.Rows.RemoveAt(iIndex)
End If
Next


我是这样写的。用了两个table

读书人网 >SQL Server

热点推荐