Top Banner
Glenn Haertlein SQL Server Portfolio Phone: 864-385-8606 Email: [email protected] http://www.linkedin.com/in/htack210 1
41
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: G Hslideshare2

Glenn HaertleinSQL Server Portfolio

Phone: 864-385-8606Email: [email protected]://www.linkedin.com/in/htack210

1

Page 2: G Hslideshare2

Introduction 3

What is Set Focus? 4

JungleBooks 5

Database Diagram 6

Single Table Query 7

Library 8Query with Concatenation 9

Advanced Query with Joins 10

Query with Outer Join 11

Join/Union Query 12

Alternate to Previous Query 14

Query Using Temporary Table 16

Query Using Aggregate Function SUM 17

Query with Calculated Column 18

Create View 19Stored Procedure 20

Table of Contents

2

Page 3: G Hslideshare2

Piggy Bank 22

Database Diagram 23

Stored Proc Example (Make a deposit) 24

DDL Example 25

About Me 21

ToC (cont’d)

3

Page 4: G Hslideshare2

Introduction•This portfolio contains examples of my development skills in MS SQL Server. It is a work in progress and reflects my current work thus far in the SetFocus Master’s Program.•SetFocus utilizes Microsoft Official Curriculum in conjunction with its own materials to produce some of the following coursework:

• RDBMS and XML•Querying using Transact SQL•Implementing and maintaining a MS SQL Server 2005 Database•Designing an MS SQL Server 2005 Infrastructure•Designing security for MS SQL Server 2005•Designing High Availability Database Solutions using MS SQL Server 2005•Troubleshooting and Optimizing Database Servers using MS SQL Server 2005•SQL Server Integration Services•SQL Server Reporting Services

4

•In addition to the coursework and programming labs, the program includes challenging real-world projects where I apply the skills learned in class.

Return to TOC

Page 5: G Hslideshare2

5

What is SetFocus?

• The SetFocus SQL Master’s Program is an intensive, hands-on, project-oriented program allowing knowledge and valuable experience putting the SQL skill set to use in a simulated work environment.

• I am currently enrolled and will receive over 300 hours of in-depth, hands-on experience focused on SQL.

• SetFocus projects are real world projects that are distributed just as I would receive in a position. I receive project specifications and am expected to identify best courses of action with deadlines set for completion.

Return to TOC

Page 6: G Hslideshare2

• Junglebooks is a book company which has a database consisting of books, authors, orders and customers.

• For this project I created a database and submitted a database diagram.

• In this project scenario, I am working with a fictitious application programmer who is building a client application using .NET.

• To meet the needs of the application programmer, I created queries for different forms of the application.

6Return to TOC

Junglebooks Scenario

Page 7: G Hslideshare2

7Return to TOC

Database Diagram

Page 8: G Hslideshare2

8

Single Table Query

The Cheap Books form displays available books below a certain price. The user enters 15 in the txtUnitPrice form field. Return ISBN, title and publisher in order by title.

USE JungleBooks; DECLARE @txtUnitPrice as int = 15; SELECT ISBN, Title, PublisherFROM JungleBooks.dbo.Books as BooksWHERE Books.UnitPrice <= @txtUnitPriceORDER BY Books.Title;

Return to TOC

Page 9: G Hslideshare2

9

The Library Database is created to support the principal functions of a lending library’s day-to-day operations.

For this database project, I created queries against the Library database that returned a number of results using:• string concatenations• different types of joins• UNION statements• CASE statements• date manipulation• and aggregate functions.

Return to TOC

Library Database Scenario

Page 10: G Hslideshare2

10

Write and execute a query on the Member and Adult tables in the Library database that returns the firstname, middleinitial, lastname, street, city, state and zip. Concatenate the firstname, middleinitial and lastname columns into one string and alias as Name. Make sure that the spacing is appropriate when there is no middle initial due to a NULL or empty string value. Display records in order by lastname and firstname.

USE library; SELECT M.firstname + ' ' +

