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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:01:08+00:00 2026-05-31T12:01:08+00:00

In my SQLite statement below there is a problem with division by zero in

  • 0

In my SQLite statement below there is a problem with division by zero in the case count(t2.ref_id) is zero.

Could I adjust the SQLite statement so that if the count(t2.ref_id) is zero the scarsity (scarsity factor) will be higher than the highest non-zero scarsity?

select t1.id, cast(:totalItems as float) / count(t2.ref_id) as scarsity
from t1 left join t2 on t1.id = t2.ref_id
group by t1.id
order by scarsity
  • 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-31T12:01:10+00:00Added an answer on May 31, 2026 at 12:01 pm

    You need to check for the value 0 and do something different. One common way is to divide by NULL as many SQL varieties have NULLIF() to turn a value into a NULL instead.

    select t1.id, cast(:totalItems as float) / NULLIF(count(t2.ref_id), 0) as scarsity
    from t1 left join t2 on t1.id = t2.ref_id
    group by t1.id
    order by scarsity
    

    But I’m not sure that SQLite has NULLIF(), so you could use the more long winded CASE version instead…

    select t1.id, cast(:totalItems as float) / CASE WHEN count(t2.ref_id) = 0 THEN NULL ELSE count(t2.ref_id) END as scarsity
    from t1 left join t2 on t1.id = t2.ref_id
    group by t1.id
    order by scarsity
    

    The COUNT() won’t be calculated twice even though you typed it twice. The value will be re-used 🙂

    Alternatively, do something completely different if you get 0 rows…

    select t1.id, CASE WHEN count(t2.ref_id) = 0 THEN 9999999 ELSE cast(:totalItems as float) / count(t2.ref_id) END as scarsity
    from t1 left join t2 on t1.id = t2.ref_id
    group by t1.id
    order by scarsity
    

    Then you can check for NULLs as being the highest value, or just check for 9999999, etc, etc.

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

Sidebar

Related Questions

Could you please help me with the unknownComputation in the SQLite statement below? select
I have a problem in sqlite wrapper method.. i have defined the below functions
The sql statement below will not run in SQLite: select * from A left
I have some codes read the query results from sqlite: while (sqlite3_step(statement) == SQLITE_ROW)
I have heard that prepared statements with SQLite should improve performance. I wrote some
sqlite uses something that the authors call Manifest Typing , which basically means that
SQLite docs specifies that the preferred format for storing datetime values in the DB
Using SQLite from .net, Is there a way to access the .dump command or
I'm trying to use sqlite in a 'data services' class like below. I keep
I'm creating a VB.Net application that stores data in a SQLite backend. The data

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.