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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:55:12+00:00 2026-05-16T22:55:12+00:00

I am really not good at sql, The problem is the following: I have

  • 0

I am really not good at sql, The problem is the following:
I have a metadata table like this

META:
id(pk)  prop1   prop2
     1  'pr1'   'pr2'
     2  'pr1'   'pr2'

At the same time, I have many child tables which look something like this:

TABLE1:
metaID(fk-pk)   indx(pk)    active  ...
            1          1         1  ...
            1          2         0  ...
            1          3         1  ...
            2          1         1  ...

and a second table similar to the first

TABLE2:
metaID(fk-pk)   indx(pk)    active  ...
            1          1         1  ...
            1          2         1  ...
            2          1         0  ...
            2          2         0  ...

I need a query which retrieves the the metadata table properties and the number of records in each child table which are active.

For example if I want the elements with prop1 = 'pr1 the query should answer:

meta-id prop1   prop2   TAB1CNT TAB2CNT
      1 'pr1'   'pr2'         2       2
      2 'pr1'   'pr2'         1       0

where TAB1CNT is the count of the active elements of TABLE1 and the same goes for TAB2CNT

  • 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-16T22:55:13+00:00Added an answer on May 16, 2026 at 10:55 pm

    Something like this should do the trick. This would return all meta records, regardless of if there were any active records in table1 or table2.

    SELECT ID, prop1, prop2, ISNULL(t1.ActiveCount, 0) AS TAB1CNT, ISNULL(t2.ActiveCount, 0) AS TAB2CNT
    FROM META m
        LEFT JOIN
        (
            SELECT metaID, COUNT(*) AS ActiveCount
            FROM TABLE1
            WHERE active = 1
            GROUP BY metaID
        ) t1 ON m.ID = t1.metaID
        LEFT JOIN
        (
            SELECT metaID, COUNT(*) AS ActiveCount
            FROM TABLE2
            WHERE active = 1
            GROUP BY metaID
        ) t2 ON m.ID = t2.metaID
    

    Hopefully from this, you can then add whatever clauses you want to return only the META records you are interested in.

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

Sidebar

Related Questions

No related questions found

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.