CASE WHEN M.middleinitial IS NULL THEN ' ' ELSE M.middleinitial + ' ' END + M.lastname As Name, A.street as [Street], A.city as [City], A.state as [State], A.zip as [Zip]

FROM member as MJOIN adult as A ON M.member_no = A.member_no

ORDER BY M.lastname, M.firstname;

Return to TOC

Query with String Concatenation

Page 11: G Hslideshare2

11

Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no, on_loan, title, translation, cover for rows in the copy table with an ISBN of 500 or 1000. Only available books should be displayed and hardback copies should be listed first.

USE library;SELECT I.isbn

, C.copy_no, C.on_loan, T.title, I.translation, I.cover

FROM item as IJOIN copy as C ON I.isbn = C.isbnJOIN title as T ON C.title_no = T.title_no

WHERE (C.on_loan = 'N' AND C.isbn = 500) or (C.on_loan = 'N' AND C.isbn =

1000)ORDER BY I.cover;

Return to TOC

Advanced Query Using Joins

Page 12: G Hslideshare2

12

Write and execute a query to retrieve the member’s full name and member_no from the Member table and the ISBN and log_date values from the Reservation table for member numbers 250, 341, and 1675. Order the results by member_no and log_date. You should show information for these members, even if they have no books on reserve.

USE library;SELECT M.member_no

, M.firstname + ' ' + CASE

WHEN M.middleinitial IS NULL THEN ' '

ELSE M.middleinitial + ' ' END+ M.lastname as Name, R.isbn, CAST(R.log_date as DATE) as log_date

FROM member as MLEFT OUTER JOIN reservation as R ON M.member_no = R.member_no

WHERE m.member_no IN (250, 341, 1675)ORDER BY m.member_no, r.log_date;

Return to TOC

Query with Outer Join

Page 13: G Hslideshare2

13

Write and execute a query to retrieve the member’s full name and member_no from the Member table and the ISBN and log_date values from the Reservation table for member numbers 250, 341, and 1675. Order the results by member_no and log_date. You should show information for these members, even if they have no books on reserve.

USE library;SELECT I.isbn

,T.title,A.member_no,M.lastname + ', ' +M.firstname + ' ' +CASE

WHEN M.middleinitial IS NULL THEN ' 'ELSE M.middleinitial + ' '

END as FullName,'Adult' as [Status]

FROM adult as AJOIN member as M ON A.member_no = M.member_noJOIN reservation as R ON R.member_no = A.member_noJOIN item as I ON I.isbn = R.isbnJOIN title as T ON T.title_no = I.title_no

WHER E I.isbn = '288'UNION ALL SELECT I.isbn

,T.title ,J.member_no,M.lastname + ', ' +M.firstname + ' ' +CASE

WHEN M.middleinitial IS NULL THEN ' 'ELSE M.middleinitial + ' '

END as FullName,'Juvenile' as [Status]

FROM Juvenile as JJOIN member as M ON J.member_no = M.member_no

JOIN reservation as R ON R.member_no = J.member_noJOIN item as I ON I.isbn = R.isbnJOIN title as T ON T.title_no = I.title_no

WHERE I.isbn = '288'Return to TOC

Join/Union Query

Page 14: G Hslideshare2

14Return to TOC

Query Proof

Page 15: G Hslideshare2

15

Write the above statement again using a CASE statement. You cannot JOIN to the Adult or Juvenile tables. Compare the two versions and determine which one is more efficient. Cut and paste the proof of your research. (No need to copy the output of records)

USE library;SELECT DISTINCT I.isbn

,T.title,M.member_no,M.lastname + ', ' +M.firstname + ' ' +CASE

WHEN M.middleinitial IS NULL THEN ' 'ELSE M.middleinitial + ' '

END as FullName,[Status] = CASE WHEN EXISTS (SELECT * FROM dbo.adult

WHERE dbo.adult.member_no=m.member_no) THEN 'Adult' ELSE 'Juvenile'

ENDFROM

