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.
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 haveTable: CustomersColumns: CustomerID, FirstName, Lastname (other stuff not important)Table: OrdersColumns: OrderID, OrderDateTime, CustomerID, Taxso 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 OrdersINNER 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 OrdersINNER JOIN Customers ON Customers.FirstName = 'Jim' AND Customers.LastName = 'Taylor'but that doesn't work it gives me an errorColumn '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.CustomerIDwhere Customers.FirstName = 'John' AND Customers.LastName = 'Doe'group by customers.FirstName, customers.LastNameBe One with the OptimizerTG |
|
|
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.CustomerIDwhere Customers.FirstName = 'John' AND Customers.LastName = 'Doe'group by customers.FirstName, customers.LastNameBe One with the OptimizerTG
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. |
|
|
|
|
|
|
|