?
Solved

MS SQL Query Help

Posted on 2008-10-26
3
Medium Priority
?
221 Views
Last Modified: 2012-05-05
I need some help with syntax for an SQL query.(MS SQL 2000)
There are 2 tables:  PEOPLE and ACTIVITY

PEOPLE contains all the staff (one person per record)

ACTIVITY lists things they have done.  There are two fields linking (matching) back to the PEOPLE.USERID field that are ACTIVITY.SALES and ACTIVITY.CLERK. Sometimes the same person can do both functions, sometimes SALES and CLERK are different PEOPLE.

I want to extract the "Full_Name" from the PEOPLE table matching the PEOPLE.USERID to either ACTIVITY.SALES or ACTIVITY.CLERK.

Here is what I have thus far:

  SELECT
  ACTIVITY.TRANSNUMBER AS 'Trans',
  PEOPLE.FULLNAME AS 'SALES',
  PEOPLE.FULLNAME AS 'CLERK'
  FROM ACTIVITY,PEOPLE
  WHERE
  (activity.sales=people.userid or activity.clerk=people.userid)
  order by activity.transnumber

I'm getting only one person and one record listed and I know that there are multiple Activities per person and Activities where SALES and CLERK are different people.
0
Comment
Question by:BJMcDonald
[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 Comments
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 500 total points
ID: 22808313
try this one:
SELECT 
  ACTIVITY.TRANSNUMBER AS 'Trans',
  pplSales.FULLNAME AS 'SALES',
  pplClerk .FULLNAME AS 'CLERK'
  FROM ACTIVITY
  left join PEOPLE pplSales on ACTIVITY.sales = pplSales.UserId
  left join PEOPLE pplClerk on ACTIVITY.clerk = pplClerk .UserId
 order by activity.transnumber

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22808352
No points please.  But I wanted to confirm that cvijo123's method is the correct way to achieve this.

Just one minor correction, which I'm sure was a copy/paste error, is the space removed after pplClerk and before .FULLNAME as 'CLERK'.
SELECT 
  ACTIVITY.TRANSNUMBER AS 'Trans',
  pplSales.FULLNAME AS 'SALES',
  pplClerk.FULLNAME AS 'CLERK'
  FROM ACTIVITY
  left join PEOPLE pplSales on ACTIVITY.sales = pplSales.UserId
  left join PEOPLE pplClerk on ACTIVITY.clerk = pplClerk .UserId
 order by activity.transnumber

Open in new window

0
 

Author Closing Comment

by:BJMcDonald
ID: 31510134
Thanks so much .... I feel pretty stupid for not considering the inner join and creating the temporary fields.  Too much coffee and not enough sleep.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

801 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