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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need Query Help

Author  Topic 

jblah
Starting Member

11 Posts

Posted - 2008-04-14 : 22:30:32
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.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-04-14 : 23:00:32
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 - 2008-04-14 : 23:04:07
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
   

- Advertisement -