-2

全て、

次のクエリがあり、期待どおりに結果セットが作成されますが、実際のレコードはすべて行番号 1 で返されます。偽のレコードのように、行番号が増加することを期待していました (コードと結果を見れば、私の言いたいことがわかるでしょう。)

Declare @FakeEquipment TABLE(
EquipmentID varchar(25),
EquipmentDesc varchar(150), 
TrackingID varchar(25))

Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz01')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz02')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz03')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz04')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz05')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz06')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz07')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz08')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz09')
Insert into @FakeEquipment values( 'aaaa01',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa02',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa03',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa04',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa05',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa06',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa07',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa08',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa09',NULL,'zzz10')
Insert into @FakeEquipment values( 'aaaa10',NULL,'zzz10')


Select 
        Equipment_EquipmentID,
        Equipment_Description,
        JobAccount_TrackingID
        ,row as rowcnt

        from 
        (
        Select row_number() over (partition by Equipment_EquipmentID order by JobAccount_TrackingID) row,*
        from 

        (SELECT DISTINCT
        CustomizedFieldLogEquipmentJobAccount_1.Equipment_EquipmentID, 
        CustomizedFieldLogEquipmentJobAccount_1.Equipment_Description, 
        CustomizedFieldLogEquipmentJobAccount_1.JobAccount_TrackingID
        FROM
        CustomizedFieldLogEquipmentJobAccount AS CustomizedFieldLogEquipmentJobAccount_1 INNER JOIN CustomizedAccount ON CustomizedFieldLogEquipmentJobAccount_1.Account_AccountID = CustomizedAccount.AccountID
        WHERE
        (CustomizedAccount.AppliesToEquipment = 1)
        AND 
        (CustomizedFieldLogEquipmentJobAccount_1.FieldLog_FieldLogID =1219)
        AND
        IsRented = 0
        AND
        isnumeric(right(CustomizedFieldLogEquipmentJobAccount_1.JobAccount_TrackingID,1))=1

        union

        select 
        EquipmentID, 
        EquipmentDesc,
        TrackingID
        from @FakeEquipment) a 
)b 

Group By
Equipment_EquipmentID,
Equipment_Description,
JobAccount_TrackingID
,row
order by Equipment_EquipmentID

次のような結果が返されます。

Equipment_EquipmentID   Equipment_Description   JobAccount_TrackingID   rowcnt
1218    PAVER   0441-01409  1
127 Ford F250 Pick-Up   0441-01409  1
1350    PNEUMATIC ROLLER    0441-01409  1
1354    VIBRATORY PUP ROLLER    0441-01409  1
1445    JOHN DEERE SKIP 0441-01409  1
16-003  FORD F-350  0441-01409  1
30  F550 CREW TRUCK 0441-01409  1
3-007   TRAFFIC CONTROL F-550   0441-01409  1
6-002   CAT PAVING ROLLER   0441-01409  1
6-006   CAT PAVING ROLLER   0441-01409  1
aaaa01  NULL    zzz01   1
aaaa01  NULL    zzz02   2
aaaa01  NULL    zzz03   3
aaaa01  NULL    zzz04   4
aaaa01  NULL    zzz05   5
aaaa01  NULL    zzz06   6
aaaa01  NULL    zzz07   7
aaaa01  NULL    zzz08   8
aaaa01  NULL    zzz09   9
aaaa01  NULL    zzz10   10

ご覧のとおり、偽の機器テーブルのレコードはすべて正しい行番号を持っていますが、実際のレコードはすべて行番号 1 を持っています。

私がやろうとしているのは、レコード セットを 2 つの SSRS 2005 マトリックス コントロールに分割して、それぞれに 6 つのレコード (最大 12) があるようにすることですが、それはできません。この方向の助けをいただければ幸いです。

4

3 に答える 3

1

これは、期待どおりにレコードに番号を付けています。データを分割しており、値Equipment_EquipmentIDの最初の部分から取得しているデータunionはすべて異なります。したがって、row_numberは増加しません。

row_number()、パーティション内の値が同じであり、値が同じでない場合にのみ増加します。

使用したい場合row_number()は、パーティショニングの方法を変更する必要があるかもしれません。

たとえば、次のデータを使用します。

CREATE TABLE yourData
    ([Equipment_EquipmentID] varchar(6), [JobAccount_TrackingID] varchar(10))
;

INSERT INTO yourData
    ([Equipment_EquipmentID], [JobAccount_TrackingID])
VALUES
    ('1218', '0441-01409'),
    ('127', '0441-01409'),
    ('1350', '0441-01409'),
    ('1354', '0441-01409'),
    ('1445', '0441-01409'),
    ('16-003', '0441-01409'),
    ('30', '0441-01409'),
    ('3-007', '0441-01409'),
    ('6-002', '0441-01409'),
    ('6-006', '0441-01409')
;

次のクエリを使用する場合:

select *,
  row_number() over(partition by Equipment_EquipmentID order by JobAccount_TrackingID) rn1
from yourdata;

行番号が増加していないことがわかります。

ただし、パーティションを次のように変更すると:

select *,
  row_number() over(partition by JobAccount_TrackingID order by JobAccount_TrackingID) rn2
from yourdata;

行番号が増加します。

両方のクエリのデモで SQL Fiddle を参照してください。

于 2012-12-28T04:01:02.117 に答える
0

ブルーフィートの答えに+1。ただし、データベースにレポートよりも多くの計算を行わせることを好みます。その場合、マトリックスを決定する「グループ」列を出力し、「グループ」列にマトリックスフィルターを設定する方が簡単かもしれません:

SELECT  *, 
        [Grouping] = CASE 
            WHEN ROW_NUMBER() OVER (ORDER BY NEWID()) <= 6 THEN 'A' 
            WHEN ROW_NUMBER() OVER (ORDER BY NEWID()) <= 12 THEN 'B' 
            ELSE 'C' 
        END 
FROM    ...
于 2013-01-04T04:11:55.703 に答える
0

実際に一意の行 ID を追加すると可能になります。

「ID」のテーブルに新しい列を追加することから始め、インクリメントされた数値を自動的に入力します。

次に、各行には一意の ID があり、その ID で特定の行を呼び出すことができます。

于 2012-12-28T04:08:22.083 に答える