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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:47:24+00:00 2026-06-05T04:47:24+00:00

I have a query like below: Result gives @sold_count:=SUM(I.quantity) = 10, but @sold_count =

  • 0

I have a query like below:

Result gives @sold_count:=SUM(I.quantity) = 10, but @sold_count = 0,

so calculations are all 0.

What should be wrong here?

SET @sold_count :=0;
SELECT 
@sold_count:=SUM(I.quantity),
@sold_count,I.from_widget,COUNT(from_widget) as order_count,
(@sold_count * buy_price) as ciro,
(@sold_count * list_price) as liste_ciro,
(@sold_count * widget_price) as vitrin_ciro,
P.*
FROM 
 tbl_products P
LEFT JOIN tbl_order_items I on I.product_id = P.id
WHERE
 P.publish_date BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 3 MONTH ) AND DATE_SUB( CURDATE(    ) ,INTERVAL 0 MONTH )
GROUP BY I.from_widget,I.product_id
ORDER BY publish_date DESC
  • 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-05T04:47:26+00:00Added an answer on June 5, 2026 at 4:47 am

    Don’t use variables. Just:

    SELECT 
        SUM(I.quantity),
        I.from_widget, 
        COUNT(from_widget)             AS order_count,
        SUM(I.quantity) * buy_price    AS ciro,
        SUM(I.quantity) * list_price   AS liste_ciro,
        SUM(I.quantity) * widget_price AS vitrin_ciro,
        P.*
    FROM 
        tbl_products P
      LEFT JOIN tbl_order_items I 
          ON I.product_id = P.id
    WHERE
     P.publish_date BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 3 MONTH ) 
                        AND DATE_SUB( CURDATE( ) , INTERVAL 0 MONTH )
    GROUP BY I.from_widget,
             I.product_id
    ORDER BY publish_date DESC ;
    

    You could also make the query a nested one, if you don’t like using SUM(quantity) many times:

    SELECT 
        sum_quantity * buy_price    AS ciro,
        sum_quantity * list_price   AS liste_ciro,
        sum_quantity * widget_price AS vitrin_ciro,
        tmp.*
    FROM
      ( SELECT 
            SUM(I.quantity)                AS sum_quantity,
            I.from_widget, 
            COUNT(from_widget)             AS order_count,
            buy_price,
            list_price,
            widget_price,
            P.*
        FROM 
            tbl_products P
          LEFT JOIN tbl_order_items I 
              ON I.product_id = P.id
        WHERE
         P.publish_date BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 3 MONTH ) 
                            AND DATE_SUB( CURDATE( ) , INTERVAL 0 MONTH )
        GROUP BY I.from_widget,
                 I.product_id
      ) AS tmp
    ORDER BY publish_date DESC ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result from a query like the below, which does not have
I have a result of an sql query.it returns some 10 rows like below:
I have a query like below declare @str_CustomerID int Insert into IMDECONP38.[Customer].dbo.CustomerMaster ( CustomerName
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have a sql server query question. I have a table like below. Giving
I have written an SQL Query that brings the result in below format ResourceName
I have a php problem, I like to make the result as below: 123*thomas*Tiffany*Catherine*EnEn*Gabriel*Elizabeth*Ah
In the most general of cases, I have a query like below: SELECT tutor_school.name,
I have below two query's SUM the values Query1:* SELECT SUM(price) FROM TABLE1 WHERE
I have a query like this: select empno,name from emp where job = 'CLERK'

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.