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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:46:32+00:00 2026-05-27T02:46:32+00:00

I have 3 tables in a query: apiusers, users these contain a user id,

  • 0

I have 3 tables in a query:

apiusers, users

  • these contain a user id, and a name.

download_stats:

this contains usertype (1 = users, 2 = apiusers), date, file downloaded_id.

What I want to do is get a layout of:

| user id | name | download stats for january | download stats for february

etc.

I’m currently trying with this query, which takes forever:

SELECT name,queryuserid,count(ROWA.id) ,count(ROWB.id) 
FROM (SELECT IFNULL(u.name, au.companyname) AS name,IFNULL(u.id, au.id) AS queryuserid
    FROM apiusers as au, users as u 
    GROUP BY 2 ) AS users 
LEFT JOIN stats_download as ROWA ON ROWA.userid=queryuserid AND ROWA.date > date('2011-01-01') AND ROWA.date < date('2011-02-01') 
LEFT JOIN stats_download as ROWB ON ROWB.userid=queryuserid AND ROWB.date > date('2011-01-01') AND ROWB.date < date('2011-02-01') 
GROUP BY 2;

Is there a better way of going about this? The client wants supports to “group” the output statistics by year, month and day. So there could potentionally be 30+ LEFT JOIN’s in there.

  • 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-27T02:46:32+00:00Added an answer on May 27, 2026 at 2:46 am

    This should get you the basis, and runs an entire year sample. Look at the pattern. By doing a sum of a qualified IF() condition returning 1 or 0 you get the total count per each month. Now, if you are looking for SIZE (such as download size), instead of 1, 0, you can just substitute the fileSize instead of 1 and you’ll have the total downloaded size… put them as different columns and you can have both Count of downloads and totalSize of downloads. Expand the date range over year, just keep going with the pattern…

    SELECT 
          AllUsers.Name,
          AllUsers.QueryUserID,
          SUM( if( SD.Date BETWEEN '2011-01-01' and '2011-01-31', 1, 0 )) as CountJan2011,
          SUM( if( SD.Date BETWEEN '2011-02-01' and '2011-02-28', 1, 0 )) as CountFeb2011,
          SUM( if( SD.Date BETWEEN '2011-03-01' and '2011-03-31', 1, 0 )) as CountMar2011,
          SUM( if( SD.Date BETWEEN '2011-04-01' and '2011-04-30', 1, 0 )) as CountApr2011,
          SUM( if( SD.Date BETWEEN '2011-05-01' and '2011-05-31', 1, 0 )) as CountMay2011,
          SUM( if( SD.Date BETWEEN '2011-06-01' and '2011-06-30', 1, 0 )) as CountJun2011,
          SUM( if( SD.Date BETWEEN '2011-07-01' and '2011-07-31', 1, 0 )) as CountJul2011,
          SUM( if( SD.Date BETWEEN '2011-08-01' and '2011-08-31', 1, 0 )) as CountAug2011,
          SUM( if( SD.Date BETWEEN '2011-09-01' and '2011-09-30', 1, 0 )) as CountSep2011,
          SUM( if( SD.Date BETWEEN '2011-10-01' and '2011-10-31', 1, 0 )) as CountOct2011,
          SUM( if( SD.Date BETWEEN '2011-11-01' and '2011-11-30', 1, 0 )) as CountNov2011,
          SUM( if( SD.Date BETWEEN '2011-12-01' and '2011-12-31', 1, 0 )) as CountDec2011
      FROM
          ( select distinct 
                    APIUsers.CompanyName as Name,
                    APIUsers.QueryUserID
               from APIUsers
            UNION
            select 
                  Users.Name,
                  Users.QueryUserID
               from Users ) AllUsers
    
          LEFT JOIN stats_download SD
              ON AllUsers.QueryUserID = SD.UserID
             AND SD.Date BETWEEN '2011-01-01' and '2011-12-31'
       GROUP BY 
          2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these 4 tables: This query var query = db.Authors .Where(x=> x.ItemAuthors .Any(z=>
I have three tables. This query will write down the right answer (x-lines for
So I have two tables students (PK sID) and mentors (PK pID). This query
I have tables & data like this: venues table contains : id +----+---------+ |
I have 4 tables and this query SELECT tag.id, title FROM tag LEFT JOIN
I have two tables involved in this query I need to create, and I'm
We have two tables of interest in this query. First off we have a
I have my tables: aspnet_UsersInRoles aspnet_Role aspnet_Users ApiUsers I want to select only users
I have some tables. Most important ones in this JOIN query are: table 'changes'
I have two tables that I am joining with the following query... select *

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.