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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:19:27+00:00 2026-05-20T18:19:27+00:00

Table 1: AccountId, ReferenceId, Name, (lots of other columns) Table 2: AccountId, ReferenceId, (other

  • 0

Table 1:
AccountId, ReferenceId, Name, (lots of other columns)

Table 2:
AccountId, ReferenceId, (other columns)

How can I do a select to get the following:

AccountId, ReferenceId, [Count(*) in Table2 where accountId and reference ID match.]
1, AB, 1
1, AC, 0
2, AD, 4
2, EF, 0

etc

Guessing a join, but that gives me values, not a count?
Tried adding a count, but get errors?

  • 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-20T18:19:28+00:00Added an answer on May 20, 2026 at 6:19 pm

    Something like:

    SELECT t1.AccountId, t1.ReferenceId, COUNT(t2.AccountId)
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.AccountId = t2.AccountId AND
                           t1.ReferenceId = t2.ReferenceId
    GROUP BY t1.AccountId, t1.ReferenceId
    

    should work. The trick is to group by both key values so you can aggregate over other values. In this case you want to simply count values from other rows (you could also sum or average values from the grouped-by rows.).

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

Sidebar

Related Questions

My table structure looks like : Account AccountId LoginName EmployeeId - nullable Employee FirstName,
When I write: CREATE TABLE accounts ( username varchar(64) PRIMARY KEY, I get primary
I want to count the number of accounts from the resulting table generated from
Table:Account AccountID|AccountName|AccountTypeID|IsActive 17 |aaaa |5 |1 18 |bbbb |5 |1 19 |cccc |5 |1
I've got an association table that groups accounts together. I'm trying to select a
Here's a nasty one. I'm trying to reference a table in an outer query
I have the following script to create a table in MySQL version 5.1 which
I have the following mappings: public AccountMap() { Id(x => x.AccountId, AccountId).Column(AccountId); Map(x =>
Domain: public class Account { public virtual int AccountId { get; set; } public
So i need to do a mapping from a Employee table (idEmployee, name, etc..)

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.