How do I join two tables and group by a field called 'Project'?

How do I write a query to join two tables and group them?
VBBRettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Depends on the tables, of course.  Please post the structure of the two tables involved.
0
VBBRettAuthor Commented:
I just want a basic example that's all.
0
Patrick MatthewsCommented:
SELECT <some fields>
FROM Table1 INNER JOIN
    Table2 ON <some fields>
GROUP BY <something>
0
dbbishopCommented:
Any columns that you name in your SELECT list that are not aggregate functions (e.g. COUNT(), MIN(), MAX(), SUM()) must be named in the GROUP BY clause.

The JOIN is usually done on matching primary/foreign keys.

example:
Employee
EmpID, LastName, FirstName, SSN, HireDate

License
LicenceID
EmpID
LicenseName
ExpireDate

SELECT LastName, FirstName, LicenseName, ExpireDate
FROM Employee JOIN License ON Employee.EmpID = License.EmpID


With grouping, lets say yo wanted a count of licenses by employee, you'd use:

SELECT EmpID, LastName, FirstName, COUNT(*) AS [Count]
FROM Employee JOIN License ON Employee.EmpID = License.EmpID
GROUP BY EmpID, LastName, FirstName

EmpID, LastName and FirstName all need to be in the GROUP BY clause since there is no aggregate function used with them. You can bypass this in a case like above, by using something like:

SELECT EmpID, MAX(LastName) AS LastName, MAX(FirstName) AS FirstName, COUNT(*) AS [Count]
FROM Employee JOIN License ON Employee.EmpID = License.EmpID
GROUP BY EmpID

since, for any given EmpID the last name and first name will ALWAYS be the same.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.