Solved

MS SQL Query Help

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 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