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 8964439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:35:24+00:00 2026-06-15T16:35:24+00:00

I have a test table to demonstrate the issue: Id NetworkId CountryCode 1 1

  • 0

I have a test table to demonstrate the issue:

Id  NetworkId   CountryCode
1       1           de
2       2           de
3       2           de
4       2           de
5       1           us
6       1           us
7       1           us
8       2           us

I need to output something like this:

NetworkId   CountryCode    DistCount
    1           de              1
    2           de              3
    1           us              3
    2           us              1

Attempted Queries

I looked for several answers on SO and I wasn’t able to find exactly what I need. Here is the first related question and the queries I tried: Counting the rows of multiple distinct columns

Query:

SELECT NetworkId, CountryCode, COUNT(*) as DistCount
FROM (SELECT DISTINCT NetworkId, CountryCode FROM TestTable) AS FOO
GROUP BY NetworkId, CountryCode

Results in:

NetworkId   CountryCode    DistCount
    1           de              1
    1           us              1
    2           de              1
    2           us              1

Query:

SELECT COUNT(DISTINCT(STR(NetworkId) + ',' + STR(CountryCode)))
FROM TestTable

Results in:

Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.

I also tried the answers in this question: How can I count distinct multiple fields without repeating the query?

Query:

SELECT 
   NetworkId, 
   CountryCode,
   COUNT(*) OVER(PARTITION BY NetworkId, CountryCode) as DistCount
FROM TestTable
GROUP BY NetworkId, CountryCode

Result:

NetworkId   CountryCode    DistCount
    1           de              1
    1           us              1
    2           de              1
    2           us              1

As you can tell, I’m having a hard time figuring out how to do this… I would think it should be relatively simple, but I’m missing something.

  • 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-06-15T16:35:25+00:00Added an answer on June 15, 2026 at 4:35 pm

    If Id is unique and not null within TestTable (which it will be if it is the primary key), then this query will return the result set you specified:

    SELECT NetworkId, CountryCode, Count(1) AS DistCount
      FROM TestTable 
     GROUP BY NetworkId, CountryCode
     ORDER BY NetworkId, CountryCode
    

    However, if the Id column is not unique, and what you want is a count of distinct non-null Id values within each group, you can add the DISTINCT keyword:

    SELECT NetworkId, CountryCode, Count(DISTINCT Id) AS DistCount
      FROM TestTable 
     GROUP BY NetworkId, CountryCode
     ORDER BY NetworkId, CountryCode
    

    Given your sample data, both queries will return the same result. There will be a difference only if you have duplicate Id values within a group.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test table like this CREATE TABLE #temp ( rid INT IDENTITY
I have a table that is something like this: ╔════════════╗ ║ table_test ║ ╠════════════╣
I have created a test table in MySQL and would like to insert 10
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I have table to test score data that I need to pivot and I
I have this test table with one row entry and 2 indexes, the first
I have a table like: CREATE TABLE test( id integer not null default nextval('test_id_seq'::regclass),
I have this query DECLARE @Test TABLE ( RowID INT IDENTITY(1,1) PRIMARY KEY ,[Name]VARCHAR(10)
I have 2 tables like these TABLE 1 **user_id** **text** 1 test 1 test
I have a table and a procedure like this: CREATE TABLE test_table ( id

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.