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.

Chapter 3: The Foundation Statements of T-<strong>SQL</strong><br />

66<br />

As you can see, this cut down the size of your list substantially and made the contents of the list more<br />

relevant. Another side benefit of this query is that it will actually perform better than the first one. Why?<br />

Well, I go into that later in the book when I discuss performance issues further, but for now, suffice it to<br />

say that not having to return every single row means that <strong>SQL</strong> <strong>Server</strong> doesn’t have to do quite as much<br />

work in order to meet the needs of this query.<br />

As the old commercials on television go, “But wait! There’s more!” We’re not done with DISTINCT yet.<br />

Indeed, the next example is one that you might be able to use as a party trick to impress your programmer<br />

friends. You see, this is one that an amazing number of <strong>SQL</strong> programmers don’t even realize you<br />

can do. DISTINCT can be used as more than just a predicate for a SELECT statement. It can also be used<br />

in the expression for an aggregate. What do I mean? Let’s compare three queries.<br />

First, grab a row count for the SalesOrderDetail table in AdventureWorks:<br />

SELECT COUNT(*)<br />

FROM Sales.SalesOrderDetail;<br />

If you haven’t modified the SalesOrderDetail table, this should yield you around 121,317 rows.<br />

Now run the same query using a specific column to COUNT:<br />

SELECT COUNT(SalesOrderID)<br />

FROM Sales.SalesOrderDetail;<br />

Since the SalesOrderID column is part of the key for this table, it can’t contain any NULLs (more on this<br />

in the chapter in indexing). Therefore, the net count for this query is always going to be the same as the<br />

COUNT(*) — in this case, it’s 121,317.<br />

Key is a term used to describe a column or combination of columns that can be used<br />

to identify a row within a table. There are actually several different kinds of keys<br />

(we’ll see much more on these in Chapters 6, 8, and 9), but when the word “key” is<br />

used by itself, it is usually referring to a table’s primary key. A primary key is a column<br />

(or group of columns) that is (are) effectively the unique name for that row.<br />

When you refer to a row using its primary key, you can be certain that you will get<br />

back only one row, because no two rows are allowed to have the same primary key<br />

within the same table.<br />

Now for the fun part. Modify the query again:<br />

SELECT COUNT(DISTINCT SalesOrderID)<br />

FROM Sales.SalesOrderDetail;<br />

Now we get a substantially different result:<br />

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

31465<br />

(1 row(s) affected)

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

Saved successfully!

Ooh no, something went wrong!