Working with a SQL Server Data Mart

Similar documents
Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

IMPORTING DATA INTO EMPLOYEE TRAINING MANAGER

Graphical Joins in More Detail

AHC IE Data Shelter User Guide

Microsoft Access 2010

Join (SQL) - Wikipedia, the free encyclopedia

Microsoft Access 2013

Microsoft Access 2013

4.6.5 Data Sync User Manual.

Job Aid. Remote Access BAIRS Printing and Saving a Report. Table of Contents

9.4 Authentication Server

Managing Your Database Using Oracle SQL Developer

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Polaris SQL Introduction. Michael Fields Central Library Consortium

Access Intermediate

The following instructions cover how to edit an existing report in IBM Cognos Analytics.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Access Intermediate

Office 2016 Excel Basics 25 Video/Class Project #37 Excel Basics 25: Power Query (Get & Transform Data) to Convert Bad Data into Proper Data Set

Create a personal geodatabase

SharePoint 2010 Instructions for Users

User's Guide c-treeace SQL Explorer

Administration. Training Guide. Infinite Visions Enterprise Edition phone toll free fax

Griffin Training Manual Grif-WebI Introduction (For Analysts)

Workspace Administrator Help File

A Practical Introduction to SAS Data Integration Studio

SAP BusinessObjects Live Office User Guide SAP BusinessObjects Business Intelligence platform 4.1 Support Package 2

Ad Hoc Reports. 1. Click on Reports. 2. Select Ad Hoc Reports from the menu. 3. To start a new report, Click on the at the top of the screen.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Perceptive Matching Engine

HL7Spy 1.1 Getting Started

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Enterprise Reporting -- APEX

View my bill online. User guide

Griffin Training Manual Grif-WebI Intermediate Class

By Scott Falla and Jamie Batiste 08/03/2010

Part 2 Uploading and Working with WebCT's File Manager and Student Management INDEX

SQL Server Reporting Services

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Table of Contents. Revu ipad. v3.6. Navigation. Document Manager. File Access. Markups. Signature Tool. Field Verification Measurements

City College of San Francisco Argos Training Documentation

Content-Based Assessments

MicroStrategy Desktop

New Perspectives on Access Module 5: Creating Advanced Queries and Enhancing Table Design

sohodox Quick Start Guide

Illinois Vital Records System Physician User Manual. Table of Contents

SAS. Information Map Studio 3.1: Creating Your First Information Map

Troubleshooting the scheduled Patron XML Update v9.0, v9.5, and v9.8

Release Notes Version 5.3.2

CRM CUSTOMER RELATIONSHIP MANAGEMENT

Microsoft Excel 2007 Macros and VBA

Join Queries in Cognos Analytics Reporting

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Introduction to Cognos

Learning Management System 2.0 User Information Guide

TeleFlash. Internal Research Notes and Custom Data Publisher

PISCES Installation and Getting Started 1

Test Information and Distribution Engine

Using DbVisualizer Variables

GUARDTOOL IMPORTER ADDENDUM

Contents 1. OVERVIEW GUI Working with folders in Joini... 4

If this is the first time you have run SSMS, I recommend setting up the startup options so that the environment is set up the way you want it.

STIDistrict Query (Basic)

Policy Commander Console Guide - Published February, 2012

Perceptive Nolij Web. Administrator Guide. Version: 6.8.x

Microsoft Access 2010

AUTOTEXT MASTER 1 PROGRAM HELP GILLMEISTER SOFTWARE.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

SQL Server. Management Studio. Chapter 3. In This Chapter. Management Studio. c Introduction to SQL Server

Microsoft Access 2007 Module 2

JOHNS HOPKINS IT - SERVICE NOW USER TRAINING

Contents Using the Primavera Cloud Service Administrator's Guide... 9 Web Browser Setup Tasks... 10

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

REPORTING Copyright Framework Private Equity Investment Data Management Ltd

Junxure Code Upgrade Instructions

Sage Estimating (SQL) v17.13

Expense Management Asset Management

Order Entry. ARUP Connect

Module 2: Managing Your Resources Lesson 5: Configuring System Settings and Properties Learn

Solution Explorer Copy Items in a Solution Files That Manage Solutions and Projects Solutions (SQL Server Management Studio) Associate a Query with a

ZENworks Reporting System Reference. January 2017

Microsoft Office 2016 Mail Merge

External Data Connector for SharePoint

