SQL Alias Use

Posted on 2015-01-22
Last Modified: 2015-01-22
Using SQL Server, I'm attempting to put a query together that gets most of it's data from a main table and some related data from another table. The main table has two fields in it, both of which have related information in the other table. By way of example, the main table, we will call it the jobs table, has two fields in it, one for the id of the person who created the record and the other for the id  person who updated the record. The other table, call it the people table, contains the names of all people and their phone numbers.

In my result set, I want a list of every job in the jobs table together with the ids, names, and phones of their creators and updaters. The tables look kind of like this:

Jobs Table
Job ID
Job Name
Creator ID
Creator Name
Creator Phone
Updater ID
Updater Name
Updater Phone

People Table
People ID

The Query that I've put together and that isn't working is as follows:

      jobs.jobid, jobs.jobname, jobs.creatorid, jobs.updaterid, as updater_name, as updater_phone, as creator_name, as creator_phone
left outer join
     people as updater
    updater.peopleid = jobs.updaterid
left outer join
    people as creator
    creator.peopleid = jobs.creatorid

SQL complains that the name and phone columns in the select statement are invalid.

Any help would be appreciated.
Question by:cbridgman
LVL 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 40565298
      jobs.jobid, jobs.jobname, jobs.creatorid, jobs.updaterid, as updater_name, as updater_phone, as creator_name, as creator_phone
left outer join
     people as updater
    updater.[People ID] = jobs.[Updater ID]
LVL 24

Accepted Solution

Phillip Burton earned 250 total points
ID: 40565306
Try putting name and phone in hard brackets, e.g. Updated.[name]
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40565312
He's got it.  Same for the other people join too (please, NO pts for me):

left outer join
      people as updater
     updater.[Updater ID] = jobs.updaterid
 left outer join
     people as creator
     creator.[Creator ID] = jobs.creatorid

Author Comment

ID: 40565396
I actually figured out what the problem was. The phone number attribute in the people table is non-persistent. Therefore, SQL Server complained. I drew the relationship to the correct table - the PHONE table - and all was good. My SQL Statement would have worked if I used the correct table names.

Thanks for your assistance.

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CDC and AOG on MS SQL 2012 13 23
SQL Server / Update DB? 22 36
SQL Server 2008 R2, need a pivot/cross tab query... 4 27
query linked sql table field from access 4 20
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

840 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