如果重复ID不在查询中,如何显示顺序ROW_NUMBER

时间:2016-07-27 22:18:23

标签: sql-server tsql reporting-services

我真的不知道如何正确解释我的问题,我使用子查询只带来不同的ProducerCode字段。 但是现在我只需要基本上带有序列号的行标识符,但是当我使用ROW_NUMBER函数时,它给了我奇怪的数字。 使用SQL 2012,SSRS 2010 这是我的代码:

    ;WITH cte_Counts   --C
AS 
    (
        SELECT      ROW_NUMBER() OVER ( ORDER BY ProducerCode) as RowNum,
                    ProducerCode, 
                    ProducerName, 
                    COUNT (distinct ControlNo) as Submissions,
                    SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quotes,
                    SUM(CASE WHEN Status IN ('Bound','Cancelled','Notice of Cancellation','Non-Renewed') THEN 1 ELSE 0 END ) AS Binds,
                    SUM(CASE WHEN  Status = 'Declined' THEN 1 ELSE 0 END ) as  Declines
        FROM        ClearanceReportMetrics
        WHERE       YEAR(EffectiveDate)=2016
                    AND CompanyLine = 'Arch Insurance Company'
        GROUP BY    ProducerCode,ProducerName
    )

SELECT   * FROM 
            (
                SELECT  RowNum,
                        ROW_NUMBER() OVER (PARTITION BY ProducerCode ORDER BY Submissions  DESC) as num,
                        ProducerCode,
                        ProducerName as 'Producer',         
                        Submissions,
                        Quotes,
                        Binds,
                        Declines,
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Bind',
                        COALESCE(CAST(Quotes as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Qt',
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Quotes,0),0) as 'Qt to Bind',
                        COALESCE(CAST(Declines as FLOAT) /NULLIF(Submissions,0),0) as 'Sub to Dec'

                FROM    cte_Counts
                    ) a 
WHERE a.num=1               
ORDER BY    
            Submissions DESC

enter image description here

我尝试用表达式

在SSRS中实现这一点
=RowNumber(Nothing)

但它也会跳过数字: enter image description here

如何只显示序列行号?

2 个答案:

答案 0 :(得分:0)

请尝试下面的代码一次,如果有任何问题,请告诉我

 ;WITH cte_Counts   --C
AS 
    (
        SELECT      ROW_NUMBER() OVER ( ORDER BY ProducerCode) as RowNum,
                    ProducerCode, 
                    ProducerName, 
                    COUNT (distinct ControlNo) as Submissions,
                    SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quotes,
                    SUM(CASE WHEN Status IN ('Bound','Cancelled','Notice of Cancellation','Non-Renewed') THEN 1 ELSE 0 END ) AS Binds,
                    SUM(CASE WHEN  Status = 'Declined' THEN 1 ELSE 0 END ) as  Declines
        FROM        ClearanceReportMetrics
        WHERE       YEAR(EffectiveDate)=2016
                    AND CompanyLine = 'Arch Insurance Company'
        GROUP BY    ProducerCode,ProducerName
    )

    Select * FROM
    (

SELECT   ROW_NUMBER() OVER ( ORDER BY ProducerCode) AS NEW_ROW_NUMBER,* FROM 
            (
                SELECT  RowNum,
                        ROW_NUMBER() OVER (PARTITION BY ProducerCode ORDER BY Submissions  DESC) as num,
                        ProducerCode,
                        ProducerName as 'Producer',         
                        Submissions,
                        Quotes,
                        Binds,
                        Declines,
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Bind',
                        COALESCE(CAST(Quotes as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Qt',
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Quotes,0),0) as 'Qt to Bind',
                        COALESCE(CAST(Declines as FLOAT) /NULLIF(Submissions,0),0) as 'Sub to Dec'

                FROM    cte_Counts
                    ) a 
WHERE a.num=1     
)     A     
ORDER BY    
            Submissions DESC

答案 1 :(得分:0)

您应该只需要一个ROW_NUMBER(),因为您已经通过CTEGROUP BY中获得了生产者代码和名称的明确列表:

    ;WITH cte_Counts   --C
AS 
    (
        SELECT      ProducerCode, 
                    ProducerName, 
                    COUNT (distinct ControlNo) as Submissions,
                    SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quotes,
                    SUM(CASE WHEN Status IN ('Bound','Cancelled','Notice of Cancellation','Non-Renewed') THEN 1 ELSE 0 END ) AS Binds,
                    SUM(CASE WHEN  Status = 'Declined' THEN 1 ELSE 0 END ) as  Declines
        FROM        ClearanceReportMetrics
        WHERE       YEAR(EffectiveDate)=2016
                    AND CompanyLine = 'Arch Insurance Company'
        GROUP BY    ProducerCode,ProducerName
    )

SELECT ROW_NUMBER() OVER (ORDER BY Submissions DESC) as RowNum,
        *
        FROM 
            (
                SELECT  ProducerCode,
                        ProducerName as 'Producer',         
                        Submissions,
                        Quotes,
                        Binds,
                        Declines,
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Bind',
                        COALESCE(CAST(Quotes as FLOAT) /NULLIF(Submissions,0),0) as 'Sub. to Qt',
                        COALESCE(CAST(Binds as FLOAT) /NULLIF(Quotes,0),0) as 'Qt to Bind',
                        COALESCE(CAST(Declines as FLOAT) /NULLIF(Submissions,0),0) as 'Sub to Dec'

                FROM    cte_Counts
                    ) a           
ORDER BY    RowNum