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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:25:56+00:00 2026-06-15T02:25:56+00:00

i have this query SELECT userId, orgQuery.timeUnit, @SUM := @SUM + orgQuery.orderValue AS sum,

  • 0

i have this query

SELECT userId, orgQuery.timeUnit, 
  @SUM := @SUM + orgQuery.orderValue AS sum, 
  @COUNT := @COUNT + 1 AS count, 
  @AVG := @SUM / @COUNT AS avg
    FROM (
    SELECT userid, orderValue,  
      DATE_FORMAT(`acceptDate`, '%Y%M') AS timeUnit
FROM `agreements` 
    WHERE userId = 4 
    AND acceptDate > 2000-00-00 
   GROUP BY timeUnit
)
AS orgQuery, 
  (SELECT @COUNT := 0, @SUM := 0,@AVG :=0) 
 AS extra GROUP BY timeUnit

Output:

userId  timeUnit    sum count   avg
4         201001    6000    1   6000.0000
4         201003    12000   2   6000.0000
4         201004    19500   3   6500.0000

But as you can see, there is a gap between some dates and I want the output to be a contiguous range eg:

userId  timeUnit    sum count   avg
4         201001    6000    1   6000.0000
4         201002    0       2   3000.0000
4         201003    12000   3   6000.0000
4         201004    19500   4   4875.0000

This Query

(SELECT DATE_FORMAT(`acceptDate`, '%Y%M') AS timeUnit FROM `agreements` GROUP BY timeUnit )

Outputs the full date range, but when I try to LEFT JOIN the two Querys, the count and avg gets all messed up. How can I get the result am looking for?

  • 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-15T02:25:57+00:00Added an answer on June 15, 2026 at 2:25 am

    Assuming you are missing an entry in your agreements table for 2012-02, it’s always good to have a table containing simply dates.

    CREATE TABLE dates(`date` date primary key);
    
    DROP PROCEDURE IF EXISTS insertDates;
    DELIMITER $$
    CREATE PROCEDURE insertDates()
    BEGIN
    SET @start_date = '2010-01-01';
    WHILE (@start_date <= '2010-12-31') DO
    INSERT INTO dates VALUES (@start_date);
    SET @start_date:=DATE_ADD(@start_date, INTERVAL 1 DAY);
    END WHILE;
    END $$
    DELIMITER ;
    
    CALL insertDates();
    

    Adjust the date range according to your needs.

    Then you can write your query like the following. I simplified it a bit, since I saw no need for your variables or the subquery.

    SELECT userId, DATE_FORMAT(dates.`date`, '%Y%M') AS timeUnit, 
      SUM(orderValue), 
      COUNT(orderValue),
      AVG(orderValue)
    FROM 
    dates LEFT JOIN
    `agreements` ON dates.date = agreements.acceptDate
      WHERE userId = 4 
      AND acceptDate > '2000-00-00'
    GROUP BY userId, timeUnit
    

    UPDATE:

    SELECT userId, orgQuery.timeUnit, 
      @SUM := @SUM + orgQuery.orderValue AS sum, 
      @COUNT := @COUNT + 1 AS count, 
      @AVG := @SUM / @COUNT AS avg
        FROM (
        SELECT userid, orderValue,  
          DATE_FORMAT(dates.`date`, '%Y%M') AS timeUnit
    FROM dates LEFT JOIN
    `agreements` ON dates.date = agreements.acceptDate
        WHERE userId = 4 
        AND acceptDate > '2000-00-00' 
       GROUP BY timeUnit
    )
    AS orgQuery, 
      (SELECT @COUNT := 0, @SUM := 0,@AVG :=0) 
     AS extra GROUP BY timeUnit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: SELECT COUNT(*) AS invoice_count, IFNULL(SUM(qa_invoices.invoice_total), 0) AS invoice_total, IFNULL(SUM(qa_invoices.invoice_discount) ,0)
I have query like this: SELECT * FROM activity WHERE (((userId = 1 OR
I have a query like this SELECT user_id FROM user WHERE user_id NOT IN
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have this query: select BUnit, value from myTable where BUnit in (555,556,557,558) and
I currently got this query: SELECT `users_sessions`.`userid` `users_sessions`.`lastActive` , `users`.`firstname` FROM `users_sessions` , `users`
I have the follow query: SELECT `peers`.uid, `user`.userid FROM `peers`, `user` WHERE `user`.userid =
I have this SELECT query: SELECT Auctions.ID, Users.Balance, Users.FreeBids, COUNT(CASE WHEN Bids.Burned=0 AND Auctions.Closed=0

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.