G Hslideshare2


Published on


Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

G Hslideshare2

  1. 1. Glenn Haertlein SQL Server PortfolioPhone: 864-385-8606Email: glenn.haertlein@setfocus.comhttp://www.linkedin.com/in/htack210 1
  2. 2. Table of Contents Introduction 3 What is Set Focus? 4 JungleBooks 5 Database Diagram 6 Single Table Query 7 Library 8 Query 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 19 Stored Procedure 20 2
  3. 3. ToC (cont’d)Piggy Bank 22 Database Diagram 23 Stored Proc Example (Make a deposit) 24 DDL Example 25About Me 21 3
  4. 4. Introduction•This portfolio contains examples of my development skills in MS SQLServer. It is a work in progress and reflects my current work thus far in theSetFocus Master’s Program.•SetFocus utilizes Microsoft Official Curriculum in conjunction with its ownmaterials 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•In addition to the coursework and programming labs, the program includeschallenging real-world projects where I apply the skills learned in class. Return to TOC 4
  5. 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 5
  6. 6. Junglebooks Scenario• 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. Return to TOC 6
  7. 7. Database Diagram Return to TOC 7
  8. 8. Single Table QueryThe Cheap Books form displays available books below acertain price. The user enters 15 in the txtUnitPrice formfield. Return ISBN, title and publisher in order by title. USE JungleBooks; DECLARE @txtUnitPrice as int = 15; SELECT ISBN, Title, Publisher FROM JungleBooks.dbo.Books as Books WHERE Books.UnitPrice <= @txtUnitPrice ORDER BY Books.Title; Return to TOC 8
  9. 9. Library Database ScenarioThe Library Database is created to support the principal functions of a lendinglibrary’s day-to-day operations.For this database project, I created queries against the Library database thatreturned a number of results using:• string concatenations• different types of joins• UNION statements• CASE statements• date manipulation• and aggregate functions. Return to TOC 9
  10. 10. Query with String ConcatenationWrite and execute a query on the Member and Adult tables in the Library database thatreturns the firstname, middleinitial, lastname, street, city, state and zip. Concatenate thefirstname, middleinitial and lastname columns into one string and alias as Name. Make surethat the spacing is appropriate when there is no middle initial due to a NULL or empty stringvalue. 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 M JOIN adult as A ON M.member_no = A.member_no ORDER BY M.lastname, M.firstname; Return to TOC 10
  11. 11. Advanced Query Using JoinsWrite 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 I JOIN copy as C ON I.isbn = C.isbn JOIN 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 11
  12. 12. Query with Outer JoinWrite and execute a query to retrieve the member’s full name and member_no from the Member tableand 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, evenif 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 M LEFT 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 12
  13. 13. USE library; SELECT I.isbnJoin/Union Query ,T.title ,A.member_no ,M.lastname + , + M.firstname + + Write and execute a query to CASE WHEN M.middleinitial IS NULL THEN retrieve the member’s full ELSE M.middleinitial + END as FullName, name and member_no from Adult as [Status] the Member table and the FROM adult as A JOIN member as M ISBN and log_date values ON A.member_no = M.member_no from the Reservation table JOIN reservation as R ON R.member_no = A.member_no for member numbers 250, JOIN item as I ON I.isbn = R.isbn 341, and 1675. Order the JOIN title as T results by member_no and ON T.title_no = I.title_no WHER E I.isbn = 288 log_date. You should show UNION ALL information for these SELECT I.isbn members, even if they have ,T.title ,J.member_no no books on reserve. ,M.lastname + , + M.firstname + + CASE WHEN M.middleinitial IS NULL THEN ELSE M.middleinitial + END as FullName, Juvenile as [Status] FROM Juvenile as J JOIN member as M ON J.member_no = M.member_no JOIN reservation as R ON R.member_no = J.member_no JOIN item as I ON I.isbn = R.isbn JOIN title as T ON T.title_no = I.title_no Return to TOC 13 WHERE I.isbn = 288
  14. 14. Query Proof Return to TOC 14
  15. 15. Alternate to Previous Query USE library; SELECT DISTINCT I.isbn ,T.title ,M.member_noWrite the above statement ,M.lastname + , + M.firstname + +again using a CASE statement. CASEYou cannot JOIN to the Adult WHEN M.middleinitial IS NULL THEN ELSE M.middleinitial + or Juvenile tables. Compare END as FullName,the two versions and [Status] = CASEdetermine which one is more WHEN EXISTS (SELECT * FROM dbo.adult WHERE dbo.adult.member_no=m.member_no) THEN Adultefficient. Cut and paste the ELSE Juvenileproof of your research. (No END FROMneed to copy the output of member as Mrecords) JOIN reservation as R ON R.member_no = M.member_no JOIN item as I ON I.isbn = R.isbn JOIN title as T ON T.title_no = I.title_no WHERE R.isbn = 288 ORDER BY FullName Return to TOC 15
  16. 16. Query Proof Return to TOC 16
  17. 17. Query Using Temporary Table to Store Records 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; GO SELECT 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.#overdue FROM loan as L JOIN member as M ON L.member_no = M.member_no JOIN title as T ON T.title_no = L.title_no WHERE due_date < CURRENT_TIMESTAMPReturn to TOC ORDER BY FullName; 17
  18. 18. Query Using Aggregate Function SUMWrite and execute a query that returns member_no, firstname, lastname and sum offine_paid for members who have paid the highest fines to date. Members should onlyappear once in the list. Display the highest fine first. If more than one member has paid thesame 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 FinePaid FROM member as M JOIN loanhist as LH ON M.member_no = LH.member_no GROUP BY M.member_no, M.firstname, M.lastname HAVING SUM(LH.fine_paid) IS NOT NULL ORDER BY FinePaid DESC, M.member_no; Return to TOC 18
  19. 19. Query with Calculated ColumnWrite 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 wishingto reserve a particular book. USE library; SELECT R.isbn ,(SELECT T.title FROM title as T WHERE I.title_no = T.title_no) as Title , COUNT(R.member_no) as Total FROM reservation as R JOIN item as I ON I.isbn = R.isbn GROUP BY R.isbn, I.title_no ORDER BY Title Return to TOC 19
  20. 20. Create ViewCreate 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. USE TSQLFundamentals2008; IF OBJECT_ID(Sales.View1) IS NOT NULL DROP VIEW Sales.View1; GO Create View Sales.View1 As SELECT orderid , DATENAME(WEEKDAY, orderdate) as "DAY" , DATENAME(MONTH, orderdate) as "MONTH" , DATENAME(DAY, orderdate) as "Date" , DATENAME(YEAR, orderdate) as "Year" FROM Sales.Orders Return to TOC 20
  21. 21. Stored ProcedureCreate 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 datefrom most recent to oldest. 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.empid JOIN Sales.Shippers as Ship ON Ship.shipperid = S.shipperid WHERE S.shipcountry = @ShipCntry Order BY S.orderdate Return to TOC 21
  22. 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. Return to TOC 22
  23. 23. Piggy Bank Database Diagram Return to TOC 23
  24. 24. Piggy Bank Stored Proc Example Return to TOC 24
  25. 25. Piggy Bank DDL Example Return to TOC 25
  26. 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. SSIS / SSRS Report Project 26
  27. 27. MiniAD Database Diagram Return to TOC 27
  28. 28. Import Orders Return to TOC 28
  29. 29. Import Orders for Each Loop Return to TOC 29
  30. 30. Import Products Data Flow Return to TOC 30
  31. 31. Import Vendors Return to TOC 31
  32. 32. Update Product Prices Return to TOC 32
  33. 33. Top Vendors Report Return to TOC 33
  34. 34. Top Vendors Design Return to TOC 34
  35. 35. Vendor Sales by Year Return to TOC 35
  36. 36. Vendor Sales Design Return to TOC 36
  37. 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. Return to TOC 37
  38. 38. BlockFlix Database Diagram Return to TOC 38
  39. 39. Add Movies from XML Phase One: Data Flow task Also puts info into staging table Inserts Movie and Cast info directly where Unique Keys weed out into their respective tables duplications Phase Two: Execute SQL Task Once 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 Three Run clean up to clear staging tables and save space. Return to TOC 39
  40. 40. Inside the Dataflow Task Get XML file Convert XML to correct data types Send directly to DB tableCapture errors Return to TOC 40
  41. 41. Handlingthe XML Return to TOC 41