+ All Categories
Home > Documents > Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical...

Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical...

Date post: 08-Jan-2018
Category:
Upload: diana-holland
View: 218 times
Download: 1 times
Share this document with a friend
Description:
Statement of Truth (Not!) Evaluations can also be based on numerical values: NumCat = 1 NumBike < 1 TRUE FALSE
30
Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.
Transcript
Page 1: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Basic Logic, SQL Statements AND/OR You

A quick tour of basic logic and how to evaluate logical expressions and

implement simple SQL queries.

Page 2: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Statement of Truth (Not!)

The statement is the foundational basis for logic. Is the statement TRUE or FALSE?

Cat = Black

Bike = Red

TRUE

FALSE

Page 3: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Statement of Truth (Not!)

Evaluations can also be based on numerical values:

NumCat = 1

NumBike < 1

TRUE

FALSE

Page 4: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

We know its value, now what?

Statement can be combined using the following conjunctions:

• AND• OR• NOT• Others…

Page 5: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

AND Truth Table

X Y X AND Y

False False

True False

False True

True True

FALSE

FALSE

FALSE

TRUE

Page 6: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

OR Truth Table

X Y X OR Y

False False

True False

False True

True True

FALSE

TRUE

TRUE

TRUE

Page 7: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

NOT Truth Table

X NOT X

False

True

TRUE

FALSE

Page 8: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Other Truths

• NAND = NOT AND: (Inverts AND result)

• NOR = NOT OR: (Inverts OR result)

• XOR: True if only one operator is true

Page 9: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Conditionals

How to compare to values.

Page 10: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Conditional Statements

• Equal• Not Equal• Greater Than• Less Than• Greater Than or Equal To• Lesser Than or Equal To• Like

=< > “≠”><>= “≥”<= “≤”Like

Page 11: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

=< >><>=<=Like

X = 4X < > 9Y > 2X < 1 BazillionX >= 4Y <= 7Z Like “Do%”

Conditional Statements

Set Values:X=4Y=5Z=“Dog”

Page 12: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Examples

Page 13: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 1:

(Cat = Black) AND (Bike = Red)

FALSE

Page 14: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 2:

(Cat = Yellow) OR (NumBike > 1)

FALSE

Page 15: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 3:

(Cat <> Grey) OR (Bike = Green)

TRUE

Page 16: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 4:

(NumCat <= 2) AND (Bike = Blue)

TRUE

Page 17: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 5:

NOT (Cat Like “Ca%”)

FALSE

Page 18: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Example 6:

NOT ((NumCat < 10) AND (Bike Like “Blu%))OR ((NumCat <= NumDog) OR (Bike = Blue))

TRUE

Page 19: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Basic SQL Statement

Page 20: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Bare Minimums

SELECT fieldname 1, fieldname 2, ...fieldname n

FROM tablename

Page 21: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Adding Conditionals

SELECT fieldname 1, fieldname 2, ...fieldname n

FROM tablenameWHERE (fieldname conditional value)

logic_operand (fieldname conditional value) ...

Page 22: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Adding Sort Values

SELECT fieldname 1, fieldname 2, ...fieldname n

FROM tablenameWHERE (fieldname conditional value)

logic_operand (fieldname conditional value) ...

ORDER BY fieldname

Page 23: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

SQL Statement Examples

Page 24: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Generic Report 2

SELECT RequestNumber, ResourceName AS Name, Agency, KindCode AS Kind, Trainee as T, UnitID, CheckinDate, CheckinTime

FROM vBasicRptsWHERE kindcode like 'hc%' or kindcode =

'cc'ORDER BY RequestNumber

Page 25: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Crew Query

SELECT RequestNumber, ResourceName AS Crew

FROM vBasicRpts WHERE Status <> 'D' AND Agency = 'BIA'

and KindCode in ('HC1', 'HC2')

Page 26: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Personnel Count by Agency

SELECT Agency, COUNT(*) AS [Agency Count], SUM(NumberPersonnel) AS NumberPersonnel

FROM vBasicRpts GROUP By Agency

Page 27: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

DIVS Checkin

SELECT ResourceName, CheckinDate FROM vBasicRpts

WHERE KindCode = 'DIVS' and CheckinDate between '05/15/07' and '05/20/07'

ORDER BY ResourceName

Page 28: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Just For Grins…

The following query is an example of how to perform a selection statement from multiple tables as well as a method of minimizing the amount of typing needed when specifying those tables.

The query itself is designed to produce a list of records that has Accounting Codes that are set incorrectly. Note: Make sure you understand all components of this query before using on an active database. You must accept all responsibility for it’s use.

Page 29: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Just For Grins…SELECT C.RequestNumber, C.ResourceName, C.AgencyCode,

C.HomeUnit, C.SectionDesc, C.KindCode, C.ActivityDate, C.AccountingCode, C.AccrualCode, B.Status

FROM vCost C ,VBasicRpts BWHERE (C.RequestNumber = B.RequestNumber ) and (B.Status like 'C')

and (C.RequestNumber like 'c%' or C.RequestNumber like 'o%') and not ((C.AccountingCode like 'paxy99' and C.AccrualCode like 'STO') or (C.AgencyCode like 'BIA' and C.AccountingCode like 'xy99') or (C.AgencyCode like 'BLM' and C.AccountingCode like 'xy99') or (C.AgencyCode like 'NPS' and C.AccountingCode like 'xy99') or (C.AgencyCode like 'FWS' and C.AccountingCode like 'xy99')or (C.AgencyCode like 'FS' and C.AccountingCode like 'paxy99')or ((C.AgencyCode like 'PVT' or C.AgencyCode like ‘CA') and

( C.AccountingCode like ‘99999') or (C.AccountingCode like ‘99998')))

ORDER BY AgencyCode

Page 30: Basic Logic, SQL Statements AND/OR You A quick tour of basic logic and how to evaluate logical expressions and implement simple SQL queries.

Questions?


Recommended