![]() |
|

Identify and resolve SQL Server problems before they happen
|
|
By: Greg Robidoux | Read Comments (8) | Related Tips: More > Query Optimization |
Problem
Sometimes querying data is not that simple and there may be the need to create temporary tables or views to predefine how the data should look prior to its final output. Unfortunately there are problems with both of these approaches if you are trying to query data on the fly.
With the temporary tables approach you need to have multiple steps in your process, first to create the temporary table, then to populate the temporary table, then to select data from the temporary table and lastly cleanup of the temporary table.
With the view approach you need to predefine how this data will look, create the view and then use the view in your query. Granted if this is something that you would be doing over and over again this might make sense to just create a view, but let's look at a totally different approach.
Solution
With SQL Server you have the ability to create derived tables on the fly and then use these derived tables within your query. In concept this is similar to creating a temporary table and then using the temporary table in your query, but the approach is much simpler, because it can all be done in one step.
Let's take a look at an example where we query the Northwind database to try to find out how many customers fall into various categories based on sales. The categories that we have predefined are as follows:
There are several ways that this data can be pulled, but let's look at an approach using a derived table.
The first step is to find out the total sales by each customer, which can be done with the following statement.
SELECT o.CustomerID, |
This is a partial list of the output:
| CustomerID | TotalSales |
| ALFKI | 4596.2000 |
| ANATR | 1402.9500 |
| ANTON | 7515.3500 |
| ... | |
| WOLZA | 3531.9500 |
The next step is to classify the TotalSales value into the OrderGroups that were specified above:
SELECT o.CustomerID, |
This is a partial list of the output:
| CustomerID | TotalSales | OrderGroup |
| ALFKI | 4596.2000 | Micro |
| ANATR | 1402.9500 | Micro |
| ANTON | 7515.3500 | Small |
| ... | ||
| WOLZA | 3531.9500 | Micro |
The next step is to figure out how many customers fit into each of these groups and this is where the derived table comes into play. Take a look at the following query which uses a derived table called OG. What we are doing here is using the same query from the step above, but calling this derived table OG. Then we are selecting data from this derived table for our final output just like we would with any other query. All of the columns that are created in the derived table are now available for our final query.
SELECT OG.OrderGroup, |
This is the complete list of the output from the above query.
| OrderGroup | OrderGroupCount |
| Large | 10 |
| Medium | 11 |
| Micro | 33 |
| Small | 15 |
| Very Large | 20 |
Next Steps
| Tuesday, December 21, 2010 - 12:33:41 PM - Jackie Bird | Read The Tip |
|
Tried your derived table example - but when you have multiple fields to pull you have to include all of them in the group by clause - moreover the AS for the inner group by doesn't work - it throws an error and when i remove it the sql executes |
|
| Tuesday, December 21, 2010 - 1:31:54 PM - Greg Robidoux | Read The Tip |
|
Hi Jackie, what version of SQL Server are you using? Since all of the data is GROUPed you will need to include any column that is not a summary such as AVG, SUM, COUNT, etc... in the GROUP BY clause. Also, I am not sure why the AS for the derived table did not work. This is not really necessary. I tried this both with and without the AS and it worked fine both ways. |
|
| Tuesday, June 19, 2012 - 7:37:13 AM - Sulthanul Arifeen | Read The Tip |
|
Derived tables only helpful for small amount data tranction. For huge data transaction with calculation, temp table is better option. My suggestion is to use temporary table whenever possible only keeping the locking issue under consideration. |
|
| Sunday, March 03, 2013 - 11:52:24 AM - Frank | Read The Tip |
|
Im am trying to create a sql query that will return the following (1) from the table snippet (2) I am using this (3): (1) EmdDescription 10D110 - CHEST PAIN - D1 Severe Respiratory distress 10D210 - CHEST PAIN - D2 Not alert 10D310 - CHEST PAIN - D3 Clammy
(2) EmdDescription 10D110 - CHEST PAIN - D1 Severe Respiratory distress 10D210 - CHEST PAIN - D2 Not alert 10D310 - CHEST PAIN - D3 Clammy 11A111 - CHOKING - A1 Not choking now 11D111 - CHOKING - D1 Not alert 11D211 - CHOKING - D2 Abnormal breathing 11011 - CHOKING - E1 Choking verified or ineffective breathing 12A112 - CONVULSIONS SEIZURES - A1 Not seizing now regularly 12B112 - CONVULSIONS SEIZURES - B1 Breathing regular not verified < 35 yrs
SELECT * FROM EmdCodes WHERE Description ="%CHESTPAIN%";
Any help is appreaciated.
|
|
| Wednesday, March 06, 2013 - 2:50:38 PM - George | Read The Tip |
|
Any thoughts on whether using a Common Table Expression is better than a derived table? |
|
| Monday, April 01, 2013 - 11:01:43 AM - RHG | Read The Tip |
|
Very helpful, thanks a lot |
|
| Monday, April 01, 2013 - 3:35:27 PM - Greg Robidoux | Read The Tip |
|
@Frank - I think you just need a space in your query: SELECT * FROM EmdCodes WHERE Description ="%CHEST PAIN%"; |
|
| Monday, April 01, 2013 - 3:36:42 PM - Greg Robidoux | Read The Tip |
|
@George - not totally sure if a CTE will perform better. The best option is to test both methods and see what is eaiser to setup and also what performs better. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |