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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:18:11+00:00 2026-06-06T23:18:11+00:00

I have a select statement: select a, b, […]; which returns the results: a|b

  • 0

I have a select statement: select a, b, [...]; which returns the results:

a|b
---------
1|8688798
2|355744
4|457437
7|27834

I want it to return:

a|b
---------
1|8688798
2|355744
3|0
4|457437
5|0
6|0
7|27834

An example query that does not do what I would like, since it does not have the gap numbers:

select
    sub.num_of_ratings,
    count(sub.rater)
from
(
    select 
        r.rater_id as rater,
        count(r.id) as num_of_ratings
    from ratings r
    group by rater
) as sub
group by num_of_ratings;

Explanation of the query:
If a user rates another user, the rating is listed in the table ratings and the id of the rating user is kept in the field rater_id. Effectively I check for all users who are referred to in ratings and count how many ratings records I find for that user, which is rater / num_of_ratings, and then I use this result to find how many users have rated a given number of times.

At the end I know how many users rated once, how many users rated twice, etc. My problem is that the numbers for count(sub.rater) start fine from 1,2,3,4,5… However, for bigger numbers there are gaps. This is because there might be one user who rated 1028 times – but no user who rated 1027 times.

I don’t want to apply stored procedures looping over the result or something like that. Is it possible to fill those gaps in the result without using stored procedures, looping, or creating temporary tables?

  • 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-06T23:18:13+00:00Added an answer on June 6, 2026 at 11:18 pm

    @mazzucci: the query is too magical and you are not actually explaining the query.

    @David: I cannot create a table for that purpose (as stated in the question)

    Basically what I need is a select that returns a gap-less list of numbers. Then I can left join on that result set and treat NULL as 0.

    What I need is an arbitrary table that keeps more records than the length of the final list. I use the table user for that in the following example:

    select @row := @row + 1 as index
        from (select @row := -1) r, users u
    limit 101;
    

    This query returns a set of the numbers von 0 to 100. Using it as a subquery in a left join finally fills the gap.

    users is just a dummy to keep the relational engine going and hence producing the numbers incrementally.

    select t1.index as a, ifnull(t2.b, 0) as b
    from (
        select @row := @row + 1 as index
            from (select @row := 0) r, users u
        limit 7
    ) as t1
    left join (
        select a, b [...]
    ) as t2
    on t1.index = t2.a;
    

    I didn’t try this very query live, so have merci with me if there is a little flaw. but technically it works. you get my point.

    EDIT:

    just used this concept to gain a gapless list of dates to left join measures onto it:

    select @date := date_add(@date, interval 1 day) as date
        from (select @date := '2010-10-14') d, users u
    limit 700
    

    starts from 2010/10/15 and iterates 699 more days.

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

Sidebar

Related Questions

I have a SELECT statement similar to the one below which returns several counts
Suppose I have a SELECT statement that returns some set of results. Is there
I have 2 select statements which I want to grep the results of both
I have a select statement which will return me 5 values as val1,val3,val5,val2,val4 I
I have a select statement running which pulls data into $row. $result = mysql_query(Select
I have a select statement and in a couple of the fields, I want
I have a select statement, retrieve about 1000 record I want to modify it
I have a select statement where I want to make the select conditional like
I have a select statement select someFields from table1,table2 which is a cartesian join,
Where can I see example of scala.dbc update statement? I have found select statement

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.