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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:43:12+00:00 2026-06-09T21:43:12+00:00

How do I add a column that displays a count for the total number

  • 0

How do I add a column that displays a count for the total number of rows that have the same values in two columns, with multiple tables being joined already. After all the joins and where statements I end up with a table that has the following columns:

Product | Policy Number | Line Number | Endorsement Name

There are a lot more columns in the table but these are the ones I need to reference.
There is a one to Many relationship from Product to Policy Number, a one to Many relationship from Policy Number to Line Number, and a possible one to Many relationship from Line Number to Endorsement or no matching relationship.

This is what the query looks like right now:

Select pd.ProductName AS Product
,p.PolicyNumber AS Policy
,l.LineNumber AS Line
,e.EndorsementName AS Endorsement

FROM Lines l
INNER JOIN Policies p On p.PolicyID = l.PolicyID
LEFT OUTER JOIN Endorsements e on e.EndorsementID = l.EndorsementID
INNER JOIN Product pd on pd.ProductID = p.ProductID

The resulting table looks something like this

Product     Policy     Line     Endorsement
CH           8001       1            A
CH           8001       2            A
CH           8001       3            A
CH           8001       3            B
CH           8002       1            D
CH           8002       2            D
CH           8002       3            A
PP           9001       1            NULL
PP           9001       2            NULL
PP           9002       1            A
PP           9002       2            A
PP           9002       2            B

What I need to do is count the number of endorsements for each policy line. In the example above, I would see that for Product CH, the only line item with multiple endorsements is Policy 8001 – Line 3. For Product PP the only line item with multiple endorsements is Policy 9002 – Line 2.

First thing I want to have is a new column that displays a count for every row that shows the total number of occurrences with that row’s same Policy and Line values. The table would then look like this:

Product     Policy     Line     Endorsement     Line Endorsement Count
CH           8001       1            A                   1
CH           8001       2            A                   1
CH           8001       3            A                   2
CH           8001       3            B                   2
CH           8002       1            D                   1
CH           8002       2            D                   1
CH           8002       3            A                   1
PP           9001       1            A                   1
PP           9001       2            A                   1
PP           9002       1            A                   1
PP           9002       2            A                   2
PP           9002       2            B                   2

After I successfully achieve this counting, I then want a separate query that would only return rows with policies that don’t have any lines with multiple endorsements, so in this case, policies 8001 and 9002 would not show up.

I’ve tried doing, GROUP BY with HAVING and all kinds of different things I’ve seen in different posts and adapted to my needs but have not found a solution that works for this specific example. Keep in mind I also already have quite a few WHERE statements and some Order By as well.

EDIT:
I’m using Microsoft SQL Server Management Studio 2010 to execute these queries

Thanks!

  • 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-09T21:43:14+00:00Added an answer on June 9, 2026 at 9:43 pm

    This depends on the database. If you have window functions, you can do it quite simply:

    Select pd.ProductName as Product, p.PolicyNumber as Policy,
           l.LineNumber Line, e.EndorsementName as Endorsement,
           count(*) over (partition by policy, line) as Policy_Line_Count
    FROM Lines l INNER JOIN
         Policies p
         On p.PolicyID = l.PolicyID LEFT OUTER JOIN
         Endorsements e
         on e.EndorsementID = l.EndorsementID INNER JOIN
         Product pd
         on pd.ProductID = p.ProductID
    

    If you don’t have the windows functions, then you can use the WITH statement to simplify the query:

    with t as (
             Select pd.ProductName as Product, p.PolicyNumber as Policy,
                l.LineNumber Line, e.EndorsementName as Endorsement
         FROM Lines l INNER JOIN
              Policies p
              On p.PolicyID = l.PolicyID LEFT OUTER JOIN
              Endorsements e
              on e.EndorsementID = l.EndorsementID INNER JOIN
              Product pd
              on pd.ProductID = p.ProductID
       )
    select t.*, tpl.cnt
    from t join
         (select Policy, Line, count(*) as cnt
          from t
          group by Policy, Line
         ) tpl
         on t.Policy = tpl.Policy and
            t.Line= tpl.Line
    

    Finally, you might be using mysql that has neither the WITH statement nor windows functions. In this case, you can create a view and use the above query. Or, you can take the original query and put it in a temporary table and use the above query. Or, you take the original query and repeat it twice following the example.

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

Sidebar

Related Questions

I have a HTML table that displays rows of records and has a column
I have a table of data that I need to dynamically add a column
I have several columns in a MySQL database that I would like to add
I have an Arraylist of HashMap . Each HashMap element contains two columns: column
I currently have an ASP.NET GridView that displays a few columns to the user
I have a ListView which displays multiple rows of ListViewItems. The user is able
I foolishly tried to add a column to a table that I did not
I want to ask how to add a computed column in BIRT that compute
What formula would I use in a persisted column so that I can add
How can I add a computed column to a table that already exists? S.O.

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.