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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We’ll go back to the AdventureWorks<strong>2008</strong> database and look again at the query where we wanted to<br />

know the orders that happened on the first date that an order was placed in the system. However, this<br />

time we want to add a new twist: We want to know the SalesOrderID(s) and OrderDate of the first<br />

order in the system for each customer. That is, we want to know the first day that a customer placed an<br />

order and the IDs of those orders. Let’s look at it piece by piece.<br />

First, we want the OrderDate, SalesOrderID, and CustomerID for each of our results. All of that information<br />

can be found in the SalesOrderHeader table, so we know that our query is going to be based, at<br />

least in part, on that table.<br />

Next, we need to know what the first date in the system was for each customer. That’s where the tricky<br />

part comes in. When we did this with a nested subquery, we were only looking for the first date in the<br />

entire file — now we need a value that’s by individual customer.<br />

This wouldn’t be that big a deal if we were to do it in two separate queries — we could just create a temporary<br />

table and then join back to it.<br />

A temporary table is pretty much just what it sounds like — a table that is created for temporary use<br />

and will go away after our processing is complete. Exactly how long it will stay around is variable and<br />

is outside the scope of this chapter. We will, however, visit temporary tables a bit more as we continue<br />

through the book.<br />

The temporary table solution might look something like this:<br />

USE AdventureWorks<strong>2008</strong>;<br />

-- Get a list of customers and the date of their first order<br />

SELECT soh.CustomerID, MIN(soh.OrderDate) AS OrderDate<br />

INTO #MinOrderDates<br />

FROM Sales.SalesOrderHeader soh<br />

GROUP BY soh.CustomerID;<br />

-- Do something additional with that information<br />

SELECT soh.CustomerID, soh.SalesOrderID, soh.OrderDate<br />

FROM Sales.SalesOrderHeader soh<br />

JOIN #MinOrderDates t<br />

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

AND soh.OrderDate = t.OrderDate<br />

ORDER BY soh.CustomerID;<br />

DROP TABLE #MinOrderDates;<br />

We get back a little over 19,000 rows:<br />

(19119 row(s) affected)<br />

CustomerID SalesOrderID OrderDate<br />

----------- ------------ -----------------------<br />

11000 43793 2001-07-22 00:00:00.000<br />

11001 43767 2001-07-18 00:00:00.000<br />

11002 43736 2001-07-10 00:00:00.000<br />

…<br />

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

193

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

Saved successfully!

Ooh no, something went wrong!