Solved

MS SQL Query Help

Posted on 2008-10-26
3
217 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
3 Comments
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 125 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server computed columns 11 34
What's wrong with this T-SQL Foreign Key? 7 51
date diff with Fiscal Calendar 4 56
performance query 4 32
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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