CollabNet Desktop - Microsoft Windows Edition

Chapter 2 The SAS Environment

To complete this database, you will need the following file:

Microsoft Access 2003 Quick Tutorial

WebStudio User Guide. OpenL Tablets BRMS Release 5.18

Sample A2J Guided Interview & HotDocs Template Exercise

Readme. HotDocs Developer Table of Contents. About This Version. About This Version. New Features and Enhancements

Microsoft Office 2010: Introductory Q&As Access Chapter 2

Introduction to Cognos Participants Guide. Table of Contents: Guided Instruction Overview of Welcome Screen 2

SAS Data Integration Studio 3.3. User s Guide

Argos. Basic Training

COGNOS Multiple Queries

Introduction to Microsoft Access 2016

Ektron Advanced. Learning Objectives. Getting Started

Clinical Looking Glass Introductory Session In-Class Exercise Two: Congestive Heart Failure

CenterStone. Release Notes. Manhattan Software Inc. World Leading Real Estate, Asset & Facilities Management Software. Version 4.

Web Intelligence Reporting Basics HTML Version

Transcription:

Working with a SQL Server Data Mart SQL Server Management Studio Logging In Navigating Basic SQL SELECT Statement WHERE Clause JOINs Exporting Data to CSV Advanced SQL Additional Resources SQL Server Management Studio SQL Server Management Studio is an interface to query the data mart using SQL. SQL queries will allow you to get data from a single table or from many tables using JOINs. Logging In To begin, open SQL Server Management Studio 2014 from the desktop shortcut, or through the start menu. When the application opens, it will ask you to connect to the server. The following values should be defaulted: Server type: Database Engine Server name: rsp-hr-db1.ahc.umn.edu Authentication: Windows Authentication Ensure that these are the values that you see, and click Connect. This will connect you to the database server using your Active Directory (AD) credentials. These are the credentials that you used to login to the Data Shelter. Permissions are configured to give you access to only the data marts that you are authorized to view. You may have more than one data mart available to you if you are listed on more than one request. The databases are named in the following format: [PI Last Name][PI First Name Initial]-Req[5 digit request number] PI Name: Jane Doe; Request Number: 42 -> DoeJ-Req00042 Once connected you will see a navigation tree in the left pane of the application, as shown below.

Navigating Once connected to the server you will need to navigate to your data mart. Expand Databases by clicking the plus icon or by double-clicking the text. This will display a list of databases. Find the database you wish to work with based on the naming convention above, and expand the database. There are two main items under the database you will be concerned with; Tables and Views. When you expand the Tables item you will see a the list of tables that are included in your data mart. If you expand a table you will see the details about the table, including Columns. If you expand Columns you will see the list of columns that are on the table. To quickly review the data that is in a single table, right-click on the table name and select Select Top 1000 Rows.

This will open a pre-written query that will show 1000 rows of data from the selected table. This is a convenience method and can help you get started. For more details on writing SQL SELECT statements continue to Basic SQL. Basic SQL SELECT Statement The most common task in SQL is the SELECT statement. To write a SELECT statement, a Query Editor Window will need to be opened. The simplest way to do this is to right-click on the name of your database and select New Query. This will open an editor to the right of the navigation tree. An example of the simplest SELECT statement is below: SELECT * FROM dbo.patient SQL is not case-sensitive, and is not affected by white-space. A SELECT statement begins with the SELECT keyword, and will be followed by the list of columns to be returned. In this example the asterisk means all columns. Next is the FROM clause; this specifies the table that should be queried. In this example the PATIENT table. After entering the above query into your Query Editor Window, execute it. This can be done by clicking the Execute button in the toolbar, or by pressing F5 on the keyboard. The results will be shown in a table below the query text.

That is the most basic SELECT statement. However, many times we may not need every column from the table. Let's refine our query to get the patients' Id, birth date, sex, and marital status. SELECT PATIENT.PATIENT_ID, PATIENT.BIRTH_DATE, PATIENT.SEX, PATIENT.MARITAL_STATUS FROM dbo.patient The above query is very similar to our earlier query, but explicitly defines the columns that should be returned from the table. Also, notice that each column is prefixed with the name of the table and separated by a dot (.). Prefixing the columns with the table name is not required and can be omitted on simple queries. The prefixes become necessary when we start using JOINs, which will be covered later. Let's review the results by executing the query as we did before. The results should display as they did before, and only the columns requested are returned. WHERE Clause The WHERE clause is used to filter data returned by the query. The most basic way to filter is on equality. Let's take our earlier query of patients and only get those that are female.

