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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:41:13+00:00 2026-05-30T03:41:13+00:00

I have 2 tables (it is only part of table columns that are needed

  • 0

I have 2 tables (it is only part of table columns that are needed for this query):

Items: Id int, OwnerId int
ItemsRelations: RelationId int, ItemId Int (FK Items(Id))

For each OwnerId i need to calculate result of ratio function:
Count of user elements that have links to ItemsRelationstable / count of all user elements.

I wrote this query, but it is slow and not optimal. Is there easier and faster method to do that?

SELECT OwnerId , (100 * SUM(HasRelation))/COUNT(Id) AS Ratio
FROM (
  SELECT 
    oi.OwnerId,
    oi.Id,
    (CASE WHEN SUM(ir.Id) > 0 THEN 1 ELSE 0 END) HasRelation
  FROM Items AS oi
  LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
  GROUP BY oi.Id, oi.OwnerId) tempTab 
GROUP BY OwnerId 
  • 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-30T03:41:15+00:00Added an answer on May 30, 2026 at 3:41 am
    SELECT oi.OwnerId, Ratio = 1.0 * Count(ir.ItemId) / Count(*)
    FROM Items AS oi
    LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
    GROUP BY oi.OwnerId
    

    Notes:

    • 1.0 is added to convert the expression into a float result
    • Count(ir.ItemId) skips NULLS, so it counts only those that have a relationship

    If Items to ItemsRelations is one to many, then you may need to DISTINCT it

    SELECT oi.OwnerId, Ratio = 1.0 * Count(DISTINCT ir.ItemId) / Count(DISTINCT oi.Id)
    FROM Items AS oi
    LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
    GROUP BY oi.OwnerId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four different tables, one main SPECIAL table containing only id's that reference
I have two tables and I want to display only the child ID that
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign
I have a database that has 16 tables, but only four are relevant to
I have an HTML/XML table generated by AJAX that displays limited columns of a
I have a table called excel that has 3 columns, name , id ,
I want to join two tables together and have ONLY the data in Table
I have a table that contains records that can become part of a bill.
I have a messages table that is structured somewhat like this: from | to
I have the following tables with their columns (only the relevant columns are listed):

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.