17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: Adding More to Our Queries<br />

198<br />

point. An example would be where, for each row in a given table, you may have multiple results in the<br />

subquery, but you’re looking for an action more complex than our IN operator provides. Essentially, what<br />

I’m talking about here are situations where you wish you could use a JOIN operator on your subquery.<br />

It’s at times like these that we turn to a somewhat lesser known construct in <strong>SQL</strong> — a derived table. A derived<br />

table is made up of the columns and rows of a result set from a query. (Heck, they have columns, rows, data<br />

types, and so on just like normal tables, so why not use them as such?)<br />

Imagine for a moment that you want to get a list of account numbers and the associated territories for all<br />

accounts that ordered a particular product — say, an HL Mountain Rear Wheel. No problem! Your query<br />

might look something like this:<br />

SELECT sc.AccountNumber, sst.Name<br />

FROM Sales.SalesOrderHeader soh<br />

JOIN Sales.SalesOrderDetail sod<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

JOIN Production.Product pp<br />

ON sod.ProductID = pp.ProductID<br />

JOIN Sales.Customer sc<br />

ON sc.CustomerID = soh.CustomerID<br />

JOIN Sales.SalesTerritory sst<br />

ON sst.TerritoryID = sc.TerritoryID<br />

WHERE pp.Name = ‘HL Mountain Rear Wheel’;<br />

OK, so other than how many tables were required, that was easy. Now I’m going to throw you a twist —<br />

let’s now say I want to know the account number and territory for all accounts that ordered not only an<br />

HL Mountain Rear Wheel, but also an HL Mountain Front Wheel. Notice that I said they have to have<br />

ordered both — now you have a problem. You’re first inclination might be to write something like:<br />

WHERE pp.Name = ‘HL Mountain Rear Wheel’<br />

AND pp.Name = ‘HL Mountain Front Wheel’<br />

But that’s not going to work at all — each row is for a single product, so how can it have both HL Mountain<br />

Rear Wheel and HL Mountain Front Wheel as the name at the same time? Nope — that’s not going<br />

to get it at all (indeed, while it will run, you’ll never get any rows back).<br />

What we really need here is to join the results of a query to find buyers of HL Mountain Rear Wheel<br />

with the results of a query to find buyers of HL Mountain Front Wheel. How do we join results, however?<br />

Well, as you might expect given the title of this section, through the use of derived tables.<br />

To create our derived table, we need two things:<br />

❑ To enclose our query that generates the result set in parentheses<br />

❑ To alias the results of the query, so it can be referenced as a table<br />

So, the syntax looks something like this:<br />

SELECT <br />

FROM () AS <br />

JOIN

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!