SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 [SOLVED]Calculating total (Quantity * Price)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

massimoo
Starting Member

2 Posts

Posted - 01/02/2013 :  04:49:01  Show Profile  Reply with Quote
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 : )

Edited by - massimoo on 01/02/2013 05:58:11

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 01/02/2013 :  05:06:43  Show Profile  Reply with Quote


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




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

massimoo
Starting Member

2 Posts

Posted - 01/02/2013 :  05:58:31  Show Profile  Reply with Quote
Thanks a lot, worked like a charm!
Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 01/02/2013 :  06:50:37  Show Profile  Reply with Quote
you are welcome..

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

madhivanan
Premature Yak Congratulator

India
22754 Posts

Posted - 01/03/2013 :  06:29:19  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

USA
649 Posts

Posted - 01/09/2013 :  19:21:59  Show Profile  Reply with Quote
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."

Edited by - Jeff Moden on 01/09/2013 19:24:41
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000