Www.prismatech.net SQL from the Trenches Presented by: Robert Arce.

Slides:



Advertisements
Similar presentations
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Advertisements

Line Efficiency     Percentage Month Today’s Date
Unit Number Oct 2011 Nov 2011 Dec 2011 Jan 2012 Feb 2012 Mar 2012 Apr 2012 May 2012 Jun 2012 Jul 2012 Aug 2012 Sep (3/4 Unit) 7 8 Units.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Whole Numbers and Tables: Writing, Rounding, and Inequalities Section 1.1.
Oracle Data Definition Language (DDL)
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Creating a Table Create a table, “emp”, containing: –empno – a 4 digit employee number –ename – up to 10 character string –job – up to 9 character string.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
STRUCTURED QUERY LANGUAGE Chandra S. Amaravadi 1.
Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Recap of SQL Lab no 8 Advance Database Management System.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Structured Query Language
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Jan 2016 Solar Lunar Data.
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
CS580 Advanced Database Topics
The Database Exercises Fall, 2009.
Q1 Jan Feb Mar ENTER TEXT HERE Notes
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
Chapter 4 Summary Query.
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Structured Query Language
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com
Step 3 Step 2 Step 1 Put your text here Put your text here
Query Functions.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Manipulating Data Lesson 3.
Project timeline # 3 Step # 3 is about x, y and z # 2
Shelly Cashman: Microsoft Access 2016
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

SQL from the Trenches Presented by: Robert Arce

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Scalar Functions CHAR DATE SUBSTR DAYS DECIMAL DIGITS Literals: CURRENT DATE CURRENT TIME TIMESTAMP NULL (is/is not) Examples: DECIMAL(Salary/52, 9, 2) It will round the result INTEGER(Salary) it will round the number to the next integer. COUNT(DISTINCT DEPT) Different departments DAY( DATE(CURRENT DATE) - DATE(' ') ) Number of days on this year

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Selection Statement 1)SELECT something(s) 2)FROM somewhere 3)WHERE condition(s) 4)GROUP BY column(s) 5)HAVING condition(s) 6)ORDER BY something Valid Combination: 1,2,3 1,2,4 1,2,6 1,2,3,4 1,2,3,6 1,2,3,4,5 1,2,3,4,5,6

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Selection Select DEPARTMENT, count(DEPARTMENT), decimal(avg(ACUMSALES),9,2) from STORDEPT group by DEPARTMENT; 1. HAVING COUNT(*) > 1; (gets departments with multiple Emp.) 2. HAVING Dept IN('D100', 'D300'); Remember: the Having clause can only have columns included in the group by statement. When using Group By, every field in the Select clause except count(*) or the field(s) having the sum, avg, min, max, var functions must be included in the Group By clause.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Select count(distinct DEPARTMENT) from STORDEPT;

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Select DEPARTMENT, count(DEPARTMENT), decimal(avg(ACUMSALES),9,2) from STORDEPT group by DEPARTMENT;

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Selection – Case (21Jan04) Select substr(digits(day(current date)),9,2) || (case month(current_date) when 01 then 'Jan' when 02 then 'Feb' when 03 then 'Mar' when 04 then 'Apr' when 05 then 'May' when 06 then 'Jun' when 07 then 'Jul' when 08 then 'Aug' when 09 then 'Sep' when 10 then 'Oct' when 11 then 'Nov’ else 'Dec' end) || substr(digits(year(current date)),9,2) From lib/file

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Functions and result current date  01/21/04 day(current date)  21 (numeric) digits(day(current date))  substr(digits(day(current date)),9,2)  21 alpha

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Inner Selection Select * from CUSMS where custno in ( Select custnum from CUSSLS where cussales > and yearsls = 2003) Selects records from CUSMS file only if the customer number is found in CUSSLS file …

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Left Outer Join Select * from CUSMS left outer join SALE on CUSMS.CUSTID = SALE.CUSTID Gets found and not found records setting null values to the not found record fields from the second file.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Left Inner Selection Select * from CUSMSADD A where not exist (select * from CUSMSADDSV B where A.company=B.company and A.customer=B.customer) It will bring only records from A file that don't exist in the B file.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Insert Insert into DEPT (DEPT_ID, DEPT_NAME, MANAGER) VALUES ('D444',,'334339') A comma has been used as a place holder.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Insert entire table Insert into DEPT Select * from TEST_DEPT where DEPT_NAME = ‘IMPORT’ Assumes that the TEST_DEPT table has the same number of columns in the same order as DEPT.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Delete Delete from EMPLOYEE where SALARY > or DEPT_ID = ‘D999’ Only deletes WHOLE rows. Select first and then change the selection for a deletion.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Update update CUSMS set CMCSCL='99' where cmcsno in (select SMCUS from REPSUP, CUSMS where SMCUS = CMCSNO and SMREPT <> 'WR' and CMCSCL <> '99' and SMSUSP <> 'S')

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Update Update CUSMS as C set (custaddr1, custaddr2, custcity, cusstate, custzip) = (Select addr1, addr2, city, state, zip from NEWADDR as NA where C.custnbr=NA.custnbr) where custnbr in (Select custnbr from NEWADDR) Updates fields in file C from file NA making the join by the cusnbr field.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. STRSQL F13=Services  this function will allow you to access the session services. One of the most important options is: 1. Change session attributes. In this option you can set the session to create a file that you specify as the output of you selection instead of just being display on your screen.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. IBM iSeries Navigator

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Runsqlstm – text source /* Robert Arce (PrismaTech, Inc.) */ /* DL Dedicated Locations */ Delete from DEDLOC; marks end of statement Insert into DEDLOC (select substr(WLITNO,1,20) ITEM, substr(WLLOCA,1,3) WRHS, substr(WLLOCA,4,7)ADDR, integer(WLRSLV) MIN, integer(WLRSQT) MAX from LOCATIONS where WLITNO <> ' ' and WLASCD ='P');

SQL from the Trenches by Robert Arce from PrismaTech, Inc. Runsqlstm – CL Add this statement to your CL: RUNSQLSTM SRCFILE(&LIBSRC/QSQLSRC) SRCMBR(mytextsource) COMMIT(*NONE) Set your file overrides and library list for your sql to use them when executing.

SQL from the Trenches by Robert Arce from PrismaTech, Inc. year(CURRENT DATE) - Integer(substr('1997XXX',1,4)) Thank you !!! Result is 7 Good Luck