Previous | Table of Contents | Next |
CASE with a VIEW
A view combined with a CASE expression is about as cool a SQL construct as you can create. This example displays different information from the sales table depending on the day of the week it is requested. On Tuesday through Friday, show the highest sale and sum of sales for the previous day for this salesmans ID. On Saturday through Monday, show the highest sale and sum of sales for the previous week.
Here is the code to create the view:
create view sales_summary as select sum( case when datepart(dw, getdate()) in (3, 4, 5, 6) -- This code runs on T-F and datediff(day, sal_date, getdate()) = 1 then amt when datepart(dw, getdate()) in (1, 2, 7) and datediff(day, sal_date, getdate()) <= 7 and datediff(day, sal_date, getdate()) > 0 then amt else null end) TotalSales, max( case when datepart(dw, getdate()) in (3, 4, 5, 6) -- This code runs on T-F and datediff(day, sal_date, getdate()) = 1 then amt when datepart(dw, getdate()) in (1, 2, 7) and datediff(day, sal_date, getdate()) <= 7 and datediff(day, sal_date, getdate()) > 0 then amt else null end) BestSale from sales where uid = user_id()
Now, let me explain what is happening in there.
Only two columns are created in this view: a column for TotalSales, which calculates a sum(), and a column for BestSale, which calculates the max() sale.
For each row in the sales table that matches the salesmans user ID, this view runs the two CASE statements. For the sum, a rows amt is included in the sum if one of two things are true:
The same calculation is made for the max() aggregate.
In the event that something terrible happened in our calculation, an ELSE clause is provided that gives a null value for the row.
When all the rows have been processed, the server has formed two lists of numbers to use. The first column sums the numbers; the second column finds the maximum value.
Today you learned about temporary tables. There are three types of temporary tables: local, global, and permanent. You learned how to create and use temporary tables. You learned the SELECT INTO syntax and how to use it to create copies of existing tables.
You also learned how to use views, which provide a means for filtering data in base tables. If you read through the examples provided at the end of today, you learned some advanced techniques for applying views.
Q If I delete a row from a view, does that change data in the table on which the view is defined?
A Yes. A view does not store data itself; it is simply a lens through which you can view, in a different way, data that lives in normal tables.
Q I tried to use SELECT INTO to create a table in a database at work, but I got a permissions error. How come?
A To run SELECT INTO, the database must have the proper options set. Also, you must have permission to read the source table and to create a table in the destination database.
Q Why would I want to use a temporary table to hold data if I can just go after the base table?
A There are two good reasons. First, if lots of users are altering data in the base table and you are interested in a snapshot of the data, you can get that with a temp table. If the base table is very busy, your table, usable only by you, will never require you to wait while another user has pages locked. Second, tempdb is often placed on a fast device, sometimes a solid state disk (an expensive bank of memory that acts just like a disk drive), and sometimes placed in the servers RAM.
The Workshop provides quiz questions to help you solidify your understanding of the material covered, as well as exercises to provide you with experience in using what youve learned. Try to understand the quiz and exercise answers before continuing on to the next days lesson. Answers are provided in the appendix.
Previous | Table of Contents | Next |