如何在此查询中获取记录的正确行号

时间:2012-12-28 03:51:55

标签: sql-server reporting-services reportingservices-2005

所有

我有以下查询,它会按照我的预期创建一个结果集,但真实的记录都会以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条记录)但是我无法做到这一点。感谢这方面的任何帮助。

3 个答案:

答案 0 :(得分: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 with Demo

答案 1 :(得分:0)

实际添加唯一行ID时可以实现。

首先在表中为“ID”添加一个新列,然后使用递增的数字自动填充它。

然后每一行都有一个唯一的ID,您可以按该ID调用特定的行。

答案 2 :(得分:0)

对于bluefeet的答案,

+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    ...