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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:43:55+00:00 2026-06-10T13:43:55+00:00

I have 3 tables: shops , PRIMARY KEY cid,zbid shop_items , PRIMARY KEY id

  • 0

I have 3 tables:

shops, PRIMARY KEY cid,zbid
shop_items, PRIMARY KEY id
shop_inventory, PRIMARY KEY id

shops a is related to shop_items b by the following: a.cid=b.cid AND a.zbid=b.szbid
shops is not directly related to shop_inventory

shop_items b is related to shop_inventory c by the following: b.cid=c.cid AND b.id=c.iid

Now, I would like to run a query which returns a.* (all columns from shops). That would be:

SELECT a.* FROM shops a WHERE a.cid=1 AND a.zbid!=0

Note that the WHERE clause is necessary.

Next, I want to return the number of items in each shop:

SELECT
  a.*,
  COUNT(b.id) items
FROM shops a
LEFT JOIN shop_items b ON b.cid=a.cid AND b.szbid=a.zbid
WHERE a.cid=1
GROUP BY b.szbid,b.cid

As you can see, I have added a GROUP BY clause for this to work.

Next, I want to return the average price of each item in the shop. This isn’t too hard:

SELECT
  a.*,
  COUNT(b.id) items,
  AVG(COALESCE(b.price,0)) average_price
FROM shops a
LEFT JOIN shop_items b ON b.cid=a.cid AND b.szbid=a.zbid
WHERE a.cid=1
GROUP BY b.szbid,b.cid

My next criteria is where it gets complicated. I also want to return the unique buyers for each shop. This can be done by querying shop_inventory c, getting the COUNT(DISTINCT c.zbid). Now remember how these tables are related; this should only be done for the rows in c which relate to an item in b which is owned by the respective shop, a.

I tried doing the following:

SELECT
  a.*,
  COUNT(b.id) items,
  AVG(COALESCE(b.price,0)) average_price,
  COUNT(DISTINCT c.zbid)
FROM shops a
LEFT JOIN shop_items b     ON b.cid=a.cid AND b.szbid=a.zbid
LEFT JOIN shop_inventory c ON c.cid=b.cid AND c.iid=b.id
WHERE a.cid=1
GROUP BY b.szbid,b.cid

However, this did not work as it messed up the items value. What is the proper way to achieve this result?

I also want to be able to return the total number of purchases made in each shop. This would be done by looking at shop_inventory c and adding up the c.quantity value for each shop. How would I add that in as well?

  • 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-10T13:43:56+00:00Added an answer on June 10, 2026 at 1:43 pm

    Try this solution:

    SELECT    a.*,
              COALESCE(b.item_cnt,  0) AS item_cnt,
              COALESCE(b.avg_price, 0) AS avg_price,
              COALESCE(b.buyer_cnt, 0) AS buyer_cnt
    FROM      shops a
    LEFT JOIN (
              SELECT    a.cid, 
                        a.szbid, 
                        COUNT(*)     AS item_cnt, 
                        AVG(a.price) AS avg_price,
                        b.buyer_cnt
              FROM      shop_items a
              LEFT JOIN (
                        SELECT   cid,
                                 iid,
                                 COUNT(DISTINCT zbid) AS buyer_cnt
                        FROM     shop_inventory
                        WHERE    cid = 1
                        GROUP BY cid, 
                                 iid
                        ) b ON a.cid = b.cid AND a.id = b.iid
              WHERE     a.cid = 1 AND
                        a.szbid <> 0
              GROUP BY  a.cid,
                        a.szbid
              ) b ON a.cid = b.cid AND a.zbid = b.szbid
    WHERE     a.cid = 1 AND 
              a.zbid <> 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: Student Data Student ID (primary key) Prior Education Student
I have following tables: CREATE TABLE IF NOT EXISTS stats ( date date NOT
I have 2 tables: Attendee(AttendeeId integer PRIMARY KEY, LastName, FirstName, etc...) Company(CompanyId integer PRIMARY
I have the following two tables in my database (the indexing is not complete
I have 3 SQLite tables. Each has an ID as primary key; if I
Lets say I have two tables, one for transactions, and another table who's primary
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
I have a table that has a primary key that's an INT... I have
I have 2 table database; Firt table's columns; ClubID(Primary key, auto-increment enabled), ClubName Second
I have an events table with two columns eventkey (unique, primary-key) and createtime ,

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.