basic logic, sql statements and/or you a quick tour of basic logic and how to evaluate logical...
DESCRIPTION
Statement of Truth (Not!) Evaluations can also be based on numerical values: NumCat = 1 NumBike < 1 TRUE FALSETRANSCRIPT
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
Statement of Truth (Not!)
Evaluations can also be based on numerical values:
NumCat = 1
NumBike < 1
TRUE
FALSE
We know its value, now what?
Statement can be combined using the following conjunctions:
• AND• OR• NOT• Others…
AND Truth Table
X Y X AND Y
False False
True False
False True
True True
FALSE
FALSE
FALSE
TRUE
OR Truth Table
X Y X OR Y
False False
True False
False True
True True
FALSE
TRUE
TRUE
TRUE
NOT Truth Table
X NOT X
False
True
TRUE
FALSE
Other Truths
• NAND = NOT AND: (Inverts AND result)
• NOR = NOT OR: (Inverts OR result)
• XOR: True if only one operator is true
Conditionals
How to compare to values.
Conditional Statements
• Equal• Not Equal• Greater Than• Less Than• Greater Than or Equal To• Lesser Than or Equal To• Like
=< > “≠”><>= “≥”<= “≤”Like
=< >><>=<=Like
X = 4X < > 9Y > 2X < 1 BazillionX >= 4Y <= 7Z Like “Do%”
Conditional Statements
Set Values:X=4Y=5Z=“Dog”
Examples
Example 1:
(Cat = Black) AND (Bike = Red)
FALSE
Example 2:
(Cat = Yellow) OR (NumBike > 1)
FALSE
Example 3:
(Cat <> Grey) OR (Bike = Green)
TRUE
Example 4:
(NumCat <= 2) AND (Bike = Blue)
TRUE
Example 5:
NOT (Cat Like “Ca%”)
FALSE
Example 6:
NOT ((NumCat < 10) AND (Bike Like “Blu%))OR ((NumCat <= NumDog) OR (Bike = Blue))
TRUE
Basic SQL Statement
Bare Minimums
SELECT fieldname 1, fieldname 2, ...fieldname n
FROM tablename
Adding Conditionals
SELECT fieldname 1, fieldname 2, ...fieldname n
FROM tablenameWHERE (fieldname conditional value)
logic_operand (fieldname conditional value) ...
Adding Sort Values
SELECT fieldname 1, fieldname 2, ...fieldname n
FROM tablenameWHERE (fieldname conditional value)
logic_operand (fieldname conditional value) ...
ORDER BY fieldname
SQL Statement Examples
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
Crew Query
SELECT RequestNumber, ResourceName AS Crew
FROM vBasicRpts WHERE Status <> 'D' AND Agency = 'BIA'
and KindCode in ('HC1', 'HC2')
Personnel Count by Agency
SELECT Agency, COUNT(*) AS [Agency Count], SUM(NumberPersonnel) AS NumberPersonnel
FROM vBasicRpts GROUP By Agency
DIVS Checkin
SELECT ResourceName, CheckinDate FROM vBasicRpts
WHERE KindCode = 'DIVS' and CheckinDate between '05/15/07' and '05/20/07'
ORDER BY ResourceName
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.
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
Questions?