Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 [SOLVED]Calculating total (Quantity * Price)

Author  Topic 

massimoo
Starting Member

2 Posts

Posted - 2013-01-02 : 04:49:01
Hi all,

I'm pretty new to SQL and I would need some help with following issue:

I have two tables:

SalesOrderHeader
SalesOrderLines

In the SalesOrderLines I have Quantity and Price

I would like to create a query that gets the SalesOrderId and calculates the total (qnt*price). SalesOrderId is the key.

SELECT
a.SalesOrderId, a.Customer, (TOTAL HERE SOMEHOW :)
FROM
SalesOrderHeader a, SalesOrderLines b
WHERE
a.SalesOrderId = b.SalesOrderId and
a.SalesOrderDate>=20130101




Thanks for the help : )

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-01-02 : 05:06:43
[code]

Try this ..

SELECT
a.SalesOrderId, a.Customer, sum(b.Quantity *b.Price)
FROM
SalesOrderHeader a, SalesOrderLines b
WHERE
a.SalesOrderId = b.SalesOrderId and
a.SalesOrderDate>=20130101
group by a.SalesOrderId, a.Customer


[/code]

Vijay is here to learn something from you guys.
Go to Top of Page

massimoo
Starting Member

2 Posts

Posted - 2013-01-02 : 05:58:31
Thanks a lot, worked like a charm!
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-01-02 : 06:50:37
you are welcome..

Vijay is here to learn something from you guys.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-01-03 : 06:29:19
If SalesOrderDate is of Datetime datatype, you need to use date values within single quotes

a.SalesOrderDate>='20130101'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2013-01-09 : 19:21:59
quote:
Originally posted by massimoo

Thanks a lot, worked like a charm!

The next question would be, "Do you understand how it works?"

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page
   

- Advertisement -