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

  • Home
  • SEARCH
  • 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 3214020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:00:42+00:00 2026-05-17T15:00:42+00:00

I have the following database schema (names changed to protect the innocent) Code Table

  • 0

I have the following database schema (names changed to protect the innocent)

Code Table

| code_id | code_desc |
|---------|-----------|
| 1       | good      |
|---------|-----------|
| 2       | bad       |
|---------|-----------|
| 3       | ugly      |
|---------|-----------|

Foo Table                               AssignedFoo Table

| foo_id | foo_desc | foo_dt    |       | assn_id | foo_id | code_id |
|--------|----------|-----------|       |---------|--------|---------|
| 101    | red      | 1/1/2000  |       | 301     | 101    | 1       |
|--------|----------|-----------|       |---------|--------|---------|
| 102    | green    | 6/1/2000  |       | 302     | 102    | 2       |
|--------|----------|-----------|       |---------|--------|---------|
| 102    | blue     | 12/1/2000 |
|--------|----------|-----------|

Bar Table                               AssignedBar Table

| bar_id | bar_desc | bar_dt    |       | assn_id | foo_id | code_id |
|--------|----------|-----------|       |---------|--------|---------|
| 201    | gold     | 12/1/2000 |       | 401     | 201    | 1       |
|--------|----------|-----------|       |---------|--------|---------|
| 202    | silver   | 1/1/2001  |       | 402     | 202    | 3       |
|--------|----------|-----------|       |---------|--------|---------|
| 202    | bronze   | 6/1/2001  |
|--------|----------|-----------|

I want to count the number of records that are mapped to the Code table regardless if they’re foos or bars. I had though that the following query would work.

SELECT
    COUNT(c.code_id) AS coded,
    c.code_desc
FROM 
    code c
    INNER JOIN assignedfoo af ON af.code_id = c.code_id
    INNER JOIN foo f ON f.foo_id = af.foo_id
WHERE
    f.foo_dt >= [start date] AND 
    f.foo_dt <= [end date]
GROUP BY 
    c.code_desc

UNION ALL   

SELECT
    COUNT(c.code_id) AS coded,
    c.code_desc
FROM 
    code c
    INNER JOIN assignedbar ab ON ab.code_id = c.code_id
    INNER JOIN bar b ON b.bar_id = ab.bar_id
WHERE
    b.bar_dt >= [start date] AND 
    b.bar_dt <= [end date]
GROUP BY 
    c.code_desc 

Unfortunately, I get the following result set

| coded | code_desc |
|-------|-----------|
| 1     | good      |
|-------|-----------|
| 1     | good      |
|-------|-----------|
| 1     | bad       |
|-------|-----------|
| 1     | ugly      |
|-------|-----------|

What I want to get is this

| coded | code_desc |
|-------|-----------|
| 2     | good      |
|-------|-----------|
| 1     | bad       |
|-------|-----------|
| 1     | ugly      |
|-------|-----------|

Any suggestions about how to fix this?

Any help would be greatly appreciated.

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-05-17T15:00:42+00:00Added an answer on May 17, 2026 at 3:00 pm

    I think you simply need to move the group by/count outside of your union like this:

    select count(a.code_id) as Coded, a.code_desc
    from (
    SELECT
        c.code_id,
        c.code_desc
    FROM 
        code c
        INNER JOIN assignedfoo af ON af.code_id = c.code_id
        INNER JOIN foo f ON f.foo_id = af.foo_id
    WHERE
        f.foo_dt >= [start date] AND 
        f.foo_dt <= [end date]
    
    UNION ALL   
    
    SELECT
        c.code_id,
        c.code_desc
    FROM 
        code c
        INNER JOIN assignedbar ab ON ab.code_id = c.code_id
        INNER JOIN bar b ON b.bar_id = ab.bar_id
    WHERE
        b.bar_dt >= [start date] AND 
        b.bar_dt <= [end date]
    ) a
    group by a.code_desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database where I store objects. I have the following (simplified) schema
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following tables in my database that have a many-to-many relationship, which
I have the following function that is pulling data from a database. The ajax
So I have the following user defined type in my oracle database: CREATE OR
I am using the SQLite database and have the following persistent class (simplified): public
I have an application that is made up of the following: A central database
I have a database in the following format: ID TYPE SUBTYPE COUNT MONTH 1
Let's say I have the following simple table variable: declare @databases table ( DatabaseID

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.