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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:55:05+00:00 2026-06-07T15:55:05+00:00

I have table A with five rows and the following values: Column1 Column2 Column3

  • 0

I have table A with five rows and the following values:

Column1  Column2  Column3  Column4
-------  -------  -------  -------
anna     ben      cat      d
anna     ben      cat      e
anna     ben      cat      f
gina     hugh     ken      m
gina     hugh     ken      p

I want to add another column called Column5. The value of Column 5 will be 3 for the first 3 rows and 2 on the next 2 rows:

Column1  Column2  Column3  Column4  Column5
-------  -------  -------  -------  -------
anna     ben      cat      d        3
anna     ben      cat      e        3
anna     ben      cat      f        3
gina     hugh     ken      m        2
gina     hugh     ken      p        2

How I did this:

SELECT DISTINCT COUNT (DISTINCT t1.Column4) AS Column5,
Column1, Column2, Column3, Column4
FROM TableA AS t1
GROUP BY Column1, Column2, Column3;

This doesn’t work:

Msg 8120, Level 16, State 1, Procedure COUNT, Line 29
Column ‘Column4’ invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.

Any help please? Much appreciated.

PS: If I add Column4 in the group by clause, I get only values of “1” in the result table in Column5.

  • 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-07T15:55:07+00:00Added an answer on June 7, 2026 at 3:55 pm

    One other way to do what you want would be to select distinct rows first, then apply the windowed COUNT() function:

    WITH distinctrows AS (
      SELECT DISTINCT
        Column1,
        Column2,
        Column3,
        Column4
      FROM TableA
    )
    SELECT
      Column1,
      Column2,
      Column3,
      Column4,
      COUNT(Column4) OVER (PARTITION BY Column1, Column2, Column3) AS Column5
    FROM distinctrows
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation I have a couple of table rows within a
I have five rows in my table in html. Each row has only 1
the table named: products . it have five hundred rows record. the table have
I have a table layout with five columns and many rows with dynamic content.
I have a table with name,age and address.I have totally five rows of data
lets assume you have a table with 1M rows and growing ... every five
I have a table with five fields, and I need to filter it to
I have a large table I imported from an Excel spreadsheet. For five of
I have table with around 70 000 rows. There is 6000 rows that i
I have table in which I am inserting rows for employee but next time

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.