Sqldev320a Week4-1
Sqldev320a Week4-1
TODAY •
•
RECAP WEEK 3
QUIZ 3 REVIEW
• ASSIGNMENT 3 REVIEW
• DREA’S QUESTION ABOUT MARGIN
VARIABLES
TEMPORARY TABLES
Tackling Q05
Get the average total value of an order by year, month
include the average number of lines and the average number of items per order
2012 2013
Tackling Q09
Get the percent of Total Sales change (quarter over quarter) for 2011, 2012, 2013,
2014
3. Understand “(Current Quarter – 1) = Previous Quarter”
j oi n
-
• …
• 2012, Q1 = 5
e l f
S
• …
Tackling Q09
Get the percent of Total Sales change (quarter over quarter) for 2011, 2012, 2013,
2014
1. Visualize the results
Current Quarter Year Current Quarter Current Quarter Sales Previous Quarter Year Previous Quarter Previous Quarter Sales Performance Pct
2011 2 $ NULL NULL NULL NULL
962,716.74
2011 3 $ 5,042,490.58 2011 2 $ 523.77%
962,716.74
2011 4 $ 6,636,464.89 2011 3 $ 5,042,490.58 131.61%
2012 1 $ 8,421,802.43 2011 4 $ 6,636,464.89 126.90%
2012 2 $ 8,808,557.97 2012 1 $ 8,421,802.43 104.59%
Defining Setting
5
SQL Script
Programming
<block-statement> := BEGIN
<single-statement> [<single-statement> …]
END
Control of Flow
IF…ELSE… RETURN
BEGIN … END THROW
BREAK TRY … CATCH …
CONTINUE WAITFOR
GOTO label
WHILE
IF [ELSE] statement
Selecting which set of
Data needs to be
statements to
evaluated as a
execute based on
Boolean expression
data
IF is part of the script language, but not part of the SQL CASE is part of a SQL statement.
language.
-- Translate colors in English to Spanish
-- Make sure table doesn't exist before creating it SELECT DISTINCT
IF OBJECT_ID('dbo.IfTest', 'U') IS NOT NULL [Color] [English],
BEGIN CASE
DROP TABLE [dbo].[IfTest] WHEN Color IS NULL THEN NULL
PRINT 'TABLE DROPPED' ELSE
END CASE Color
WHEN THEN
CREATE TABLE [dbo].[IfTest] 'Black' 'Negro'
( Col1 INT NOT NULL WHEN 'Blue' THEN 'Azul'
PRIMARY KEY WHEN THEN
) 'Grey'
WHEN 'Silver''Gris'
THEN 'Plata'
WHEN
WHEN'Red'
'White'THEN 'Rojo'
SELECT [SalesOrderID]
, SUM(IIF([LineTotal] - [OrderQty] * [StandardCost] <
0, [LineTotal] - [OrderQty] * [StandardCost], NULL )) AS
[Total Negative Margin]
, SUM([LineTotal] - [OrderQty] * [StandardCost]) AS [Total
Order Margin]
FROM [Sales].[SalesOrderDetail] AS D join …
CHOOSE operator
Returns the item at the specified index from a
list of values in SQL Server
SELECT JobTitle,
HireDate,
Choose(Month(HireDate), 'Winter', 'Winter', 'Spring',
'Spring', 'Spring', 'Summer', 'Summer', 'Summer', 'Autumn',
'Autumn', 'Autumn', 'Winter') AS Quarter_Hired
FROM HumanResources.Employee
WHERE Year(HireDate) > 2005
ORDER BY Year(HireDate);
WHILE statement
Repeats a set of statements zero or more time based on data
While Boolean expression evaluates as TRUE, the following statement or ‘block’ is
executed.
If the expression evaluates as ‘FALSE’ the execution continues on the statement after the
WHILE block
13
WHILE statement
WHILE <boolean-expression> (<single-statement>|<block-
statement>)
<block-statement> := BEGIN
<single-statement>
[<single-statement>
…]
END
13
IF etc…
DEMO
SQL Script Programming
Table
variables
N
1
ProductAssemblyID ComponentID
SQL Script
517 497
517 528
517 530
517 911
738 324
738 325
Programming
738 326
738 327
738 399
738 492
ProductAssemblyID ComponentID 738 532
738 533
770 517 738 534
738 802
770 738 806 1
806 4
770 806 806 323
806 402
770 811 806 459
806 462
770 818 811 329
811 356
770 826 811 398
811 529
770 894 818 400
818 490
770 907 818 506
818 510
770 938 818 527
818 922
770 945 818 931
826 400
770 948 826 490
826 506
770 950 826 510
826 527
770 952 826 922
826 931
770 994 894
894
355
535
894 679
945 351
945 352
950 318
950 320
950 321
950 322
950 332
994 3
994 525
Week 4 Assignment