Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query Question
Message
From
29/01/2014 12:48:22
 
 
To
29/01/2014 12:40:40
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01592560
Message ID:
01592572
Views:
35
>>The company table has a name column char(25) and a comp_code column char(4).
>>The comp_code column is unique, but there are duplicates in the name column.
>>
>>I'm trying to design a query that will give the name once and the comp_code associated with that name.
>>For duplicates, it doesn't matter which comp_code appears, so long as it is associated with that name.
>>Any ideas?
>
>This should get you started
>
>
>declare @Tmp table
>(	comp_Id int IDENTITY(1,1) NOT NULL,
>	comp_code char(4) not null,
>	comp_name char(25)
>);
>
>insert into @Tmp(comp_code, comp_name) values ( 'AAAA', 'Bill')
>insert into @Tmp(comp_code, comp_name) values ( 'BBBB', 'Bill')
>insert into @Tmp(comp_code, comp_name) values ( 'CCCC', 'BillZ')
>insert into @Tmp(comp_code, comp_name) values ( 'DDDD', 'BillX')
>insert into @Tmp(comp_code, comp_name) values ( 'EEEE', 'BillY')
>insert into @Tmp(comp_code, comp_name) values ( 'FFFF', 'BillY')
>
>;
>
>-- select * from  @Tmp
>select	comp_Id,
>		comp_code,
>		comp_name 
>	from (
>			select	row_number() over (
>						partition by comp_name
>						order  by comp_name
>						) as nRow,
>					comp_Id, 
>					comp_code,
>					comp_name 
>				from @Tmp 
>			) X 
>		where nRow = 1
>		
>
Thank, Gregory!
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.
Previous
Reply
Map
View

Click here to load this message in the networking platform