member as M JOIN reservation as R ON R.member_no = M.member_noJOIN item as I ON I.isbn = R.isbnJOIN title as T ON T.title_no = I.title_no

WHERE R.isbn = '288' ORDER BY FullName

Return to TOC

Alternate to Previous Query

Page 16: G Hslideshare2

Query Proof

16Return to TOC

Page 17: G Hslideshare2

17

Write and execute a query that returns the member_no, full name, out_date, due_date, and title columns from the Loan, Member, and Title tables. Convert the datetime values from the out_date and due_date columns to char(12), format 101. Restrict the results to books which were due prior to the current date. Load the records into a temporary table called #overdue. Leave the query window open after you create the temporary table.

USE library;IF OBJECT_ID('tempdb.dbo.#overdue') IS NOT NULL

DROP TABLE dbo.#overdue;GOSELECT M.member_no

, M.firstname + ' ' +CASE

WHEN M.middleinitial IS NULL THEN ' 'ELSE M.middleinitial + ' '

END + ' ' + M.lastname as FullName, CONVERT(char(12), L.out_date, 101) as [Out Date], CONVERT(char(12), L.due_date, 101) as [Due Date], T.title

INTO dbo.#overdueFROM loan as L

JOIN member as M ON L.member_no = M.member_noJOIN title as T ON T.title_no = L.title_no

WHERE due_date < CURRENT_TIMESTAMPORDER BY FullName;Return to TOC

Query Using Temporary Table to Store Records

Page 18: G Hslideshare2

Query Using Aggregate Function SUM

18

Write and execute a query that returns member_no, firstname, lastname and sum of fine_paid for members who have paid the highest fines to date. Members should only appear once in the list. Display the highest fine first. If more than one member has paid the same amount display the records in order by member_no.

USE library;SELECT M.member_no

, M.firstname, M.Lastname, CAST(SUM(LH.fine_paid) as

Numeric(5,2)) as FinePaidFROM member as M

JOIN loanhist as LH ON M.member_no = LH.member_no

GROUP BY M.member_no, M.firstname, M.lastnameHAVING SUM(LH.fine_paid) IS NOT NULLORDER BY

FinePaid DESC,M.member_no;

Return to TOC

Page 19: G Hslideshare2

19

Write and execute a query on the Reservation table that returns the ISBN, title and Total. The Total column is a calculated column which represents the number of members wishing to reserve a particular book.

USE library;SELECT R.isbn

,(SELECT T.titleFROM title as TWHERE I.title_no =

T.title_no) as Title, COUNT(R.member_no) as

TotalFROM reservation as R

JOIN item as I ON I.isbn = R.isbn

GROUP BY R.isbn, I.title_noORDER BY Title

Return to TOC

Query with Calculated Column

Page 20: G Hslideshare2

20

Create a view in the TSQLFundamentals2008 database that returns the orderid, day of the week (Spelled Out), the name of the month (spelled Out), the day of the month, and the year based on the order date in the sales.orders table.

Return to TOC

Create View

USE TSQLFundamentals2008; IF OBJECT_ID('Sales.View1') IS NOT NULL DROP VIEW Sales.View1;GOCreate View Sales.View1AsSELECT orderid

, DATENAME(WEEKDAY, orderdate) as "DAY", DATENAME(MONTH, orderdate) as "MONTH", DATENAME(DAY, orderdate) as "Date" , DATENAME(YEAR, orderdate) as "Year"

 FROM Sales.Orders

Page 21: G Hslideshare2

21

Create a stored procedure in the TSQLFundamentals2008 database that returns the order ID,the order date, the ship country. The employee full name, and the company name. The ship country should be a parameter and the result set should be sorted by order date from most recent to oldest.

Return to TOC

Stored Procedure

USE TSQLFundamentals2008; if OBJECT_ID('StoredProc1', 'P') IS NOT NULL

DROP PROC StoredProc1;

Go CREATE PROC StoredProc1

@ShipCntry as varchar(20) = 'USA'AS SELECT S.orderid, S.orderdate, S.shipcountry FROM Sales.Orders AS S

