在重复值前生成数字

时间:2018-03-14 21:29:03

标签: sql-server sql-server-2014

场景:我从导入SQL Server的平面文件中获取公司名称,地址,城市和联系人的数据。

我正在尝试将此数据导入只接受唯一公司名称的平台。让我们看一下平面数据的例子:

 CompanyName                 City               Address                            
 -------------------------------------------------------------------------
 Starbucks (Seattle)         Seattle            Null                    
 Starbucks (Seattle-1)       Seattle            44 East Ave                       
 Starbucks (Seattle-2)       Seattle            2701 Freedom way                  
 Starbucks (Seattle-3)       Seattle            3500 E Destination Drive          
 Starbucks (Luxembourg)      Luxembourg         N2915 Countrt Road AB             
 Starbucks (Orleans)         Orleans            2800 Rice St.                     
 Starbucks (St. Paul)        St. Paul           6500 Henri-Bourassa BE 

使用这样的数据集,我试图获得如下所示的结果:

Row_number

我尝试过使用Partition ByCreate table #Company ( companyname nvarchar(255), City nvarchar(100), [Address] nvarchar(255), Contact nvarchar(255) ) insert into #Company (companyname, City, Contact) values ('Starbucks', 'Seattle', 'Pedram'); insert into #Company (companyname, City, [Address], Contact) values ('Starbucks', 'Seattle', '44 East Ave', 'Daniel'), ('Starbucks', 'Seattle', '2701 Freedom way', 'April'), ('Starbucks', 'Seattle','3500 E Destination Drive', 'Steve'), ('Starbucks', 'Luxembourg', 'N2915 Countrt Road AB', 'Hans'), ('Starbucks', 'Orleans', '2800 Rice St.', 'Emily'), ('Starbucks', 'St. Paul', '6500 Henri-Bourassa BE', 'Casey'), ('Starbucks', 'St. Paul', '6500 Henri-Bourassa BE', 'Kathy'); SELECT * FROM #Company SELECT ROW_NUMBER() OVER (PARTITINO BY companyname, city, [address] ORDER BY companyname), CASE WHEN ROW_NUMBER() OVER (PARTITION BY companyname, city, [address] ORDER BY companyname, city, [address]) = 1 THEN companyname + ' ' + '(' + ISNULL(city,'')+ ')' ELSE companyname END, --+ CAST(ROW_NUMBER() OVER (PARTITION BY T1.COMPANY, T1.CITY ORDER BY T1.[Address 1]) AS VARCHAR(3)) * FROM #Company ,但我遇到的问题是:如何在前面生成该数字?

以下是数据集的代码和我尝试过的内容:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("HeadTest fired");

    HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);

    HttpContent httpContent = new ByteArrayContent(new Byte[0]);

    httpContent.Headers.ContentLength = 1234;

    httpResponseMessage.Content = httpContent;

    httpResponseMessage.Headers.Add("X-Header-Test", "true");

    return httpResponseMessage;
}

3 个答案:

答案 0 :(得分:2)

让我建议设置此顺序:

order by companyname,city,[address]

 ;WITH X AS
 (
     SELECT ROW_NUMBER() over (Partition by companyname,city,[address] order by companyname,city,[address]) rn,
     companyname,
     city,
     address,
     contact
     FROM #Company
 )
 SELECT CONCAT(companyname,' (', city, iif(rn=1, '', '-' + CAST(rn as varchar(10))), ')') CompanyName,
        City, Address, Contact
 FROM   X;
 GO


 CompanyName            | City       | Address                  | Contact
 :--------------------- | :--------- | :----------------------- | :------
 Starbucks (Luxembourg) | Luxembourg | N2915 Countrt Road AB    | Hans   
 Starbucks (Orleans)    | Orleans    | 2800 Rice St.            | Emily  
 Starbucks (Seattle)    | Seattle    | <em>null</em>                     | Pedram 
 Starbucks (Seattle)    | Seattle    | 2701 Freedom way         | April  
 Starbucks (Seattle)    | Seattle    | 3500 E Destination Drive | Steve  
 Starbucks (Seattle)    | Seattle    | 44 East Ave              | Daniel 
 Starbucks (St. Paul)   | St. Paul   | 6500 Henri-Bourassa BE   | Casey  
 Starbucks (St. Paul-2) | St. Paul   | 6500 Henri-Bourassa BE   | Kathy  

dbfiddle here

答案 1 :(得分:2)

<强>查询

INNER JOIN

结果集

SELECT 
        companyname 
       + ' (' + City 
       + REPLACE( ' - ' 
       + CAST( 
        ROW_NUMBER() OVER (PARTITION BY companyname , City 
                            ORDER BY CASE WHEN [Address] IS NULL 
                                            THEN '0' ELSE [Address] END
                            ) - 1 AS VARCHAR(10))
        + ')' , ' - 0', '') AS CompanyNameNew
        , City
        , [Address]

FROM #Company
ORDER BY CompanyName , [Address]

答案 2 :(得分:1)

使用一些cte来格式化并使用row_number():

def my_method(foo="bar"):
    print(foo)
var = "foo='baz'"
my_method(var)

>>> baz