sql-server 从SQL Server表中选择备用行

jxct1oxe  于 2022-10-31  发布在  SQL Server
关注(0)|答案(7)|浏览(194)

我正在使用SQL Server 2008。我的表不包含任何唯一列;如何从中获取交替行?
SQL Server数据表:

+-----+--------+
|  id |  name  |
|-----+--------|
|  1  |  abc   |
|  2  |  pqr   |
|  2  |  pqr   |
|  3  |  xyz   |
|  4  |  lmn   |
|  5  |  efg   |
|  5  |  efg   |
+-----+--------+

由于我们必须至少提供一个与问题相关的工作建议,所以我尝试了下面的代码;这在从大量数据中提取数据时不是一种合适的技术。
试用版:

create table #tmp
(
    id int, name varchar(10), srNo int
)

insert into #tmp
   select 
      id, name,
      ROW_NUMBER() OVER (ORDER BY id) % 2 as srNo   --,alternate rows
   from 
      Employee

select * 
from #tmp 
where srNo = 1 --or srNo = 0

以上查询给出了备用行,即第1、第3、第5行 OR 第2、第4、第6等。
请帮助我以正当的方式*无#tmp * 地实现目标!

sc4hvdpw

sc4hvdpw1#

您可以只将select语句用作内联视图,而不需要#tmp表。

select t.id, name
from (select id, name, ROW_NUMBER() over (order by id) as srNo from Employee) t
where (t.srNo % 2) = 1

SqlFiddle函数

dxxyhpgq

dxxyhpgq2#

--从表中提取ALTERNATE记录(偶数)

Select * from TableName where ColumnName % 2 = 0

对于例如:从人力资源.员工中选择 *,其中业务实体ID % 2 = 0
--从表中提取ALTERNATE记录(奇数)

Select * from TableName where ColumnName % 2 = 1

对于例如:从人力资源.员工中选择 *,其中业务实体ID % 2 = 1

8aqjt8rx

8aqjt8rx3#

我将student作为表名。
这是我的答案-〉
1.对于偶数行号-
> SELECT id from (SELECT rowno, id from student) where mod(rowno,2)=1
1.对于奇数行-
> SELECT id from (SELECT rowno, id from student) where mod(rowno,2)=1

wecizke3

wecizke34#

同样也可以使用**having**子句实现;但是它增加了group by任务:

SELECT id, name
FROM (SELECT id, name, ROW_NUMBER()over(order by id) AS srNo FROM Employee) x
GROUP BY srNo, id, name
HAVING (srNo % 2) = 0
qvk1mo1f

qvk1mo1f5#

您可以只使用select语句作为内联视图,而不需要#tblCities表。

select tbl1.CityID,tbl1.CityName from (select ROW_NUMBER() over(order by CityID asc) as row_no,CityID,CityName from tblCities) as tbl1  where tbl1.row_no%2=1
hpxqektj

hpxqektj6#

declare  @t table
    (
        id int,
        name nvarchar(20)
    )
    insert into @t
    Select 1, 'abc'
    union all
    Select 2, 'pqr'
    union all
    Select 2, 'pqr'
    union all
    Select 3, 'xyz'
    union all
    Select 4, 'lmn'
    union all
    Select 5, 'efg'
    union all
    Select 2, 'efg'

    Select * from(
    Select *, row_number() over(order by id) as rnum from @t ) t where rnum % 2 <> 0
uyto3xhc

uyto3xhc7#

create table t (id bigint NOT NULL, input_1 boolean not null, data_gps timestamp(0) not null);

insert into t (id, input_1,data_gps) values 
  (1, false  , '2022-01-01 15:42:07'),
  (2, true   , '2022-01-02 15:42:07'),
  (3, true   , '2022-01-03  15:42:07'),
  (4, false   , '2022-01-04  15:42:07'),
  (5, true   , '2022-01-05  15:42:07'),
  (6, true   , '2022-01-06  15:42:07'),
  (7, true   , '2022-01-07  15:42:07'),
  (8, true   , '2022-01-08  15:42:07'),
  (9, false   , '2022-01-09  15:42:07'),
  (10 ,true   , '2022-01-10  15:42:07'),
  (11, true   , '2022-01-11  15:42:07'),
  (12, true   , '2022-01-12  15:42:07'),
  (13, false   , '2022-01-13  15:42:07'),
  (14, true   , '2022-01-14  15:42:07');

您将拥有

下面是将按值更改分组的查询

select input_1, min(data_gps) as mind, max(data_gps) as maxd
from (
select input_1, data_gps,
     row_number() over (order by data_gps) 
     - row_number() over (partition by input_1 order by data_gps) as grp
from t
) as tmp
group by input_1, grp
order by min(data_gps);

结果

演示https://dbfiddle.uk/6Ajy3H5O

相关问题