Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

row number - Access query producing results like ROW_NUMBER() in T-SQL

Do we have ROW_NUMBER function in MS Access? If it has then please let me know any syntax for it as I am stuck here. I have tried forums but I get sql server syntax. Following is my query:

select 
    ROW_NUMBER() OVER (ORDER BY t.TID) AS uni , 
    t.TSource as [Source],
    t.TText as [Text],
    u.Name as [UserId],
    u.Image_Url as [ImageFilePath], 
from table1 t inner join table2 u on t.UserId = u.UIds

but it gives syntax error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Access SQL we can sometimes use a self-join to produce a rank order. For example, for [table1]

TID  UserId  TSource  TText
---  ------  -------  -----
412  homer   foo      bar  
503  marge   baz      thing
777  lisa    more     stuff

the query

SELECT 
    t1a.TID, 
    t1a.UserId, 
    t1a.TSource, 
    t1a.TText,
    COUNT(*) AS TRank
FROM
    table1 AS t1a
    INNER JOIN
    table1 AS t1b
        ON t1a.TID >= t1b.TID
GROUP BY
    t1a.TID, 
    t1a.UserId, 
    t1a.TSource, 
    t1a.TText

produces

TID  UserId  TSource  TText  TRank
---  ------  -------  -----  -----
412  homer   foo      bar        1
503  marge   baz      thing      2
777  lisa    more     stuff      3

and we can use that as a subquery in our JOIN to the other table

select 
    t.TRank as uni,
    t.TSource as [Source],
    t.TText as [Text],
    u.Name as [UserId],
    u.Image_Url as [ImageFilePath]
from 
    (
        SELECT 
            t1a.TID, 
            t1a.UserId, 
            t1a.TSource, 
            t1a.TText,
            COUNT(*) AS TRank
        FROM
            table1 AS t1a
            INNER JOIN
            table1 AS t1b
                ON t1a.TID >= t1b.TID
        GROUP BY
            t1a.TID, 
            t1a.UserId, 
            t1a.TSource, 
            t1a.TText
    ) AS t 
    INNER JOIN 
    table2 AS u 
        ON  t.UserId = u.UIds

producing something like

uni  Source  Text   UserId        ImageFilePath
---  ------  -----  ------------  -------------
  1  foo     bar    HomerSimpson  whatever1    
  2  baz     thing  MargeSimpson  whatever2    
  3  more    stuff  LisaSimpson   whatever3    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...