SQL - Case
SQL CASE is a very unique conditional statement providing if/then/else logic for any ordinary SQL command, such as SELECT or UPDATE. It then provides when-then-else functionality (WHEN this condition is met THEN do_this).This functionality provides the developer the ability to manipulate the presentation of the data without actually updating or changing the data as it exists inside the SQL table.
SQL Select Case Code:
USE mydatabase; SELECT product, 'Status' = CASE WHEN quantity > 0 THEN 'in stock' ELSE 'out of stock' END FROM dbo.inventory;
SQL Results:
product | Status |
19" LCD Screen | in stock |
HP Printer | in stock |
Pen | in stock |
Stapler | in stock |
Hanging Files | in stock |
Laptop | in stock |
SQL - Case: Real World Example
As a store owner, there might be a time when you would like to offer sale prices for products. This is a perfect opportunity to write a CASE query and alter the inventory sale prices at the presentation level rather than actually changing the price inside of the inventory table. CASE provides a way for the store owner to mask the data but still present it in a useful format.Let's back up a second and pull a listing of our recent orders and join this with the inventory table so that the results contain both the quantity of items purchased and the price from the inventory table. To accomplish this we will need to first write a SQL JOIN query.
SQL Join Query:
USE mydatabase; SELECT dbo.orders.id, dbo.orders.customer, dbo.orders.quantity, dbo.inventory.product, dbo.inventory.price FROM orders JOIN inventory ON orders.product = inventory.product
SQL Results:
id | customer | quantity | product | price |
1 | Tia | 11 | Hanging Files | 14.99 |
2 | Tia | 3 | Stapler | 7.99 |
3 | A+Maintenance | 14 | Hanging Files | 14.99 |
4 | Gerald Garner | 5 | 19" LCD Screen | 179.99 |
5 | Tia | 5 | 19" LCD Screen | 179.99 |
6 | Tia | 4 | HP Printer | 89.99 |
SQL Select Case Code:
USE mydatabase; SELECT dbo.orders.id, dbo.orders.customer, dbo.orders.quantity, dbo.inventory.product, dbo.inventory.price, 'SALE_PRICE' = CASE WHEN price > 0 THEN (price * .75) END FROM orders JOIN inventory ON orders.product = inventory.product
SQL Results:
id | customer | quantity | product | price | SALE_PRICE |
1 | Tia | 11 | Hanging Files | 14.99 | 11.2425 |
2 | Tia | 3 | Stapler | 7.99 | 5.9925 |
3 | A+Maintenance | 14 | Hanging Files | 14.99 | 11.2425 |
4 | Gerald Garner | 5 | 19" LCD Screen | 179.99 | 134.9925 |
5 | Tia | 5 | 19" LCD Screen | 179.99 | 134.9925 |
6 | Tia | 4 | HP Printer | 89.99 | 67.4925 |
Since SQL CASE offers a conditional statement (price > 0), it wouldn't take much more effort to create some conditional statements based on how many products each customer had ordered and offer different discounts based on the volume of a customer order.
For instance, as a web-company, maybe we would like to offer an additional 10% discount to orders totaling more than $100. We could accomplish this in a very similar fashion.
SQL Results:
USE mydatabase; SELECT dbo.orders.id, dbo.orders.customer, dbo.orders.quantity, dbo.inventory.product, dbo.inventory.price, 'SALE_PRICE' = CASE WHEN (orders.quantity * price) > 100 THEN (price * .65) ELSE (price * .75) END FROM orders JOIN inventory ON orders.product = inventory.product
:
id | customer | quantity | product | price | SALE_PRICE |
1 | Tia | 11 | Hanging Files | 14.99 | 9.7435 |
2 | Tia | 3 | Stapler | 7.99 | 5.9925 |
3 | A+Maintenance | 14 | Hanging Files | 14.99 | 9.7435 |
4 | Gerald Garner | 5 | 19" LCD Screen | 116.9935 | 134.9925 |
5 | Tia | 5 | 19" LCD Screen | 179.99 | 116.9935 |
6 | Tia | 4 | HP Printer | 89.99 | 58.4935 |
In each of the examples above, SQL CASE has been utilized to perform presentation level adjustments on data values and its versatility provides limitless results.
0 comments:
Post a Comment