?
Solved

SQL Query using Group

Posted on 2013-11-15
4
Medium Priority
?
278 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 49

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 49

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 49

Accepted Solution

by:
PortletPaul earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

800 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