如何选择前10名没有重复

时间:2016-07-22 16:45:49

标签: tsql reporting-services sql-server-2012

使用SQL Server 2012 我需要根据ProducerCode选择TOP 10 Producer。但是数据搞砸了,用户输入的生产者只是拼写不同而且使用相同的ProducerCode。 所以我只需要TOP 10,所以如果ProducerCode重复,我只想选择列表中的第一个。 我怎样才能做到这一点?enter image description here

我的数据样本

        ;WITH cte_TopWP   --T
AS 
    (
        SELECT distinct ProducerCode, Producer,SUM(premium) as NetWrittenPremium,
        SUM(CASE WHEN PolicyType = 'New Business' THEN Premium ELSE 0 END) as NewBusiness1,
        SUM(CASE WHEN PolicyType = 'Renewal' THEN Premium ELSE 0 END) as Renewal1,
        SUM(CASE WHEN PolicyType = 'Rewrite' THEN Premium ELSE 0 END) as Rewrite1

        FROM ProductionReportMetrics 
        WHERE YEAR(EffectiveDate) = 2016 AND TransactionType = 'Policy' AND CompanyLine = 'Arch Insurance Company'--AND ProducerType = 'Wholesaler'
        GROUP BY ProducerCode,Producer
    )
    ,
    cte_Counts   --C
AS 
    (
        SELECT distinct ProducerCode, ProducerName, COUNT (distinct ControlNo) as Submissions2,
        SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quoted2,
        SUM(CASE WHEN Type = 'New Business' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as  NewBusiness2,
        SUM(CASE WHEN Type = 'Renewal'      AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as  Renewal2,
        SUM(CASE WHEN Type = 'Rewrite'      AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as  Rewrite2,
        SUM(CASE WHEN  Status = 'Declined' THEN 1 ELSE 0 END ) as  Declined2
        FROM ClearanceReportMetrics
        WHERE YEAR(EffectiveDate)=2016 AND CompanyLine = 'Arch Insurance Company'
        GROUP BY ProducerCode,ProducerName
    )


SELECT      top 10 RANK() OVER (ORDER BY NetWrittenPremium desc) as Rank, 
            t.ProducerCode,
            c.ProducerName as 'Producer', 
            NetWrittenPremium, 
            t.NewBusiness1,
            t.Renewal1, 
            t.Rewrite1,
            c.[NewBusiness2]+c.[Renewal2]+c.[Rewrite2] as PolicyCount,
            c.Submissions2,
            c.Quoted2,
            c.[NewBusiness2],
            c.Renewal2,
            c.Rewrite2, 
            c.Declined2         
FROM        cte_TopWP t --LEFT OUTER JOIN tblProducers p on t.ProducerCode=p.ProducerCode
                        LEFT OUTER JOIN cte_Counts c ON t.ProducerCode=c.ProducerCode

2 个答案:

答案 0 :(得分:1)

您应该使用ROW_NUMBER来解决问题。

https://msdn.microsoft.com/en-us/library/ms186734.aspx

这方面的一个很好的例子是以下答案:

https://dba.stackexchange.com/a/22198

以下是答案中的代码示例。

SELECT * FROM  
(
    SELECT acss_lookup.ID AS acss_lookupID, 
    ROW_NUMBER() OVER 
    (PARTITION BY your_distinct_column ORDER BY any_column_you_think_is_appropriate)
    as num,
    acss_lookup.product_lookupID AS acssproduct_lookupID,
    acss_lookup.region_lookupID AS acssregion_lookupID,
    acss_lookup.document_lookupID AS acssdocument_lookupID,
    product.ID AS product_ID,
    product.parent_productID AS productparent_product_ID,
    product.label AS product_label,
    product.displayheading AS product_displayheading,
    product.displayorder AS product_displayorder,
    product.display AS product_display,
    product.ignorenewupdate AS product_ignorenewupdate,
    product.directlink AS product_directlink,
    product.directlinkURL AS product_directlinkURL,
    product.shortdescription AS product_shortdescription,
    product.logo AS product_logo,
    product.thumbnail AS product_thumbnail,
    product.content AS product_content,
    product.pdf AS product_pdf,
    product.language_lookupID AS product_language_lookupID,
    document.ID AS document_ID,
    document.shortdescription AS document_shortdescription,
    document.language_lookupID AS document_language_lookupID,
    document.document_note AS document_document_note,
    document.displayheading AS document_displayheading
    FROM acss_lookup
        INNER JOIN product ON (acss_lookup.product_lookupID = product.ID)
        INNER JOIN document ON (acss_lookup.document_lookupID = document.ID)
)a
WHERE a.num = 1
ORDER BY product_displayheading ASC;  

答案 1 :(得分:0)

你可以这样做:

SELECT ProducerCode, MIN(Producer) AS Producer, ...
GROUP BY ProducerCode