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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:46:08+00:00 2026-06-14T00:46:08+00:00

I am fairly new to SQL Server and trying to do something that seems

  • 0

I am fairly new to SQL Server and trying to do something that seems like it ought to be fairly basic, but I must be missing a significant concept.

First, here is the story:

  • I have table X which contains line items from orders
  • I have table Y which contains the orders themselves

I want to divide the number of line items by the number of orders, thus generating the number of line items on an average order.

I’ve tried a few options:

  1. I tried joining the two tables together, and then dividing the count() of X by the count of Y. Unfortunately, by X to Y it creates an entry for every Y, so the result of the division is one.

    select count(x) / count (y) 
    from x 
    join y
    
  2. I tried making half of the select statement “distinct” by trying to count “distinct” orders but not distinct line items. Turns out you can’t make half the select statement be distinct.

    select count(x) / distinct count (y) 
    from x 
    join y
    
  3. I tried making a new table, and outputting the sum of the number of orders into the new table. Then I tried to go divide the number of line items by the value of that table. That doesn’t work unless I join the tables together, which returns me to the original problem.

    select count(x) / temptable
    from x
    join temptable
    
  4. I tried looking up how to output a SQL statement to a variable so I could then use that variable for the division, but got nowhere on that.

  5. I have read diligently through lots of stack overflow posts that hint at this issue but don’t actually resolve it.

Any help will be sincerely appreciated!

  • 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-14T00:46:09+00:00Added an answer on June 14, 2026 at 12:46 am

    The easiest way to do this is without a subquery:

    select count(*) * 1.0 / count(distinct order_id)
    from line_items
    

    This assumes that all orders have at least one line item. If this is not the case, you can do:

    select count(*) * 1.0 / (select count(*) from orders)
    from line_items
    

    Or, if you don’t like subqueries in select clauses, you can use a join instead:

    select count(li.line_item_id)*1.0/count(distinct o.order_id)
    from orders o left outer join
         line_items li
         on o.order_id = li.order_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to SQL, but trying to write quite a complicated query, but
I fairly new to JQuery and perhaps trying to achieve something that might be
I am fairly new to SSAS and SQL Server but Google didn't help. I
We currently use SQL Server 2005 Enterprise for our fairly large application, that has
I'm fairly new to Visual Studio (specifically Visual Basic .NET) but have been programming
I'm fairly new to SQL Server and would really appreciate some help with this.
I am fairly new to StructureMap, but my understanding is that there are two
I am fairly new to sql and I've run into an error while trying
I'm fairly new to SQL Server and am still learning some of the tricks
Sorry for the stupid question but I am fairly new to SQL and I'm

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.