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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:50:30+00:00 2026-06-08T11:50:30+00:00

SELECT b.cID, b.ID, Count(r.userRead) AS readCount, COUNT(DISTINCT r.userID) AS UserCount, Count(c.userDownload) AS downloadCount, COUNT(DISTINCT

  • 0
SELECT
b.cID,
b.ID,
Count(r.userRead) AS readCount,
COUNT(DISTINCT r.userID) AS UserCount,
Count(c.userDownload) AS downloadCount,
COUNT(DISTINCT c.userID) AS userDownloadCount
FROM
 book AS b
  INNER JOIN book_event AS r ON r.bookID=s.ID AND r.bookRead = 1
  INNER JOIN book_event as c ON c.bookID=s.ID AND c.bookDownload = 1
WHERE
 b.cID = 1011
GROUP BY
 b.ID
ORDER BY
 b.ID DESC

this SQL query output (count’s problem)

+-----------+-----+-----------+-----------------+--------------+-------------------+
| cID       | ID  | readCount | UserCount       | downloadCount| userDownloadCount |
+-----------+-----+-----------+-----------------+--------------+-------------------+
|      1011 | 278 |      3168 |              67 |         3168 |                19 |
|      1011 | 272 |      9918 |             122 |         9918 |                41 |
|      1011 | 241 |     31694 |              99 |        31694 |                38 |
+-----------+-----+-----------+-----------------+--------------+-------------------+
3 rows in set

real value

+-----------+-----+-----------+-----------------+--------------+-------------------+
| cID       | ID  | readCount | UserCount       | downloadCount| userDownloadCount |
+-----------+-----+-----------+-----------------+--------------+-------------------+
|      1011 | 278 |       133 |              67 |           24 |                19 |
|      1011 | 272 |       174 |             122 |           57 |                41 |
|      1011 | 241 |       299 |              99 |          106 |                38 |
+-----------+-----+-----------+-----------------+--------------+-------------------+

book_event (table)

+-----+--------+----------+--------------+
| ID  | userID | userRead | userDownload |
+-----+--------+----------+--------------+
| 278 |   5169 | 1        | 0            |
| 278 |   5169 | 0        | 1            |
| ... |   .... | .        | .            |
| 278 |   5628 | 1        | 0            |
| 278 |   5162 | 1        | 0            |
+-----+--------+----------+--------------+

I need to get the count grouped on two columns. readCount and downloadCount columuns is not correct but UserCount, userDownloadCount columuns value is correct.

how can i fix this problem?

  • 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-08T11:50:32+00:00Added an answer on June 8, 2026 at 11:50 am

    This is because you have multiple read and download events on the same book, so your query is generating a cross product of events.

    A good way to fix this is to aggregate the pieces of information separately. However, your query offers an easier solution. Just join to the book_event table once and then count the different events.

    SELECT b.cID, b.ID,
           sum(be.bookRead) as readCount,
           count(distinct case when be.bookRead = 1 then be.userId end) as UserCount,
           sum(be.userDownload) as downloadCount,
           count(distinct case when be.userDownload = 1 then be.userId end) as userDownloadCount
    FROM book AS b INNER JOIN
         book_event be
         on be.bookID = s.ID
    WHERE b.cID = 1011
    GROUP BY b.ID, b.cid
    ORDER BY b.ID DESC
    

    I added b.cid to the group by clause. It is good form to include all non-aggregated values in the SELECT clause in the GROUP BY. Other databases enforce this, and the rule is standard SQL.

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

Sidebar

Related Questions

I have query SELECT helpc.cid, COUNT(help.cid) AS children_count FROM air_help_category AS helpc LEFT JOIN
I have the following script select c.id from .TBL_COUPONS. as c inner join .TBL_BUSINESS.
SELECT `name`, count(`cid`) AS count, Min(`price-3`) AS min, Max(`price-3`) AS max FROM ed_prices WHERE
my sql statement is SELECT c.type,c.title,c.datereg, d.ranknum FROM T_News AS c INNER JOIN (
Is this: Dim mydata = (From c In dc.Table Select New With {c.ID}).Count any
SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =
Without the third join D.cid = C.id, this query gives me the count of
I have the following code: SELECT c.c_id, c_name, count(h.h_id) AS count FROM c LEFT
Using SQLite. SELECT c.*, COUNT(m.course_id) AS meeting_count, COUNT(r.meeting_id) AS race_count FROM course c LEFT
I have the following query: select top 100 eid, cid, id, position, ROW_NUMBER() over(order

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.