English 中文(简体)
a. 选择特定身份的普通指数
原标题:SQL to get an ordinal index in selection for specific ID

I m与SQ服务器数据库合作。 Say, 我选择了一批身份识别器。 我能否找到一个在那个阵列中平等的NID的顺序?

我这里指的是什么。 这是我的实际选择:

SELECT [id] FROM [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 
ON t1.[id]=t2.[itmid] 
ORDER BY t2.[iid] ASC;

因此,它将归还以下几套身份证:

13, 7, 34, 5, 2, 14

而且,我需要知道该阵列中的“ID=5”指数(回答为3,如果13是指数0,7=指数1,34=指数2,5 = 成为指数3)。

最佳回答

利用ROW_NUMBER():

SELECT ROW_NUMBER() OVER (ORDER BY t2.[iid] ASC) -1 AS RowIndex
    ,[id] 
FROM [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 
    ON t1.[id]=t2.[itmid] 
ORDER BY t2.[iid] ASC;

http://msdn.microsoft.com/en-us/library/ms186734.aspx”rel=“nofollow”http://msdn.microsoft.com/en-us/library/ms186734.aspx

问题回答

Try this:

SELECT x.RowNum - 1
FROM
(
    SELECT [id], ROW_NUMBER() OVER (ORDER BY t2.[iid]) as RowNum 
    FROM   [dbo.test_db_002] t1
           LEFT JOIN [dbo.test_db_003] t2 ON t1.[id]=t2.[itmid] 
) x
WHERE x.[id] = 5

请注意,<代码>-1是因为ROW_NUMBER()从1个而不是0个开始,你具体提到了一个零指数阵列。

Try this... Though I m not sure on your table structure

Declare @Temp table
(
    id int,
    Name varchar(20)
)
Insert into @Temp 
    select 1,  Bob 
union all
    select 2,  Mark 
union all
    select 3,  Shaun 
union all
    select 4,  Ryan 
union all
    select 5,  Steve 
union all
    select 6,  Bryan 
union all
    select 7,  Henry 

Declare @Temp2 table
(
    iid int,
    itmid int,
    Name varchar(20)
)
Insert into @Temp2
    select 1, 3,  Thing 
union all
    select 2, 2,  This 
union all
    select 3, 5,  That 
union all
    select 4, 1,  They 
union all
    select 5, 3,  There 
union all
    select 6, 5,  Though 
union all
    select 7, 6,  Thought 

SELECT t1.[id], Row_Number() OVER (Order by t1.[id]) as RowNum
FROM @Temp t1
LEFT JOIN @Temp2 t2 ON t1.[id]=t2.[itmid] 
ORDER BY t1.[id] ASC;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签