Solved

SQL Query using Group

Posted on 2013-11-15
4
275 Views
Last Modified: 2013-11-15
What is the needed Query statement to find the total number of orders per person

Given:

Table A:
ID      OrderNumber
___________________
1      X123
2      X213
1      X345
2      x237
3      x554
4      x435
1      x333

Table B:
ID      FName      Lname
_____________________
1      Jon         Smith
3      Al            Berry
2      Sue        Jones
4      Jack        Jill

Result Table:
Name            Count
_____________________
Jon Smith      3
Sue Jones      2
Al Berry         1
Jack Jill           1
0
Comment
Question by:williambhowardjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39652685
select b.FName , b.Lname, count(*) as Count_of
from table_b as b
inner join table_a as a on b.id = a.id
group by b.FName , b.Lname
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39652688
or:

select b.FName + ' ' + b.Lname as FullName, count(*) as Count_of
from table_b as b
inner join table_a as a on b.id = a.id
group by b.FName + ' ' + b.Lname
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 200 total points
ID: 39652695
by the way, if you want all names, then the count of the orders placed
then use either of the above

however, if you are only interested in those names that have ordered, then reverse the table sequence e.g.

select b.FName + ' ' + b.Lname as FullName, count(*) as Count_of
from table_a as a
inner join table_b as b on a.id = b.id
group by b.FName + ' ' + b.Lname
0
 

Author Closing Comment

by:williambhowardjr
ID: 39652703
Works Great - Thanks for the Help.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 63
Replacing unrecognized function name in SQL Query 4 47
T-SQL: Wrong Result 7 38
SQL Job Failed 6 30
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question