如何获得一个表中的奇数行的数据和偶数行的数据
就是有个表。
比如
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
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;
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