0

I would like to perform mathematical operations along with fetching records from table.

Example : I have table with below data

| orderid | value | orderdate    |
 ----------------------------------
  1       | 100   |  2013-08-08  | 
  2       | 200   |  2013-08-05  | 
  3       | 300   |  2013-08-06  | 
  4       | 400   |  2013-08-09  | 

And I am looking to sum(value) along with fetching all records from table. Example output

sum(value)| orderid | value | orderdate    |
 -------------------------------------------
  1000    | 1       | 100   |  2013-08-08  | 
  1000    | 2       | 200   |  2013-08-05  | 
  1000    | 3       | 300   |  2013-08-06  | 
  1000    | 4       | 400   |  2013-08-09  | 
1
  • Please note 1000 is sum of value of all fetched records. Commented Aug 13, 2013 at 6:09

2 Answers 2

3

you can use CROSS JOIN.

SELECT  b.TotalValue, a.*
FROM    tableName a
        CROSS JOIN 
        (SELECT SUM(value) totalValue FROM tableName) b

or a correlated subquery,

SELECT  (SELECT SUM(value) FROM tableName) totalValue, 
        a.*
FROM    tableName a
Sign up to request clarification or add additional context in comments.

Comments

0

Try This

SELECT a.*,SUM(b.value) FROM tablename AS a,tablename AS b GROUP BY a.orderid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.