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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:31:47+00:00 2026-06-10T13:31:47+00:00

Sorry for this long winded question, but I’m not sure how to go about

  • 0

Sorry for this long winded question, but I’m not sure how to go about constructing this SQL query needed for the results I want. I’ll outline the two queries that I currently run and work fine and I will outline the results I need. Any help will be appreciated.

1st Query:

SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (2, 4, 5)
GROUP BY c.name ORDER BY c.name;

Result:

--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa |     2 |         150 |            75 |
bbbb |     1 |          75 |            75 |
dddd |     1 |          25 |            25 |
--------------------------------------------

2nd query:

SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (1, 3)
GROUP BY c.name ORDER BY c.name;

2nd results:

--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa |     2 |         200 |           100 |
bbbb |     1 |         100 |           100 |
dddd |     1 |          25 |            25 |
--------------------------------------------

Given these tables with this data:
Table table_a:

-----------------------------------
id | b_id | views | type | status |
-----------------------------------
 1 |  100 |   100 |    2 |      0 |
 2 |  200 |    75 |    4 |      0 |
 3 |  300 |    50 |    5 |      0 |
 4 |  400 |    25 |    2 |      0 |
 5 |  500 |   100 |    1 |      0 |
 6 |  600 |   100 |    1 |      0 |
 7 |  700 |   100 |    3 |      0 |
 8 |  800 |    25 |    3 |      0 |
-----------------------------------

Table table_b:

-------------
id  | c_id  |
-------------
100 |  1000 |
200 |  2000 |
300 |  1000 |
400 |  4000 |
500 |  1000 |
600 |  2000 |
700 |  4000 |
800 |  1000 |
-------------

Table table_c:

-------------
id   | name |
-------------
1000 | aaaa |
2000 | bbbb |
3000 | cccc |
4000 | dddd |
-------------

This is the table that I actually want, which is simply a concantenation of the above two tables with the common column being the name column.

-------------------------------------------------------------------------------------------------------------------------------
name | total_type245 | total_views_type245 | average_views_type245 | total_type13 | total_views_type13 | average_views_type13 |
-------------------------------------------------------------------------------------------------------------------------------
aaaa |             2 |                 150 |                    75 |            2 |                200 |                  100 |
bbbb |             1 |                  75 |                    75 |            1 |                100 |                  100 |
dddd |             1 |                  25 |                    25 |            1 |                 25 |                   25 |
-------------------------------------------------------------------------------------------------------------------------------

It’s most likely quite a simple query, but I cannot work out how to do it.

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-10T13:31:48+00:00Added an answer on June 10, 2026 at 1:31 pm

    Just join the results together;

    SELECT ResultsA.name, 
    total_type245, 
    total_views_type245, 
    average_views_type245,
    total_type13,
    total_views_type13,
    average_views_type13
    
    FROM
    (
        SELECT c.name AS name, count(*) AS total_type245, sum(a.views) AS total_views_type245, sum(a.views) / count(*) as average_views_type245
        FROM table_a a
        JOIN table_b b ON b.id = a.b_id
        JOIN table_c c ON c.id = b.c_id
        WHERE a.status = 0 AND a.type in (2, 4, 5)
        GROUP BY name
    
    ) as ResultsA
    
    JOIN 
    (
        SELECT c.name AS name, count(*) AS total_type13, sum(a.views) AS total_views_type13, sum(a.views) / count(*) as average_views_type13
        FROM table_a a
        JOIN table_b b ON b.id = a.b_id
        JOIN table_c c ON c.id = b.c_id
        WHERE a.status = 0 AND a.type in (1, 3)
        GROUP BY name
    
    ) as ResultsB ON ResultsA.name = ResultsB.name
    
    ORDER BY ResultsA.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this long post. The question is however small but requires full detail.
Sorry this is not a very well defined question, I am thinking about an
Sorry if this is a bit long winded but I thought better to post
Hey guys sorry this is a pretty long question but I cannot call printPASSInfo()
somewhat related to: libxml2 from java yes, this question is rather long-winded - sorry.
Sorry about this long block of code, but I think it makes sense to
This question has disturbed me for a long time. Sorry if it is a
Sorry this is a basic question, but all my research just barely missed answering
Sorry this is probably super basic. But in all my javabean examples, I've not
sorry this is such a simple question but I can't figure it out. How

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.