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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:39:42+00:00 2026-06-16T21:39:42+00:00

I have the following query: SELECT saturday_combinations.index, v.val AS `row` , COUNT( * )

  • 0

I have the following query:

SELECT saturday_combinations.index, v.val AS  `row` , COUNT( * )  AS  `count` 
FROM saturday_combinations  
INNER JOIN (

SELECT ONE AS val
FROM saturday_combinations 
WHERE ONE IS NOT NULL 

UNION 
SELECT TWO AS val
FROM saturday_combinations
WHERE TWO IS NOT NULL 

UNION 
SELECT THREE AS val
FROM saturday_combinations
WHERE THREE IS NOT NULL 

UNION 
SELECT FOUR AS val
FROM saturday_combinations
WHERE FOUR IS NOT NULL 

UNION 
SELECT FIVE AS val
FROM saturday_combinations
WHERE FIVE IS NOT NULL


UNION 
SELECT SIX AS val
FROM saturday_combinations
WHERE SIX IS NOT NULL 

UNION 
SELECT SEVEN AS val
FROM saturday_combinations
WHERE SEVEN IS NOT NULL 

) v  ON v.val = saturday_combinations.ONE
  OR v.val = saturday_combinations.TWO
  OR v.val = saturday_combinations.THREE
  OR v.val = saturday_combinations.FOUR
  OR v.val = saturday_combinations.FIVE
  OR v.val = saturday_combinations.SIX
  OR v.val = saturday_combinations.SEVEN 
  GROUP BY v.val 

The purpose of the query is to provide a count of the different values contained in the columns ONE,TWO,THREE,FOUR,FIVE,SIX and SEVEN in the table saturday_combinations. However I want to put a desc limit 4 so that it only performs the count based on the last 4 rows (last four maximum indexes). But I am not getting it to work with the union. Adding order and limit at the very end only limits from the final select, rather than get the last 4 rows and calculate the distribution on them. Any tips?

The table schema is as follows:

index | ONE|TWO|THREE|FOUR|FIVE|SIX|SEVEN
 1       1   3   7     10    11  12  13
 2       3   4   5     30    31  22  23
 3       1   2   3      4     5   6   7
 4       1   2   3      4     5   6   7
 5       1   2   3      4     5   6   7
 6       1   2   3      4     5   6   7
 7       1   2   3      4     5   6   7
 8       1   2   3      4     5   6   7
 9       1   2   3      4     5   6   7
 10      1   2   3      4     5   6   7

Index is auto-increment and ONE-SEVEN has integer values.

There are about 3000 rows in the table and I want to count occurences for each value based on the last n rows.

Ideal result for the last n rows where n = last 3 rows should be
Numbers|Count
   1       3
   2       3
   3       3
   4       3
   5       3
   6       3
   7       3

If I increase n to include last 6 rows their count should increase. If I could last 10 rows the count should increase and other numbers should appear with their count.

Here is a link to a sample of the real table.
http://sqlfiddle.com/#!2/d035b

  • 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-16T21:39:43+00:00Added an answer on June 16, 2026 at 9:39 pm

    If answer to my comment is yes then, you could try the following. When you need to add limit, order by to union selects you need to wrap union queries with brackets ().

    • SQLFIDDLE DEMO

    Code:

    (SELECT ONE AS val
    FROM saturday_combinations 
    WHERE ONE IS NOT NULL 
    order by ONE desc limit 4)
    
    UNION 
    (SELECT TWO AS val
    FROM saturday_combinations
    WHERE TWO IS NOT NULL 
    order by TWO desc limit 4)
    
    UNION 
    (SELECT THREE AS val
    FROM saturday_combinations
    WHERE THREE IS NOT NULL 
    order by THREE desc limit 4)
    

    If answer to my comment is no, then please clarify.

    Here is the code based on your sample date:

    select distinct x.one as uniqunumbers,
    count(x.one) as counts
    from(
    sELECT DISTINCT 'one' 
    AS col1, one FROM sat_comb
    UNION ALL
    SELECT DISTINCT 'two' 
    AS col1, two FROM sat_comb
    UNION ALL
    SELECT DISTINCT 'three' 
    AS col1, three FROM sat_comb
    ) as x
    group by x.one;
    
    UNIQUNUMBERS    COUNTS
    1               1
    3               2
    4               1
    5               1
    7               1
    

    EDIT as per OP has clarified and updated the question.

    Quoted: “However I want to limit it so that it first takes the last n rows and then does the count on the values in those n rows. This means, if I have 3 columns with 3000 rows and 35 integers randomly appearing in these 3000 rows it should count how many times each integer appears.”

    • SQLFIDDLE DEMO2

    Query:

    select x.one as uniqunumbers,
        count(x.one) as counts
        from(
        (sELECT DISTINCT 'one' 
        AS col1, one FROM sat_comb
         order by id desc limit 4)
        UNION ALL
        (SELECT DISTINCT 'two' 
        AS col1, two FROM sat_comb
         order by id desc limit 4)
        UNION ALL
        (SELECT DISTINCT 'three' 
        AS col1, three FROM sat_comb
         order by id desc limit 4)
          UNION ALL
        (SELECT DISTINCT 'four' 
        AS col1, four FROM sat_comb
         order by id desc limit 4)
          UNION ALL
        (SELECT DISTINCT 'five' 
        AS col1, five FROM sat_comb
         order by id desc limit 4)
          UNION ALL
        (SELECT DISTINCT 'six' 
        AS col1, six FROM sat_comb
         order by id desc limit 4)
          UNION ALL
        (SELECT DISTINCT 'seven' 
        AS col1, seven FROM sat_comb
         order by id desc limit 4)
        ) as x
        group by x.one;
    

    Output:

    UNIQUNUMBERS    COUNTS
    2               4
    3               3
    4               3
    5               4
    6               4
    8               3
    9               4
    20              3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
i have following query SELECT *, count(jx_commissions.commission_amount) AS summe FROM jx_members INNER JOIN jx_commissions
I have following SQL query: SELECT Count(*) AS CountOfRecs FROM tblAccount INNER JOIN tblAccountOwner
I have the following query: select count(L.ID) from LA inner join L on (LA.leadid
I have the following query: SELECT DISTINCT w.name, count(*) FROM widgets AS w JOIN
I have the following query: SELECT dev.DeviceName, Count(dom.DomainID) AS CountOfDomains FROM tblDevices dev JOIN
i have the following query SELECT DISTINCT dr.Revision FROM tblActionHeader ah INNER JOIN tblActionType
I have the following query: SELECT dm.app_id, apt.app_name, COUNT(dm.app_id) FROM dm_openapp dm JOIN app_table
I have following SQL Query SELECT * FROM KDMS_dynamic vd INNER JOIN KDMS_definition tblKDMS
I have the following query: select * from player_source where source is not null

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.