在oracle中,我们将在创建此表时在select上使用rownum.现在在teradata,我似乎无法让它工作.除非我一起使用3列,否则我没有可以排序的列并且具有唯一值(大量重复).
旧的方式是这样的,
create table temp1 as
select
rownum as insert_num,
col1,
col2,
col3
from tables a join b on a.id=b.id
;
Run Code Online (Sandbox Code Playgroud) 我有以下SQL查询:
select
ID, COLUMN1, COLUMN2
from
(select ID, COLUMN1, COLUMN2, row_number() over (order by 2 DESC) NO from A_TABLE)
where
NO between 0 and 100
Run Code Online (Sandbox Code Playgroud)
我想要做的是选择查询的前100条记录
select ID, COLUMN1, COLUMN2 from ATABLE order by 2 DESC
Run Code Online (Sandbox Code Playgroud)
以下是问题:
显然,该order by条款不起作用.我注意到我必须在之后添加另一个order by 2 DESC子句(...) from ATABLE,以便我的查询工作.有什么我做错了吗?或者是预期的行为?
我该如何添加where条款?假设我只需要选择表格的前100条记录where COLUMN1 like '%value%'.我之前尝试过添加where子句,(...) from ATABLE但是它产生了一个错误......
救命?谢谢.
PS:我正在使用Oracle 10g R2.
我在SQL Server 2008上,使用NHibernate作为持久层(尽管这个问题纯粹是SQL,我相信).
我把我的问题归结为以下SQL语句:
SELECT TOP 2
this_.Id as Id36_0_,
this_.Name as Name36_0_,
ROW_NUMBER() OVER (ORDER BY this_.IsActive) as MyOrder
FROM Campsites this_
ORDER BY this_.IsActive /* a bit field */
Run Code Online (Sandbox Code Playgroud)
这是NH为检索分页结果集而生成的查询的一部分.以上陈述给出了以下结果:
Id36_0_ Name36_0_ MyOrder
9806 Camping A Cassagnau 1
8869 Camping a la ferme La Bergamotte 2
Run Code Online (Sandbox Code Playgroud)
但是,如果我省略ROW_NUMBER()OVER(ORDER BY this_.IsActive) - 这是NH为在第一页上检索结果而生成的 - 我在结果中得到两个完全不同的表条目:
SELECT TOP 2
this_.Id as Id36_0_,
this_.Name as Name36_0_
/* ROW_NUMBER() OVER(ORDER BY this_.IsActive) as MyOrder */
FROM Campsites this_
ORDER BY this_.IsActive /* a …Run Code Online (Sandbox Code Playgroud) 我需要在以下Query中使用ROW_NUMBER()来返回结果的第5到10行.谁能告诉我我需要做什么?我一直试图无济于事.如果有人可以提供帮助,我会非常感激.
SELECT *
FROM villa_data
INNER JOIN villa_prices
ON villa_prices.starRating = villa_data.starRating
WHERE villa_data.capacity >= 3
AND villa_data.bedrooms >= 1
AND villa_prices.period = 'lowSeason'
ORDER BY villa_prices.price,
villa_data.bedrooms,
villa_data.capacity
Run Code Online (Sandbox Code Playgroud) 我要做的是以下内容:我有一个包含多位作者的表SingleAuthor.该表有时不止一次包含同一作者.我想要做的是更新表并添加作者特定的号码.例如:
sat_name - > sat_rowNumber
弗雷迪 - > 1
作者2 - > 2
弗雷迪 - > 1
AnotherOne - > 3
我已经有了查询给我这个结果:
SELECT ROW_NUMBER() OVER( ORDER BY sat_name),
sat_name
FROM SingleAuthor
GROUP BY sat_name
但问题是,我想在sat_rowNumber列中插入此数据.我带着这个问题来到这里:
UPDATE SingleAuthor SET sat_rowNumber = ( SELECT newTable.numb
FROM(
SELECT ROW_NUMBER() OVER( ORDER BY sat_name) as numb, sat_name
FROM SingleAuthor
GROUP BY sat_name) AS newTable
WHERE newTable.sat_name =) -- the current row in the to be updated table
Run Code Online (Sandbox Code Playgroud)
我想要做的就是更新SingleAuthor表的 …
想象一下,我有一个非规范化的表,如下所示:
CREATE TABLE Persons
(
Id int identity primary key,
FirstName nvarchar(100),
CountryName nvarchar(100)
)
INSERT INTO Persons
VALUES ('Mark', 'Germany'),
('Chris', 'France'),
('Grace', 'Italy'),
('Antonio', 'Italy'),
('Francis', 'France'),
('Amanda', 'Italy');
Run Code Online (Sandbox Code Playgroud)
我需要构建一个返回每个人姓名的查询,以及他们所在国家/地区的唯一ID.ID不一定必须是连续的; 更重要的是,他们并没有必须在任何顺序.实现这一目标的最有效方法是什么?
最简单的解决方案似乎是DENSE_RANK:
SELECT FirstName,
CountryName,
DENSE_RANK() OVER (ORDER BY CountryName) AS CountryId
FROM Persons
-- FirstName CountryName CountryId
-- Chris France 1
-- Francis France 1
-- Mark Germany 2
-- Amanda Italy 3
-- Grace Italy 3
-- Antonio Italy 3
Run Code Online (Sandbox Code Playgroud)
然而,这在我的CountryName …
我需要为id分组的显式行获取行号.假设dataframe(df)如下所示:
id a b
3 2 NA
3 3 2
3 10 NA
3 21 0
3 2 NA
4 1 5
4 1 0
4 5 NA
Run Code Online (Sandbox Code Playgroud)
我需要创建一个列,它将给出行号序列,不包括大小写b == 0.
期望的输出:
id a b row
3 2 NA 1
3 3 2 2
3 10 NA 3
3 21 0 -
3 2 NA 4
4 1 5 1
4 1 0 -
4 5 NA 2
Run Code Online (Sandbox Code Playgroud)
我使用dplyr但无法实现相同,我的代码:
df <- df %>%
group_by(id) %>%
mutate(row = row_number(id[b …Run Code Online (Sandbox Code Playgroud) 我在表中有一些重复的值,我试图用它Row_Number来过滤它们.我想使用排序行datediff并根据最接近的值将结果排序为零,但我正在努力解释负值.
以下是数据和我当前Row_Number字段的示例(rn)列:
PersonID SurveyDate DischargeDate DaysToSurvey rn
93638 10/02/2015 30/03/2015 -48 1
93638 27/03/2015 30/03/2015 -3 2
250575 23/10/2014 29/10/2014 -6 1
250575 19/11/2014 24/11/2014 -5 2
203312 23/01/2015 26/01/2015 -3 1
203312 26/01/2015 26/01/2015 0 2
387737 19/02/2015 26/02/2015 -7 1
387737 26/02/2015 26/02/2015 0 2
751915 02/04/2015 04/04/2015 -2 1
751915 10/04/2015 25/03/2015 16 2
712364 24/01/2015 30/01/2015 -6 1
712364 26/01/2015 30/01/2015 -4 2
Run Code Online (Sandbox Code Playgroud)
我对上面的选择声明是:
select
PersonID, SurveyDate, DischargeDate, …Run Code Online (Sandbox Code Playgroud) 我有这样的桌子
+------+----------+------------+
| id | 1_value | 2_value |
+------+----------+------------+
| 3 | foo1 | other |
| 10 | fooX | stuff |
| 13 | fooJ | here |
| 22 | foo7 | and |
| 31 | foou | here |
+------+----------+------------+
Run Code Online (Sandbox Code Playgroud)
我想要得到的是拥有行号
我试图做这样的事情
SELECT id, @curRow := @curRow + 1 AS row_number
FROM table
JOIN (SELECT @curRow := 0) r
Run Code Online (Sandbox Code Playgroud)
而且确实有效...
+------+--------------+
| id | row_number |
+------+--------------+
| 3 | 1 |
| 10 …Run Code Online (Sandbox Code Playgroud) 由于我的 DBMS 不允许在 order by 子句中使用“Nulls Last”,因此我需要以下方面的帮助。
row_number() over(Partition by a.ID order by a.Date asc
我需要我的行有一个按日期升序排列的 ID 的行号序列,但空日期行在我的序列中是最后一个。显然,如果我可以按我的顺序说“NULLS LAST”就好了,但是,我的 dbms (MSSQL) 不允许这样做。
例如目前正在发生的事情:
ID Date ROW_NUMBER
1 NULL 1
1 1/2/17 2
1 1/3/17 3
2 NULL 1
2 2/2/17 2
2 2/3/17 3
2 2/4/17 4
Run Code Online (Sandbox Code Playgroud)
我想要发生的事情:
ID Date ROW_NUMBER
1 1/2/17 1
1 1/3/17 2
1 NULL 3
2 2/2/17 1
2 2/3/17 2
2 2/4/17 3
2 NULL 4
Run Code Online (Sandbox Code Playgroud)
帮助?谢谢你!
row-number ×10
sql ×7
sql-server ×4
sql-order-by ×3
select ×2
t-sql ×2
dataframe ×1
dense-rank ×1
dplyr ×1
inner-query ×1
mysql ×1
nhibernate ×1
oracle ×1
oracle10g ×1
partition ×1
r ×1
row ×1
sql-update ×1
teradata ×1