行号3区分SQL Server中的字段

时间:2016-03-10 20:27:16

标签: sql-server row

我有这张桌子:

| RecordLocator | DepartureStation | ArrivalStation | JourneyNumber | SegmentNumber | LegNumber | FlightNumber |
|---------------|------------------|----------------|---------------|---------------|-----------|--------------|
|    DADABC     |      MAO         |       GRU      |        2      |       1       |      1    |     1421     |
|    CEDLDA     |      MAO         |       STM      |        1      |       2       |      1    |     1643     |
|    CEDLDA     |      GRU         |       MAO      |        1      |       1       |      1    |     1640     |
|    DADABC     |      GRU         |       FLN      |        1      |       1       |      1    |     1848     |
|    CEDLDA     |      BEL         |       SLZ      |        1      |       2       |      3    |     1643     |
|    DADABC     |      GIG         |       FOR      |        1      |       2       |      3    |     1154     |
|    CEDLDA     |      SLZ         |       FOR      |        1      |       2       |      4    |     1680     |
|    CEDLDA     |      FOR         |       REC      |        1      |       2       |      5    |     1680     |
|    DADABC     |      FOR         |       MAO      |        1      |       2       |      4    |     1982     |
|    CEDLDA     |      REC         |       SSA      |        1      |       2       |      6    |     1680     |
|    CEDLDA     |      STM         |       BEL      |        1      |       2       |      2    |     1643     |
|    DADABC     |      POA         |       GIG      |        1      |       2       |      2    |     1201     |
|    CEDLDA     |      SSA         |       GRU      |        1      |       3       |      1    |     1817     |
|    DADABC     |      FLN         |       POA      |        1      |       2       |      1    |     1201     |

我希望根据row numberJourneyNumberSegmentNumberLegNumber order by添加新列RecordLocator,以获取此结果:

| RecordLocator | DepartureStation | ArrivalStation | JourneyNumber | SegmentNumber | LegNumber | FlightNumber | rowNum |
|---------------|------------------|----------------|---------------|---------------|-----------|--------------|--------|
|    CEDLDA     |        GRU       |        MAO     |        1      |        1      |     1     |      1640    |   1    |
|    CEDLDA     |        MAO       |        STM     |        1      |        2      |     1     |      1643    |   2    |
|    CEDLDA     |        STM       |        BEL     |        1      |        2      |     2     |      1643    |   3    |
|    CEDLDA     |        BEL       |        SLZ     |        1      |        2      |     3     |      1643    |   4    |
|    CEDLDA     |        SLZ       |        FOR     |        1      |        2      |     4     |      1680    |   5    |
|    CEDLDA     |        FOR       |        REC     |        1      |        2      |     5     |      1680    |   6    |
|    CEDLDA     |        REC       |        SSA     |        1      |        2      |     6     |      1680    |   7    |
|    CEDLDA     |        SSA       |        GRU     |        1      |        3      |     1     |      1817    |   8    |
|    DADABC     |        GRU       |        FLN     |        1      |        1      |     1     |      1848    |   1    |
|    DADABC     |        FLN       |        POA     |        1      |        2      |     1     |      1201    |   2    |
|    DADABC     |        POA       |        GIG     |        1      |        2      |     2     |      1201    |   3    |
|    DADABC     |        GIG       |        FOR     |        1      |        2      |     3     |      1154    |   4    |
|    DADABC     |        FOR       |        MAO     |        1      |        2      |     4     |      1982    |   5    |
|    DADABC     |        MAO       |        GRU     |        2      |        1      |     1     |      1421    |   6    |

我尝试了这个查询:

SELECT  
    RecordLocator, 
    DepartureStation, ArrivalStation, 
    JourneyNumber, SegmentNumber, 
    LegNumber, FlightNumber, 
    (SELECT ((P.JourneyNumber + P.SegmentNumber + P.LegNumber)) 
     FROM PAX P2 
     WHERE P2.RecordLocator = P.RecordLocator 
       AND P2.DepartureStation = P.DepartureStation 
       AND P2.ArrivalStation = P.ArrivalStation 
       AND P2.JourneyNumber = P.JourneyNumber 
       AND P2.SegmentNumber = P.SegmentNumber
       AND P2.LegNumber = P.LegNumber
       AND P2.FlightNumber = P.FlightNumber) AS rowNum
FROM 
    PAX P

我正在尝试对JourneyNumberSegmentNumberLegNumber列进行求和,但不起作用,我相信最好的方法,基于Recordlocator定义“列“JourneyNumber>的重量” SegmentNumber> LegNumber,但我不知道如何实施它。

在C#中,我知道如何使用对齐for

// First `for` - Journey
for(int i = 0; i < Journey.Count(); i++)
{
    // Second `for` - Segment
    for(int j = 0; j < Segment.Count(); j++)
    {
        // Third `for` - Leg
        for(int k = 0; k < Leg.Count(); k++)
        {
            result = i + j + k;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

基本上,将问题分解为部分,您希望为每个Found Entry Network: 10.0.1.0/24 Nexthop: 10.8.111.45 Best Path: False Pref: 115 0 Found Network: 10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.0.3.0/29 Nexthop: 10.8.111.45 Best Path: False Pref: 115 0 Found Network: 10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.8.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50 0 Found Network: 10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.9.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50 0 Found Network: 10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.2.0/24 Nexthop: 10.8.10.2 Best Path: True Pref: 100 0 Found Network: 10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.5.0/24 Nexthop: 10.8.142.15 Best Path: False Pref: 85 0 Found Network: 10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.7.0/24 Nexthop: 10.8.40.84 Best Path: True Pref: 100 0 Found Network: 10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.8.0/24 Nexthop: 10.8.10.8 Best Path: True Pref: 100 0 Found Network: 10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.11.0/24 Nexthop: 10.8.42.8 Best Path: True Pref: 100 0 Found Network: 10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.12.0/24 Nexthop: 10.8.10.12 Best Path: False Pref: 100 0 Found Network: 10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.15.0/24 Nexthop: 10.8.10.15 Best Path: False Pref: 100 0 Found Network: 10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 Found Entry Network: 10.10.27.0/24 Nexthop: 10.8.41.81 Best Path: False Pref: 100 0 Found Network: 10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120 1 Found Network: 10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 2 Found Network: 10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120 3 重新开始编号,然后按升序排列所需的3个字段。

总的来说,这就是RecordLocator的想法,它允许使用ROW_NUMBER()的分区集对您的记录进行排序,以根据需要重新开始编号。所以,这样的事情应该这样做:

RecordLocator

因此,您的最终SQL成为:

ROW_NUMBER() OVER(PARTITION BY RecordLocator ORDER BY JourneyNumber, SegmentNumber, LegNumber)

希望这可以解决问题!