JOIN HR.Employees as E on S.empid = E.empidJOIN Sales.Shippers as Ship ON Ship.shipperid = S.shipperid

WHERE S.shipcountry = @ShipCntryOrder BY S.orderdate

Page 22: G Hslideshare2

Return to TOC 22

Piggy Bank Project• The PiggyBank Database simulates bank operations such as

Overdraft Accounts, Customer and Accounts relationships, and Transactions.

• This database has been used for a couple of projects:– Create an Entity Relationship Diagram given some

specifications such as Overdraft Fees, Error Information when a transaction fails, Login Failures, and Customer/Account relationships.

– Design back-end stored procedures, DDL/DML triggers, parameterized stored procedures that select from views. Some of the actions created are Create/Update Customer, Create Checking/Savings Accounts, Deposit/Withdrawal Procedures, Simulate ATM Balances, Customer Account History (bank statements) and Use of Overdraft Accounts.

Page 23: G Hslideshare2

Return to TOC 23

Piggy Bank Database Diagram

Page 24: G Hslideshare2

Return to TOC 24

Piggy Bank Stored Proc Example

Page 25: G Hslideshare2

Return to TOC 25

Piggy Bank DDL Example

Page 26: G Hslideshare2

SSIS / SSRS Report Project 26

Mini AdventureWorks Project • The company Mini-AD (short for Mini-AdventureWorks) is interested in taking

historical spreadsheet (CSV) data for their list of products, vendors, and purchase order history, and loading the data into a SQL Server database.

• Mini-AD wants the load process to work on a go-forward basis, so that new/modified products/vendors/orders can be loaded in SQL Server as well

• Mini-AD’s load process for orders should validate that any incoming orders with product numbers or vendor numbers that do not match an existing product/vendor number should NOT be written to the SQL Server database. Instead, this data should be written to an exception file and emailed.

• Mini-AD also wishes to build two reports : one for top vendor and product sales, and the second for sales by vendor and ship method across years.

Page 27: G Hslideshare2

Return to TOC 27

MiniAD Database Diagram

Page 28: G Hslideshare2

Return to TOC 28

Import Orders

Page 29: G Hslideshare2

Return to TOC 29

Import Orders for Each Loop

Page 30: G Hslideshare2

Return to TOC 30

Import Products Data Flow

Page 31: G Hslideshare2

Return to TOC 31

Import Vendors

Page 32: G Hslideshare2

Return to TOC 32

Update Product Prices

Page 33: G Hslideshare2

Return to TOC 33

Top Vendors Report

Page 34: G Hslideshare2

Return to TOC 34

Top Vendors Design

Page 35: G Hslideshare2

Return to TOC 35

Vendor Sales by Year

Page 36: G Hslideshare2

Return to TOC 36

Vendor Sales Design

Page 37: G Hslideshare2

Return to TOC 37

BlockFlix Project

• As part of a team I helped develop a database complete with SSIS and SSRS services for a fictional movie rental company called “BlockFlix”

• The general requirements included the creation of a central, online database that handled movie rentals through online streaming video, kiosks, and brick-and-mortar stores.

• My primary responsibility involved using SSIS to import movie information into the DB from an XML file.

Page 38: G Hslideshare2

Return to TOC 38

BlockFlix Database Diagram

Page 39: G Hslideshare2

Return to TOC 39

Add Movies from XML

Phase Three

Run clean up to clear staging tables and save space.

Phase Two: Execute SQL TaskOnce the XML data has been refined, the SQL task inserts the info into the

MovieCast table where Movies are associated with their respective cast members

Phase One: Data Flow task

Inserts Movie and Cast info directly into their respective tables

Also puts info into staging table where Unique Keys weed out

duplications

Page 40: G Hslideshare2

Return to TOC 40

Inside the Dataflow Task

Send to XML

Staging for

further

processing

Capture errors

Get XML file

Convert XML to correct data types

Send directly to DB table

Page 41: G Hslideshare2

Return to TOC 41

Handling the XML