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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jblah
Starting Member

11 Posts

Posted - 04/14/2008 :  22:30:32  Show Profile  Reply with Quote
Hello. I'm new to the world of SQL and SQL Servers.

I was trying to write a SQL Query to identify something specific but I'm not sure how to go about it.

Here is the situation:

I have about 8 tables for a Customer buying Products Database..but I am going to simplify this down.

So i have

Table: Customers
Columns: CustomerID, FirstName, Lastname (other stuff not important)

Table: Orders
Columns: OrderID, OrderDateTime, CustomerID, Tax

so basically I'm trying to write a query where I find the TOTAL Tax paid by a customer named 'John' 'Doe' (john doe has 3 orders so i need to SUM the taxes)

displaying in SQL FirstName, LastName, 'Total Tax'

sorry for being such a noob.

Thanks in advance for anyone that can help. i highly appreciate it.

I know that you can do the following:
SELECT SUM(TAX) as 'Total Tax' FROM Orders
INNER JOIN Customers ON Customers.FirstName = 'John' AND Customers.LastName = 'Doe'

but i don't know how to display FirstName and LastName along with the select statement.

i tried:
SELECT FirstName, LastName, SUM(TAX) as 'Total Tax' FROM Orders
INNER JOIN Customers ON Customers.FirstName = 'Jim' AND Customers.LastName = 'Taylor'
but that doesn't work it gives me an error
Column 'Customers.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Edited by - jblah on 04/14/2008 22:59:55

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 04/14/2008 :  23:00:32  Show Profile  Reply with Quote
SELECT customers.FirstName, customers.LastName, SUM(TAX) as [Total Tax]
FROM Orders
INNER JOIN Customers ON Customers.CustomerID = orders.CustomerID
where Customers.FirstName = 'John'
AND Customers.LastName = 'Doe'
group by customers.FirstName, customers.LastName


Be One with the Optimizer
TG
Go to Top of Page

jblah
Starting Member

11 Posts

Posted - 04/14/2008 :  23:04:07  Show Profile  Reply with Quote
quote:
Originally posted by TG

SELECT customers.FirstName, customers.LastName, SUM(TAX) as [Total Tax]
FROM Orders
INNER JOIN Customers ON Customers.CustomerID = orders.CustomerID
where Customers.FirstName = 'John'
AND Customers.LastName = 'Doe'
group by customers.FirstName, customers.LastName


Be One with the Optimizer
TG



thanks that was easy enough. I didn't know about the Group By statement. I guess i should read up on that. I had everything before that up to that point.
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.05 seconds. Powered By: Snitz Forums 2000