Solved

Hiding Duplicate Column values to easily break out groups

Posted on 2016-10-20
4
51 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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

705 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