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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:33:25+00:00 2026-05-23T15:33:25+00:00

Hi maybe someone can help me here … I have a slight problem with

  • 0

Hi maybe someone can help me here …
I have a slight problem with an SQL Statement. ( On MS – SQL Server 2008)
So i have 6 Tables looking like this

ID / Company / Month / ClosedTimeStamp / Different Information

Now i need (preferrable in one Statement :P) the count of Datasets from each table grouped by Company and Month at the time it looks something like this.
And there is another thing not all tables need to have data for that Company and that Month so there can be 0 as Result for count(*)

SELECT COUNT(*) as c, Month, Company 
FROM Table1  WHERE ClosedTimeStamp IS NULL
GROUP BY Company, Month 
ORDER BY Company

I can do this for all the tables and just pick out the results for each company … Well if someone has any Idea i really would appreciate it 🙂

Sorry forgot something … the result should look like this:

Company / Month / CountTable1 / CountTable2 / CountTable3 / …..
Test 02 1 0 50

If it’s not possible in one statement well then i have to make it work another way. 🙂

Thanks

Lim

  • 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-23T15:33:26+00:00Added an answer on May 23, 2026 at 3:33 pm

    If your DB was normalized the query would be much simpler.

    Because, your company and Month are spread across 6 tables, we need to make union of those tables in order to get the distinct dataset of all company+month, as such:

    select company, month from table1
     union
    select company, month from table2
     union
    select company, month from table3
     union
    select company, month from table4
     union
    select company, month from table5
     union
    select company, month from table6
    

    Note, that we need union, not union all, because we don’t want the same company+month pair repeated.

    Then, just use this dataset to query the quantities for each table:

    select t.company, t.month,
        (select count(*) from table1
          where company = t.company
            and month = t.month
            and ClosedTimeStamp is null) as qt1,
        (select count(*) from table2 
          where company = t.company
            and month = t.month
            and ClosedTimeStamp is null) as qt2,
        (select count(*) from table3
          where company = t.company 
            and month = t.month
            and ClosedTimeStamp is null) as qt3,
        (select count(*) from table4
          where company = t.company
            and month = t.month
            and ClosedTimeStamp is null) as qt4,
        (select count(*) from table5
          where company = t.company
            and month = t.month
            and ClosedTimeStamp is null) as qt5,
        (select count(*) from table6
          where company = t.company
            and month = t.month
            and ClosedTimeStamp is null) as qt6
    from (
      select company, month from table1
       union
      select company, month from table2
       union
      select company, month from table3
       union
      select company, month from table4
       union
      select company, month from table5
       union
      select company, month from table6
    ) t
    order by t.company
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem, maybe someone can help me (or explain, where my
maybe someone can help me: i'm using the rest server from here: https://github.com/philsturgeon/codeigniter-restserver .
I have one problem with a gridview control and maybe someone can help me.
A have a question, maybe someone can help me. I am trying to make
I'm going slidely mad over here, maybe someone can help me figure out what's
I'm having a really frustrating problem I hope someone can help me with. Here
I am hoping that someone can help me with this relative simple problem: <?php
I'm having some trouble with xslt and was hoping that maybe someone here can
I'm having trouble groking something in Linq - maybe someone can give me some
I'm a Linq noobie, maybe someone can point me in the right direction. What's

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.