Solved

Hiding Duplicate Column values to easily break out groups

Posted on 2016-10-20
4
46 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:Scott Pletcher
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:
Scott Pletcher 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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 …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

821 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