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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:33:21+00:00 2026-05-27T16:33:21+00:00

I have 3 queries: table: pageview SELECT event_id, count(*) AS pageviews FROM pageview GROUP

  • 0

I have 3 queries:

table: pageview
SELECT event_id, count(*) AS pageviews 
FROM pageview 
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000

table: upvote
SELECT event_id, count(*) AS upvotes 
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000

table: attending
SELECT event_id, count(*) AS attendants
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000

I’d like to combine the event_ids of all 3 queries ordered by amount and then choose the top 5. How do I do that?

EDIT: HERE IS WHAT I DID TO MAKE IT HAPPEN:

SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) AS amount
FROM   pageview 
GROUP  BY event_id
ORDER  BY amount DESC, rand()
LIMIT  1000)

UNION ALL
(SELECT event_id, count(*) as amount
FROM   upvote
GROUP  BY event_id
ORDER  BY amount DESC, rand()
LIMIT  1000)

UNION ALL
(SELECT event_id, count(*) as amount
FROM   attending
GROUP  BY event_id
ORDER  BY amount DESC, rand()
LIMIT  1000)
) x
GROUP  BY 1
ORDER  BY  sum(amount) DESC
LIMIT  5;
  • 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-27T16:33:22+00:00Added an answer on May 27, 2026 at 4:33 pm

    To UNION the resulting rows of all three queries and then pick the 5 rows with the highest amount:

    (SELECT event_id, count(*) AS amount
    FROM   pageview 
    GROUP  BY event_id
    ORDER  BY pageviews DESC, rand()
    LIMIT  1000)
    
    UNION ALL
    (SELECT event_id, count(*)
    FROM   upvote
    GROUP  BY event_id
    ORDER  BY upvotes DESC, rand()
    LIMIT  1000)
    
    UNION ALL
    (SELECT event_id, count(*)
    FROM   attending
    GROUP  BY event_id
    ORDER  BY attendants DESC, rand()
    LIMIT  1000)
    
    ORDER  BY 2 DESC
    LIMIT  5;
    

    The manual:

    To apply ORDER BY or LIMIT to an individual SELECT, place the
    clause inside the parentheses that enclose the SELECT.

    UNION ALL to keep duplicates.


    To add the counts for every event_id:

    SELECT event_id, sum(amount) AS total
    FROM (
       (SELECT event_id, count(*) AS amount
        FROM   pageview 
        GROUP  BY event_id
        ORDER  BY pageviews DESC, rand()
        LIMIT  1000)
        
        UNION ALL
        (SELECT event_id, count(*)
        FROM   upvote
        GROUP  BY event_id
        ORDER  BY upvotes DESC, rand()
        LIMIT  1000)
        
        UNION ALL
        (SELECT event_id, count(*)
        FROM   attending
        GROUP  BY event_id
        ORDER  BY attendants DESC, rand()
        LIMIT  1000)
        ) x
    GROUP  BY 1
    ORDER  BY sum(amount) DESC
    LIMIT  5;
    

    The tricky part here is that not every event_id will be present in all three base queries. So take care that a JOIN does not lose rows completely and additions don’t turn out NULL.

    Use UNION ALL, not UNION. You don’t want to remove identical rows, you want to add them up.

    x is a table alias and shorthand for AS x. It is required for for a subquery to have a name. Can be any other name here.

    The SOL feature FULL OUTER JOIN is not implemented in MySQL (last time I checked), so you have to make do with UNION. FULL OUTER JOIN would join all three base queries without losing rows.

    Answer to follow-up question

    SELECT event_id, sum(amount) AS total
    FROM (
       (SELECT event_id, count(*) / 100 AS amount
        FROM   pageview ... )
        
        UNION ALL
        (SELECT event_id, count(*) * 5 
        FROM   upvote ... )
        
        UNION ALL
        (SELECT event_id, count(*) * 10
        FROM   attending ... )
        ) x
    GROUP  BY 1
    ORDER  BY  sum(amount) DESC
    LIMIT  5;
    

    Or, to use the base counts in multiple ways:

    SELECT event_id
          ,sum(CASE source
                  WHEN 'p' THEN amount / 100
                  WHEN 'u' THEN amount * 5
                  WHEN 'a' THEN amount * 10
                  ELSE 0
               END)  AS total
    FROM (
       (SELECT event_id, 'p'::text AS source, count(*) AS amount
        FROM   pageview ... )
        
        UNION ALL
        (SELECT event_id, 'u'::text, count(*)
        FROM   upvote ... )
        
        UNION ALL
        (SELECT event_id, 'a'::text, count(*)
        FROM   attending ... )
        ) x
    GROUP  BY 1
    ORDER  BY 2 DESC
    LIMIT  5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two queries that I run in the same table: SELECT id, COUNT(up)
I have these queries: SELECT a.Id, a.Name, (SELECT COUNT(Id) FROM b WHERE b.IdTableA =
I have to queries: select id, name, num from pending id, name, num 1,
If I have two queries SELECT Id, Forename, Surname FROM Person WHERE PersonName Like(‘%frank%’)
I have two queries serving the same purpose. SELECT * FROM #user INNER JOIN
I have 2 queries which return counts of different information in a table: SELECT
Well I have 2 queries a) select * from players b)select * from players
I have a cursor that queries a table like this CURSOR Cur IS SELECT
I have mysql queries with a WHERE IN statement. SELECT * FROM table1 WHERE
I have two queries. The first query: select in_gentime from in_time_temp where cardnumber =

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.