Scenario: I am getting the data of companies name, address, city and contact from a flat file imported into SQL Server.
I am trying to import this data into a platform that only accepts unique company names. Let's look at an example of the flat data:
CompanyName City Address Contact
------------------------------------------------------------------------
Starbucks Seattle Null Pedram
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
With a data set like this, I am trying to get a result as shown below:
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
I have tried using Row_number and Partition By but the issue that I have is: how do I generate that number in front?
Below is the code for data Set and what I have tried:
Create 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
Query
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]
Result Set
╔══════════════════════════╦════════════╦══════════════════════════╗
║ CompanyNameNew ║ City ║ Address ║
╠══════════════════════════╬════════════╬══════════════════════════╣
║ Starbucks (Seattle) ║ Seattle ║ NULL ║
║ Starbucks (Seattle - 1) ║ Seattle ║ 2701 Freedom way ║
║ Starbucks (Seattle - 2) ║ Seattle ║ 3500 E Destination Drive ║
║ Starbucks (Seattle - 3) ║ Seattle ║ 44 East Ave ║
║ Starbucks (Orleans) ║ Orleans ║ 2800 Rice St. ║
║ Starbucks (St. Paul) ║ St. Paul ║ 6500 Henri-Bourassa BE ║
║ Starbucks (St. Paul - 1) ║ St. Paul ║ 6500 Henri-Bourassa BE ║
║ Starbucks (Luxembourg) ║ Luxembourg ║ N2915 Countrt Road AB ║
╚══════════════════════════╩════════════╩══════════════════════════╝
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With