视觉foxpro中的分区排名

时间:2017-01-30 19:25:39

标签: sql visual-foxpro dense-rank

我有下表,看起来像

+ --- + --- +
| AID | Tag |
+ --- + --- +
| 1   | 1   |
| 2   | 2   |
| 2   | 3   |
| 2   | 820 |
| 2   | 821 |
| 3   | 2   |
| 4   | 5   |
| 4   | 18  |
| 4   | 2744|
| 4   | 2745|
+ --- + --- +

当我编写以下SQL Server 2008代码时

select AID,
       Tag,
       RANK() over (partition by AID order by Tag asc) as rank
from My_Table

它产生以下结果

+ --- + --- + ---- +
| AID | Tag | Rank |
+ --- + --- + ---- +
| 1   | 1   | 1    |
| 2   | 2   | 1    |
| 2   | 3   | 2    |
| 2   | 820 | 3    |
| 2   | 821 | 4    |
| 3   | 2   | 1    |
| 4   | 5   | 1    | 
| 4   | 18  | 2    |
| 4   | 2744| 3    |
| 4   | 2745| 4    |
+ --- + --- + ---- +

这正是我想要的。

现在,我想在Visual FoxPro 9 SQL中编写相同的东西。我使用 recno()尝试了它,如所示here;这是我的记录,但似乎不支持分区的能力,VFP 9 SQL似乎不支持相关的子查询。我知道我可以用游标和扫描来做到这一点,但我不想这样做。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

在VFP中没有rank()函数。但是,您可以通过多种方式实现相同的效果。一种方法是简单扫描... endscan通过更新排名值,如下例所示:

*** Sample Data
    Create Cursor mytable ( AID Int, Tag Int)
    Insert Into mytable Values (1,1   )
    Insert Into mytable Values (2,2   )
    Insert Into mytable Values (2,3   )
    Insert Into mytable Values (2,820 )
    Insert Into mytable Values (2,821 )
    Insert Into mytable Values (3,2   )
    Insert Into mytable Values (4,5   )
    Insert Into mytable Values (4,18  )
    Insert Into mytable Values (4,2744)
    Insert Into mytable Values (4,2745)
*** Sample Data

Select AID, Tag, Cast(0 As Int) As rank ;
    from mytable ;
    order By AID, Tag ;
    into Cursor crsRanked ;
    readwrite

Scan
    AID = AID
    rcno = Recno()
    Replace rank With Recno()-m.rcno+1 While AID = m.AID
    Skip -1
Endscan

Locate
Browse
编辑:昨天我忽略了MS SQL服务器的RANK()函数是如何工作的,对不起。这是一个像MS SQL Server的Rank(),Dense_Rank(),Row_number():

Create Cursor mytable ( AID Int, Tag Int)
Insert Into mytable Values (1,1   )
Insert Into mytable Values (2,2   )
Insert Into mytable Values (2,3   )
Insert Into mytable Values (2,820 )
Insert Into mytable Values (2,821 )
Insert Into mytable Values (3,2   )
Insert Into mytable Values (4,5   )
Insert Into mytable Values (4,18  )
Insert Into mytable Values (4,18  )
Insert Into mytable Values (4,18  )
Insert Into mytable Values (4,2744)
Insert Into mytable Values (4,2745)

Select AID, Tag, ;
    Cast(0 As Int) As rownum, ;
    Cast(0 As Int) As rank, ;
    Cast(0 As Int) As denserank ;
    from mytable ;
    order By AID, Tag ;
    into Cursor crsRanked ;
    readwrite

Local AID,rank,denserank,nextrank,rcno
Scan
    AID = AID
    rank = 0
    nextrank = 0
    denserank = 0
    rcno = Recno()
    Scan While m.AID = AID
        Tag = Tag
        rank = nextrank + 1
        denserank = m.denserank + 1
        Replace ;
            rank With m.rank, ;
            denserank With m.denserank, ;
            rownum With Recno()-m.rcno+1 ;
            While AID = m.AID And Tag = m.Tag
        nextrank = m.nextrank + _Tally
        Skip -1
    Endscan
    Skip -1
Endscan

Locate
Browse

答案 1 :(得分:0)

我发现答案,对于任何关心的人都知道。 Visual FoxPro 9.0中支持以下SQL代码,并且可以执行我们想要的操作。

select  t1.aid, ;
        t1.tag, ;
        count(*) as rank ;
    from my_table t1 ;
    inner join my_table t2 ;
        on t2.aid = t1.aid ;
        and t2.tag <= t1.tag ;
    group by t1.aid, t1.tag

要了解原因,让我们通过省略聚合并包含来自 t2 的标记来仔细研究内连接。

select  t1.aid, ;
        t1.tag, ;
        t2.tag ;
    from my_table t1 ;
    inner join my_table t2 ;
        on t2.aid = t1.aid ;
        and t2.tag <= t1.tag ;
    order by t1.aid, t1.tag

此代码生成一个类似

的表
+ --- + ---- + ---- +
| AID | Tag1 | Tag2 |
+ --- + ---- + ---- +
| 1   | 1    | 1    |
| 2   | 2    | 2    |
| 2   | 3    | 2    |
| 2   | 3    | 3    |
| 2   | 820  | 2    |
| 2   | 820  | 3    |
| 2   | 820  | 820  |
| 2   | 821  | 2    |
| 2   | 821  | 3    |
| 2   | 821  | 820  |
| 2   | 821  | 821  |
| 3   | 2    | 2    |
| 4   | 5    | 5    | 
| 4   | 18   | 5    |
| 4   | 18   | 18   |
| 4   | 2744 | 5    |
| 4   | 2744 | 18   |
| 4   | 2744 | 2744 |
| 4   | 2745 | 5    |
| 4   | 2745 | 18   |
| 4   | 2745 | 2744 |
| 4   | 2745 | 2745 |
+ --- + ---- + ---- +

我们实际上并不关心 Tag2 中的数据,但现在我们可以清楚地看到排名是按援助分组的 Tag1 的计数 Tag1