Solved

Hiding Duplicate Column values to easily break out groups

Posted on 2016-10-20
4
31 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect...thank you!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

11 Experts available now in Live!

Get 1:1 Help Now