Solved

What's going on with this query?

Posted on 2014-02-05
2
271 Views
Last Modified: 2014-02-05
I'm looking at a query and it utilizes some shorthand that I'm not familiar with and I wanted to run it past the ninjas so I could understand it better.

Here's the select statement:

select z.capxid, z.spmodelid
      , z.assettypeid, at.shortname assettypename, at.refname assettyperefname
      , z.description, z.purchasemonth, z.priorityflag
      , z.equipmenttypeid, et.shortname equipmenttypename, et.refname equipmenttyperefname
      , z.financingflag, z.financingcomment
      , z.financingoptionid, fo.shortname financingoptionname, fo.refname financingoptionrefname
      , z.neworreplace, z.revenueornon, z.itemcost, z.shipping, z.installation, z.taxes
      , z.totalcost, z.explanation, z.codesrequirementflag, z.codescomment
      , z.additionalnewcasesflag, z.additionalnewcases, z.additionalnewamount, z.additionalnewcomment
      , z.projectedlosscasesflag, z.projectedlosscases, z.projectedlossamount, z.projectedlosscomment
      , z.budgetnewcasesflag, z.budgetnewcases, z.budgetnewamount, z.budgetnewcomment
      , z.updateuserid, u.displayname updateusername
      , dbo.ptl_formatdate(z.updatedate,'DATETIME') updatedttm
      from strpl_CapX z
            inner join strpl_Model m on m.spmodelid = z.spmodelid
            inner join fin_AssetType at on at.assettypeid = z.assettypeid
            left outer join mat_EquipmentType et on et.equipmenttypeid = z.equipmenttypeid
            left outer join fin_FinancingOption fo on fo.financingoptionid = z.financingoptionid
            left outer join ptl_user u on z.updateuserid = u.userid
      where z.spmodelid = @spmodelid
      order by at.sortorder, at.shortname, z.purchasemonth, z.description

I'm hip to the strpl_CapX table, but I've never used the "z" dynamic that is here and I wanted to make sure that what appears to be the case, is indeed what's happening.

It looks as though that rather than have to write out "strpl_CapX.capxid," I can instead writer "z.capxid" provided I put a "z" after the name of the table like what you see at the beginning of the FROM clause: from strpl_CapX z. Is that right? The above query works so I'm not questioning its utility, I'm just trying to figure out the scripting conventions. The joins are a subject all by themselves, but I wanted to first understand the apparent one letter abbreviations for the tables themselves and ask if that is, in fact, what they are.
0
Comment
Question by:brucegust
2 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 250 total points
ID: 39836943
Yes, that's exactly the case. Not only does it save you time in typing, but there are other advantages.

For example, if you ever need to self-join a table, you give each a different alias in the FROM clause, for example z and x. Then in your SELECT, using either x or z will select from the correct instance of the table.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 39836944
Yes z is an alias for the strl_CapX table as defined in the from clause

http://sqlmag.com/t-sql/aliases-t-sql
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now