Solved

Hiding Duplicate Column values to easily break out groups

Posted on 2016-10-20
4
36 Views
Last Modified: 2016-10-21
Hello,

There has to be a way to do this that i cannot find.  I have a table laid out below.   I would like to supress the username column from displaying the value after the first record:

Table:
username    Process   Date  ID

select
username,
Process,
Count(*)
from table
group by username, Process
order by username,Process

I would lile the output to look like this

John Smith        Process1          10
                           Process2           5
                           Process3           87
Mary Smith      Process1          3
                           Process2           66
                           Process3           5

Also, is there any way to put a space inbetween each grouping of username?
0
Comment
Question by:posae
  • 2
  • 2
4 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41852966
Typically you prefer to do that outside the db engine, but something like this should do it for you.  Fyi, I don't have any ready data to test this code with, so it's untested.

select case when row_num = 1 then username else '' end as username,
    Process, [count]
from (
    select *, ROW_NUMBER() over(partition by username order by Process) as row_num
    from (
        select
        username,
        Process,
        Count(*) as [count]
        from table_
        group by username, Process
    ) as derived
) as derived2
order by username,Process
0
 

Author Comment

by:posae
ID: 41853153
Thanks for the post.  this almost worked.  the output was:
                              Process2      25
                              Process3      41
John Smith            Process1      75
Mary Smith          Process1        34

I'm looking to have the name value show on the first row.  I'm thinking it is sort order as the first column is '' ?  Do you need some data ?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 41853902
Ooh, right, same column name, so SQL uses the value after the sort.  We'll need to give the username in the query a different name from the final output, for example:

select case when row_num = 1 then username else '' end as user_name, /*must be a new column name*/
    Process, [count]
from (
    select *, ROW_NUMBER() over(partition by username order by Process) as row_num
    from (
        select
        username,
        Process,
        Count(*) as [count]
        from table_
        group by username, Process
    ) as derived
) as derived2
order by username,Process /*must be the old column name*/
0
 

Author Closing Comment

by:posae
ID: 41853955
Perfect...thank you!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now