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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:01:49+00:00 2026-05-15T05:01:49+00:00

I currently have a table which stores a load of statistics such as views,

  • 0

I currently have a table which stores a load of statistics such as views, downloads, purchases etc. for a multiple number of items. To get a single operation count on each item I can use the following query:

SELECT *, COUNT(*)
FROM stats
WHERE operation = 'view'
GROUP BY item_id

This gives me all the items and a count of their views. I can then change ‘view’ to ‘purchase’ or ‘download’ for the other variables. However this means three separate calls to the database.

Is it possible to get all three in one go?

  • 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-15T05:01:49+00:00Added an answer on May 15, 2026 at 5:01 am
    SELECT item_id, operation, COUNT(*) 
    FROM stats 
    WHERE operation IN ('view','purchase','download') 
    GROUP BY item_id, operation
    

    Will return a table with one line per item_id and operation, containing three columns: the item_id, the operation type, and the number of rows with that item_id.

    1 view 3
    1 purchase 5
    2 download 7
    3 download 1
    

    You can omit the WHERE if you want all item_id’s, and you can order in COUNT(*) to get the most popular or something. Depends what you are looking for or how you are using the data.

    If you want the columns next to eachother, use an IF:

    SELECT s1.item_id, SUM( IF( s1.operation = 'view', 1, 0 ) ) views, SUM( IF( s1.operation = 'download', 1, 0 ) ) downloads, SUM( IF( s1.operation = 'purchase', 1, 0 ) ) purchases
    FROM stats s1
    GROUP BY s1.item_id
    
    item_id | views | downloads | purchases
    1 | 3 | 0 | 5
    2 | 0 | 7 | 0
    3 | 0 | 1 | 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently dynamically building an asp:table which contains checkboxes these checkboxes have a CheckChanged
Currently we have a table that we use to track inivitations. We have an
Currently I have a table that I search upon 4 fields, FirstName, LastName, MiddleName,
I have a table with almost 800,000 records and I am currently using dynamic
We have a number of databases which store 10s to 100s of gigabytes of
I have a task tracking application, which is very basic. It currently has a
I have a question about tables in MySQL. I'm currently making a website where
I have a pretty standard table set-up in a current application using the .NET
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
I currently have an MS Access application that connects to a PostgreSQL database via

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.