Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to avoid repeating same column value every ro

Author  Topic 

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-12-17 : 10:23:43

Hi

I am working in sqlserver 2008 r2. Below is my table structure

Table Name : Details

Client, company, division, active

google, abc, finance, 1

google, abc, management, 1

google, def, HR,1

my query is

select Client, company, division, active from details where client = 'google'

My desired output should be :

google, abc, finance, 1

abc, management, 1

def, HR,1

How to avoid the client name repeating in every rows.

if possible please show me some sample query to achieve this,

Thanks in advance.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-12-17 : 10:49:11
This is a display issue. What are you using to display this information? Is it some kind of report writer or application? If so you should use that to control this conditional rendering of Client.
If you need to do this in sql you would need something like this:

select case when rn = 1 then Client else null end as Client
,company
, division
, active
from (
select Client
, company
, division
, active
, row_number() over (partition by client order by company, division, active) as rn
from details
where client = 'google'
) d


Be One with the Optimizer
TG
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-12-17 : 11:14:58
Awesome TG!! This is what i expected and i learnt today from you that how to do this using rownumber.

Thanks a lot
Go to Top of Page
   

- Advertisement -