SELECT PATIENT.PATIENT_ID, PATIENT.BIRTH_DATE, PATIENT.SEX, PATIENT.MARITAL_STATUS FROM dbo.patient WHERE SEX = 'F' Running the above query will return all patients that are female. The WHERE clause can contain comparison operators and logical operators. Below are brief descriptions of these operators. Comparison Operators Operator Description Example Result = is equal to PATIENT.SEX = 'F' Only patients where SEX is equal to 'F' will be returned in the results. < less than SERVICE.ENCOUNTER_DATE < '2014-01-01' <= less than or equal to SERVICE.ENCOUNTER_DATE <= '2014-01-01' > greater than SERVICE.ENCOUNTER_DATE > '2014-01-01' >= greater than or equal to SERVICE.ENCOUNTER_DATE >= '2014-01-01' Only services where the encounter dates come before midnight on January 1, 2014 will be returned. Note that the less than operator works with dates, numbers, and text. This similar to less than, but will include encounters that occur at midnight on January 1, 2014. Only services where the encounter dates come after midnight on January 1, 2014 will be returned. Similar to greater than,but will include encounters that occur at midnight on January 1, 2014.

<>,!= not equal to PATIENT.SEX <> 'F' Only patients where SEX is not equal 'F' will be returned. Note that NULL v alues for SEX will not be returned. Anything compared to NULL is false. LIKE is like PATIENT.LAST_NAME LIKE '%JOHN%' Returns patients with a last name that contains the text 'JOHN'. The '%' is a wildcard. It will match any character. In the example '%' is used on both ends of the search, so 'JOHN' may appear anywhere in the last name. The example can be updated to search for last names that begin with 'JOHN' as follows, PATIENT.LAST_NAME like 'JOHN%'. IS NULL is null PATIENT.SEX IS NULL Returns rows where the SEX is null. This is the only way to equate something to NULL. Logical Operators Operator Description Example Result AND and PATIENT.SEX = 'F' AND PATIENT.MARITAL_STATUS = 'M' OR or PATIENT.SEX = 'M' OR PATIENT.MARITAL_STATUS = 'S' NOT not NOT (PATIENT.SEX = 'F' AND PATIENT.MARITAL_STATUS = 'M') Married females will be returned. All males and all singles will be returned. Note that only single females will be returned, and all males regardless of marital status. The NOT operator negates a statement. The inner statement would return all married females, with the NOT applied it will return all other patients that are not both married and female. The NOT operator can be used with IS NULL to get all non- NULL values (i.e. PATIENT.SEX IS NOT NULL). A WHERE clause can be simple or can become very complicated. Parenthesis are used to group statements together in the order they should be applied. There is an order of operations that is used when evaluating AND and OR clauses. The AND operator will be evaluated before the OR operator. Review the following example. SELECT PATIENT.PATIENT_ID, PATIENT.BIRTH_DATE, PATIENT.SEX, PATIENT.MARITAL_STATUS FROM dbo.patient WHERE SEX = 'F' AND MARITAL_STATUS = 'D' OR MARITAL_STATUS = 'W' It would be easy to expect the above query to return females that are divorced or widowed. However, this not what is returned. This query will return females that are divorced or any other patients that are widowed. To get the results that are expected parenthesis should be used as in the updated example below.

