SQL Server


Teradata offers the ability to build a query, and then use a QUALIFY statement that filters the resulting resultset. This is especially handy when using a ROW_NUMBER function where you are partitioning on a natural key (or whatever it may be) and you want to return only the rows where the ROW_NUMBER value is 1, for example.

In SQL Server today, this has to be done with derived tables/CTEs like so:

USE AdventureWorksDW
GO
/* Ignoring the fact that this table
   has a "Status" and an EndDate column */
;WITH CTE AS (
SELECT EmployeeKey,
       FirstName,
       LastName,
       MiddleName,
       Title,
       ROW_NUMBER() OVER (PARTITION BY EmployeeNationalIDAlternateKey
                              ORDER BY StartDate DESC) AS ROW_NUM
  FROM dbo.DimEmployee
)
SELECT EmployeeKey,
       FirstName,
       LastName,
       MiddleName,
       Title
  FROM CTE
 WHERE row_num = 1
 ORDER BY FirstName, LastName
GO

That’s easy enough to understand, of course, but the problem lies in the fact that you have to write a bunch of extra lines of code to accomplish this task.

Teradata has the QUALIFY option which would be represented like this:

 SELECT EmployeeKey,
        FirstName,
        LastName,
        MiddleName,
        Title
   FROM dbo.DimEmployee
QUALIFY ROW_NUMBER() OVER (PARTITION BY EmployeeNationalIDAlternateKey
                               ORDER BY StartDate DESC) = 1

Note how much cleaner that looks. QUALIFY has many advantages, but I’ll leave that as an exercise for you to review. In the mean time, if you would like to see such an option in SQL Server, please vote on Itzik Ben-Gan’s feature request: https://connect.microsoft.com/SQLServer/feedback/details/532474/qualify-request-for-a-new-filtering-clause

If you have something that you’d like to see blogged here regarding SSIS and/or SQL Server, please let me know and I’ll see what I can do.

Also, if you find a post here that you’re using to work on an issue in SSIS 2008, let me know if it isn’t working for you. It could be that I don’t have updated content for use in the SSIS 2008 environment. It’s easy enough to update, I just won’t be able to catch all of the items listed here.

One last thing - the annual MVP Summit is coming up in February. If you have something that you’d like for me to bring to the SSIS dev team in terms of feedback, feature requests, bugs, etc…, let me know and I’ll do my best to present them to the team.

A collection of 53 Microsoft MVPs (myself included) have wrapped up authoring one of the greatest SQL Server books yet to hit the shelves - SQL Server MVP Deep Dives.

This book brings together the experience from all corners of SQL Server: Design & Architecture, Development, Administration, Performance Tuning & Optimization, and Business Intelligence. Each section contains unique topics ranging from avoiding common query mistakes, running SQL Server on Hyper-V, to SSIS performance tuning.

The book is available to pre-order here: http://www.sqlservermvpdeepdives.com

(EDIT: Save 50% by purchasing the book from the above site by the end of September when you use the code, pop0928)

All author royalties - and this is what really makes this book stand out - are being donated to War Child International, a charity involved in bringing food, health, and happiness to kids across the world involved in war.

Many thanks to the editors for pushing this book through to completion: Paul Nielsen, Kalen Delaney, Greg Low, Adam Machanic, Paul S. Randal, and Kimberly L. Tripp

For more information on the book, continue reading:
(more…)

There seems to be some misinformation going around about how SQL Server works with ordered data, especially with how the data is *physically* stored, even with a clustered-indexed table.

Please see this post for more details on SQL Server ordering: http://blogs.msdn.com/sqltips/archive/2005/07/20/441053.aspx

To add to that list a bit:

  • Updates are (almost) the same as select queries, internally, in that updates are turned into selects internally, which means the ordering rules of selects are also at play
  • Updates can have parallelism, meaning that updates do not have to preserve update order
  • Update order can’t be guaranteed because clustered indexes can be rebuilt with a different order
  • Ordering generally happens at the operator level within a query plan; not at the top
  • Unless you’ve seen it in BOL or in a MS Blog post, don’t assume anything with respect to SQL Server ordering - even if your queries operate on a given order today!

The PASS team has assembled a unique opportunity for those wishing to expand their SQL Server knowledge, be it in database administration, business intelligence (including, of course, SSIS), blogging, etc…, can attend one or more sessions (out of a total of 24) for free by registering here: http://24hours.sqlpass.org/

No matter where in the world you are, there should be a session available that will appeal to your knowledge craving noggin, so don’t hesitate, sign up today.

The session is officially on Sept 2nd, 2009, but depending on your time zone, it may be Sept 1st or 3rd, so please be sure to check out their time zone information.

It would seem that the DATAllegro acquisition is complete as Microsoft has shipped its first technology preview of the project code-named “Madison” to ten customers for their feedback.

Madison looks to be quite the offering, allowing for rapid growth of a data warehouse architecture because of its “appliance” model as well as integration with existing SQL 2008 instances, minimizing time-to-market. Customers have reported loading over a terabyte of data within an hour using Madison and over 1.5 terabytes of query processing per minute.

When you have a chance, stroll on over to the Madison Web page to read more about it: http://www.microsoft.com/sqlserver/2008/en/us/madison.aspx

Starting with SQL Server 2005 RTM and through the latest SQL Server 2008 build at the time of this writing, there is a pretty nasty bug with respect to the COUNT(expression) function and its rule that when using an expression, it filters out NULLs in its count results.
(more…)

Simon Sabin, fellow SQL MVP, has posted on the topic of formatting SQL.

For me, I align my queries vertically like so:


select track,
       level,
       title,
       Name,
       ss.length 

  from ConferenceSession cs 

  join session ss
    on ss.sessionId = cs.SessionId 

  join Speaker sp
    on sp.SpeakerId = ss.ownerId 

 where cs.Approved = 1
   and cs.ConferenceId = 4 

 order by length,
          title,
          cs.SessionId desc

If I have multiple join predicates, I continue the indention pattern:


select track,
       level,
       title,
       Name,
       ss.length

  from ConferenceSession cs 

  join session ss
    on ss.sessionId = cs.SessionId
   and (ss.approved  = 1
        or ss.track  = 'DBA')  

  join Speaker sp
    on sp.SpeakerId = ss.ownerId 

 where cs.Approved = 1
   and cs.ConferenceId = 4

order by length,
         title,
         cs.SessionId desc

What is your SQL formatting style?

SQL Server 2008 brings with it the ability to change the color of the status bar in Management Studio per your choosing. This is especially good to distinguish between production and non-production machines, or if you simply wish to have a different color other than the default gray.

However, the only way to do this is in the properties of a server when registering it under Registered Servers (Ctrl-Alt-G). Add a new server and in the connection properties, select the color you wish for this particular server.

I have a short video demonstrating it:

SQL Server Management Studio color example

For those of you that have access to the SQL Server 2008 distribution via the various channels, please pay attention to the Microsoft KB article which will likely apply to your installation experience until Visual Studio 2008 (Orcas) SP1 has shipped: http://support.microsoft.com/kb/956139

Basically, if you want to install SQL Server 2008, go ahead. If you want to install SSIS, SSRS, SSAS, and a few other tools, you’re going to have to wait until VS 2008 SP1 has shipped and is installed on your machine before going through the SQL Server 2008 setup. Also note, that the warning in the KB article appears quite late in the SQL Server 2008 setup process, so you have been warned!

Next Page »