Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hiding Duplicate Column values to easily break out groups

Posted on 2016-10-20
4
Medium Priority
?
57 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 70

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

610 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