Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:18:12+00:00 2026-05-24T12:18:12+00:00

I’m looking to take a dataset like below and generate some statistics off the

  • 0

I’m looking to take a dataset like below and generate some statistics off the data. However, i’m having trouble figuring out how to get the data or if its even possible with a single query. I have different types of ports, in the below example its only user/printer/unknown, but there can be more than just those three. I also have status and again there can be more than just the statuses that are listed. I’ve tried using groupby, but it just doesn’t seem to be the right tool since I’m wanting to group by one type, but I also need a count on each of the statuses?!? Any suggestions on how to achieve this would be greatly appreciated.

| Status        | Type 

| connected   | User
| disabled    | User
| connected   | Printer
| disabled    | Printer
| connected   | User
| disabled    | Unknown
| disabled    | Unknown


Want Resuls like this:

| Type      | Connected   | Disabled

| User      | 2           | 1
| Printer   | 1           | 1
| Unknown   | 0           | 2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:18:12+00:00Added an answer on May 24, 2026 at 12:18 pm

    As @JNK mentioned, you can use PIVOT, but to do it dynamically, I believe you would have to construct the statement based on the available Status values.

    The example below uses PIVOT with hard-coded status values, and then constructs the statement using the values from the sample data. You could also get the Status values from a table of valid statuses, etc.

    create table #temp
    (
        [Status] nvarchar(20),
        [Type] nvarchar(20)
    )
    
    insert into #temp values
        ('Connected', 'User'),
        ('Disabled', 'User'),
        ('Connected', 'Printer'),
        ('Disabled', 'Printer'),
        ('Connected', 'User'),
        ('Disabled', 'Unknown'),
        ('Disabled', 'Unknown')
    
    -- pivot
    select [Type], [Connected], [Disabled]
    from 
        (select [Status], [Type] from #temp) t
        pivot
        (count([Status]) for [Status] in ([Connected], [Disabled])) as p    
    order by [Connected] desc
    
    -- dynamic pivot 
    declare @statusList nvarchar(max),
            @pivot nvarchar(max)
    
    -- get the list of Status values
    select @statusList = coalesce(@statusList + ',', '') + '[' + [Status] + ']'
    from (select distinct [Status] from #temp) t
    order by [Status]
    
    -- build the pivot statement
    set @pivot = 
        'select [Type],' + @statusList + 
        ' from (select [Status], [Type] from #temp) t' +
        ' pivot (count([Status]) for [Status] in (' + @statusList + ')) p'
    
    -- and execute it
    exec (@pivot)
    
    drop table #temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.