Solved

MS SQL Query Help

Posted on 2008-10-26
3
216 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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