SELECT PATIENT.PATIENT_ID, PATIENT.BIRTH_DATE, PATIENT.SEX, PATIENT.MARITAL_STATUS FROM dbo.patient WHERE SEX = 'F' AND (MARITAL_STATUS = 'D' OR MARITAL_STATUS = 'W') Note the parenthesis around the two statements separated by the OR. In most cases if you are going to use an OR operator you should be using parenthesis to group your statements. JOINs JOINs are used to combine related data together into a single query. For example, let's query services and include the patients' first name, last name, MRN. Let's start with the query below querying from the SERVICE table. SELECT SERVICE.SERVICE_ID, SERVICE.ENCOUNTER_DATE, SERVICE.TYPE_CODE, SERVICE.DEPARTMENT_NAME FROM dbo.service This is return all services and contains the services' Id, encounter date, type code, and department name. However, we don't have any patient information to support this data. To add the patient data we'll add a JOIN to the PATIENT table. SELECT SERVICE.SERVICE_ID, SERVICE.ENCOUNTER_DATE, SERVICE.TYPE_CODE, SERVICE.DEPARTMENT_NAME, PATIENT.PATIENT_ID, PATIENT.SEX FROM dbo.service INNER JOIN dbo.patient ON PATIENT.PATIENT_ID = SERVICE.PATIENT_ID There are a couple things of note in this new query. First, we'll start line 9 that begins with INNER JOIN. This is the specification to join the data from another table. In this case we specifying a JOIN to the PATIENT table where the PATIENT_ID column from both tables are equal to each other. Also of note is the INNER JOIN itself. An INNER JOIN means that each row from the SERVICE table must match a corresponding row from the PATIENT table. If there were no corresponding PATIENT row, the SERVICE row would be omitted from the results. Second, you'll see the new column beginning at line 6. These columns are coming from the PATIENT table. Note that there is a PATIENT_ID column on both tables, and so the table prefix on PATIENT_ID is required. There are three common types of joins. The basics of each kind of join is below. INNER JOIN - returns all rows where there is a match on both tables LEFT JOIN - returns all rows from the first table; will include matches from the second table if found RIGHT JOIN - returns all rows from the second table; will include matches from the first table if found

Let's make a simple test case to demonstrate the differences between each of the joins. Consider the data below. Employee Table PERSON_ID FIRST_NAME LAST_NAME DEPARTMENT_ID 1 Adam West 1 2 John Doe 1 3 Jane Doe 2 4 Ann Johnson NULL Department Table DEPARTMENT_ID DEPARTMENT_NAME 1 First floor 2 Second floor 3 Third floor Let's start with an INNER JOIN. SELECT * FROM EMPLOYEE INNER JOIN DEPARTMENT ON EMPLOYEE.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID INNER JOIN Results PERSON_ID FIRST_NAME LAST_NAME DEPARTMENT_ID DEPARTMENT_ID DEPARTMENT_NAME 1 Adam West 1 1 First floor 2 John Doe 1 1 First floor 3 Jane Doe 2 2 Second floor Note that the results are only those employees that had a matching department Id in the DEPARTMENT table. Next, we'll use the same query, but make it a LEFT JOIN. SELECT * FROM EMPLOYEE LEFT JOIN DEPARTMENT ON EMPLOYEE.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID LEFT JOIN Results PERSON_ID FIRST_NAME LAST_NAME DEPARTMENT_ID DEPARTMENT_ID DEPARTMENT_NAME 1 Adam West 1 1 First floor 2 John Doe 1 1 First floor 3 Jane Doe 2 2 Second floor 4 Ann Johnson NULL NULL NULL Note that all employees are returned. The employee, Ann Johnson, has NULL data in the department fields because she had no matching department. Finally, we'll do the query one last time with a RIGHT JOIN.

SELECT * FROM EMPLOYEE RIGHT JOIN DEPARTMENT ON EMPLOYEE.DEPARTMENT_ID = DEPARTMENT.DEPARTMENT_ID RIGHT JOIN Results PERSON_ID FIRST_NAME LAST_NAME DEPARTMENT_ID DEPARTMENT_ID DEPARTMENT_NAME 1 Adam West 1 1 First floor 2 John Doe 1 1 First floor 3 Jane Doe 2 2 Second floor NULL NULL NULL NULL 3 Third floor The results here are similar to the LEFT JOIN. However, this time the outcome is flipped so that all the rows from the DEPARTMENT are returned, even if there is no match. The use of RIGHT JOINs is very infrequent. Typically, a query would be re-written to a LEFT JOIN. In this case the query would use the DEPARTMENT table as the base of the query and LEFT JOIN to the EMPLOYEE table. A post by Jeff Atwood may help you to visualize each of the JOINs used above. Exporting Data to CSV The results of a query can be exported to a CSV file by right-clicking on the results and selecting Save Results As... Notice that the right-click occurs within any of the table cells, and not the tab or header columns. This will open a dialog to save the new file. Navigate in the explorer to the project folder, give the file a name, and click Save. This will create a CSV file of the data that can be opened in Excel. Ensure that the file is saved on the network to your request project folder. Do not save the file locally.

Advanced SQL Coming soon... Additional Resources w3schools: SQL Home Technet: Writing SQL Queries: Let's Start with the Basics