chapter 3 query language database application sak 3408

78
Chapter 3 Chapter 3 Query Language Query Language Database Application Database Application SAK 3408 SAK 3408

Upload: melvin-morris

Post on 11-Jan-2016

236 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 Query Language Database Application SAK 3408

Chapter 3Chapter 3Query Query

LanguageLanguage

Database ApplicationDatabase ApplicationSAK 3408SAK 3408

Page 2: Chapter 3 Query Language Database Application SAK 3408

What is SQL?

SQL or Structured Query Language is an English-like language used to create and manipulate databases.SQL is an ANSI (American National Standards Institute) standard for accessing database systems. SQL statements are used to retrieve and update data in a database.SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc.With SQL, we can query a database and have a result set returned.

Page 3: Chapter 3 Query Language Database Application SAK 3408

Types of StatementsThree types of statements:

Data Definition Language (DDL)

Data Manipulation Language (DML)

Data Control Language (DCL)

Data Definition Language creates and manipulates the structure, delete or define indexes of table in databaseData Manipulation Language manipulates data in the tableData Control Language determines who is allowed to what within the database

Page 4: Chapter 3 Query Language Database Application SAK 3408

SQL Grammar

SQL statements are always terminated by a semi-colon (;)

SELECT CUSTOMER_ID

FROM CUSTOMER;

Page 5: Chapter 3 Query Language Database Application SAK 3408

SQL GrammarSQL statements can be entered on a

single line or split across multiple lines (preferred)

SELECT CUSTOMER_ID, CUSTOMER_NAME, CREDIT_LINE FROM CUSTOMER WHERE CREDIT_LINE > 1000000 AND CUSTOMER_ID IN [1000,2000];

SELECT CUSTOMER_ID, CUSTOMER_NAME, CREDIT_LINE FROM CUSTOMERWHERE CREDIT_LINE > 1000000 AND CUSTOMER_ID IN [1000,2000];

Page 6: Chapter 3 Query Language Database Application SAK 3408

SQL Grammar

SQL statements are not case sensitive; however data is.

SeLEct *fROm REQueSTwHErE aCcT_sT In [‘GA’,’WA’,’NJ’];

Returns data for all requests where the state is either GA, WA or NJ. No records would have been returned if the where clause had been:

wHErE aCcT_sT In [‘ga’,’wA’,’Nj’];

Page 7: Chapter 3 Query Language Database Application SAK 3408

Qualifying a Field Name

When the same field name occurs in two or more tables used in a single SQL statement, you must qualify the field name much like you would include the last name to avoid confusion if two people have the same first name.

Tablename.fieldnameCustomer.last_name

Page 8: Chapter 3 Query Language Database Application SAK 3408

Data Definition Language (DDL)

Used to create and modify database objects

Create

Drop

Alter

Page 9: Chapter 3 Query Language Database Application SAK 3408

Data Manipulation Language (DML)

Used to create, modify and retrieve data

Insert

Select

Update

Delete

Page 10: Chapter 3 Query Language Database Application SAK 3408

Data Control Language (DCL)

Used to control database privileges

Grant

Revoke

Page 11: Chapter 3 Query Language Database Application SAK 3408

DDL - Creating a Table

Use the Create keyword and specify:table namefield (column) name(s)field typeconstraints

primary key - unique identifier for a recordforeign key - establishes relationshipcheck - value must be in the specified listnot null - must have a valueunique - value must be unique

Page 12: Chapter 3 Query Language Database Application SAK 3408

Table with Primary Key Only

CREATE TABLE room( roomID number, bldg char(1) CHECK (bldg IN

('A','B')), roomNo varchar2(10), maxCapacity number, style varchar2(15) CHECK (style IN

('LECTURE','LECTURE/LAB','LAB','OFFICE')),

CONSTRAINT room_pk PRIMARY KEY (roomID));Constraint_name

Constraint_type

Constraint_attributes

Page 13: Chapter 3 Query Language Database Application SAK 3408

Table with Foreign KeyCREATE TABLE faculty( facultyID number, lname varchar2(30) NOT NULL, fname varchar2(20) NOT NULL, dept varchar2(5), officeID number, phone varchar2(15), email varchar2(75) UNIQUE, rank char(4) CHECK (rank IN ('INST', 'ASOC','ASST','FULL','SENR')), CONSTRAINT faculty_pk PRIMARY KEY (facultyID), CONSTRAINT faculty_fk FOREIGN KEY (officeID)

REFERENCES room(roomID));

Page 14: Chapter 3 Query Language Database Application SAK 3408

Table with Compound Primary Key

create table participation( eventID number, memberID number, constraint participation_pk primary key

(eventID, memberID), constraint participation_event_fk foreign

key (eventID) references event(eventID), constraint participation_member_fk foreign

key (memberID) references member(memberID));

Page 15: Chapter 3 Query Language Database Application SAK 3408

DDL - Altering a Table

Use the Alter and Add/Modify keywords and specify:

table namenew or existing field name(s)field type

alter table invoice add(sent_dt date);

alter table invoice modify (invoice_nbr varchar2(5));

Page 16: Chapter 3 Query Language Database Application SAK 3408

DDL - Removing a Table

Use the Drop keyword and specify:table name

drop table invoice;

If this table is referenced by a foreign key, use the cascade constraints clause

drop table invoice cascade constraints;

Page 17: Chapter 3 Query Language Database Application SAK 3408

Indexes

Conceptually similar to book indexIncreases data retrieval efficiencyAutomatically assigns record numbersUsed by DBMS, not by usersFields on which index built called Index KeyHave a sorted order

Can guarantee uniqueness

Can include one or more fields

Page 18: Chapter 3 Query Language Database Application SAK 3408

Indexes – SyntaxCREATE UNIQUE INDEX cust_num_ind ON

customer(Name);

CREATE INDEX CustomerName ON Customer (CustomerNum);

CREATE INDEX CreditLimitRep_ind ON Customer(CreditLimit, RepNum);

DROP INDEX RepBal;

Page 19: Chapter 3 Query Language Database Application SAK 3408

Customer Table with Record NumbersFigure 4.10

Page 20: Chapter 3 Query Language Database Application SAK 3408

Customer Table Index on CustomerNum Figure 4.11

Page 21: Chapter 3 Query Language Database Application SAK 3408

Table Indexes on CreditLimit, RepNumFigure 4.12

Page 22: Chapter 3 Query Language Database Application SAK 3408

Indexes

PROsFaster/more efficient data retrieval

CONsRequires additional spaceIncreased overhead

Page 23: Chapter 3 Query Language Database Application SAK 3408

Data Manipulation Language (DML)

Used to create, modify and retrieve data

Insert

Select

Update

Delete

Page 24: Chapter 3 Query Language Database Application SAK 3408

DML - Adding Data to a Table

Use the Insert keyword and specify:table namefield names - optionalvalues for each field

insert into customer values(‘Teplow’,’MA’,23445.67);

OR

insert into customer (last_name, state_cd, sales) values (‘Teplow’ ,’MA’,23445.67);

Page 25: Chapter 3 Query Language Database Application SAK 3408

DML - Updating Data in a Table

Use the Update and Set keywords and specify:

table namefield name(s)where clause (optional)

update inventory set price = price*1.05;

update inventoryset price = price*1.05where product_id = 'P103';

Page 26: Chapter 3 Query Language Database Application SAK 3408

DML-Deleting Records

Use the Delete From keywords and specify:

table namewhere clause (optional)

delete from customer;

delete from customer

where sales < 10000;

Page 27: Chapter 3 Query Language Database Application SAK 3408

Parts of a DML Select Statement

Select

From

Where (optional)

Order By (optional)

Group By (optional)

Having (optional)

Page 28: Chapter 3 Query Language Database Application SAK 3408

SELECT

Which fields do you want retrieved?

* is used to select all fields in the table

Page 29: Chapter 3 Query Language Database Application SAK 3408

FROM

Which table(s) are these fields in?

Page 30: Chapter 3 Query Language Database Application SAK 3408

Two Sample Tables

customer tablelast_name state_cd salesTeplow MA 23445.67Abbey CA 6969.96Porter CA 6989.99Martin CA 2345.45Laursen CA 34.34Bambi CA 1234.55McGraw NJ 123.45

state tablestate_name state_cdMassachusetts MACalifornia CANew Jersey NJ

Page 31: Chapter 3 Query Language Database Application SAK 3408

Select - SimplestShow everything in a single table

SELECT *FROM customer;

ReturnsLAST_NAME STATE_CD SALES----------------- -------- ----------------Teplow MA 23445.67Abbey CA 6969.96Porter CA 6989.99Martin CA 2345.45Laursen CA 34.34Bambi CA 1234.55McGraw NJ 123.45

Page 32: Chapter 3 Query Language Database Application SAK 3408

Select Statement - SimpleSELECT last_name, state_cdFROM customer;

ReturnsLAST_NAME STATE_CD ----------------- -------Teplow MA Abbey CA Porter CAMartin CALaursen CABambi CAMcGraw NJ

Page 33: Chapter 3 Query Language Database Application SAK 3408

WHERE - Optional

Use to:

specify how to join two tables

restrict the data returned

Page 34: Chapter 3 Query Language Database Application SAK 3408

SQL OperatorsLogical Operatorsandor

Comparison Operators= equality!= or <> inequalityLike string searchIn list of valuesBetween range of values

Page 35: Chapter 3 Query Language Database Application SAK 3408

Select Statement - EqualsSELECT *FROM customerWHERE state_cd = ‘CA’;

ReturnsLAST_NAME ST SALES----------------- -- ---------Abbey CA 6969.96Porter CA 6989.99Martin CA 2345.45Laursen CA 34.34Bambi CA 1234.55

Page 36: Chapter 3 Query Language Database Application SAK 3408

Select Statement – Not Equals

SELECT *

FROM customer

WHERE state_cd <> ‘CA’;

Returns

LAST_NAME ST SALES-------------- -- ----------

Teplow MA 23445.67

McGraw NJ 123.44

Page 37: Chapter 3 Query Language Database Application SAK 3408

Select Statement - LikeSELECT last_name, state_cd, salesFROM customerWHERE upper(state_cd) LIKE ‘%A’;

ReturnsLAST_NAME ST SALES--------------- -- ----------------Teplow MA 23445.67Abbey CA 6969.96Porter CA 6989.99Martin CA 2345.45Laursen CA 34.34Bambi CA 1234.55

Page 38: Chapter 3 Query Language Database Application SAK 3408

Select Statement - In

SELECT last_name, state_cdFROM customerWHERE state_cd IN (’MA’,’NJ’);

ReturnsLAST_NAME ST------------- --Teplow MA McGraw NJ

Page 39: Chapter 3 Query Language Database Application SAK 3408

Select Statement – Between

SELECT *FROM customerWHERE sales BETWEEN 6500 AND 25000;

ReturnsLAST_NAME ST SALES-------------- -- ----------Teplow MA 23445.67Abbey CA 6969.96Porter CA 6989.99

Page 40: Chapter 3 Query Language Database Application SAK 3408

Select Statement – Multiple Conditions Using OR

SELECT *FROM customerWHERE state_cd <> ‘CA’ OR

sales BETWEEN 6500 AND 25000;

ReturnsLAST_NAME ST SALES-------------- -- ----------Teplow MA 23445.67Abbey CA 6969.96Porter CA 6989.99McGraw NJ 123.44

Page 41: Chapter 3 Query Language Database Application SAK 3408

Select Statement – Multiple Conditions Using AND

SELECT *FROM customerWHERE state_cd <> ‘CA’ AND

sales BETWEEN 6500 AND 25000;

ReturnsLAST_NAME ST SALES-------------- -- ----------Teplow MA 23445.67

Page 42: Chapter 3 Query Language Database Application SAK 3408

Select Statement – Calculated Field

SELECT last_name, sales, sales*05 as TaxFROM customer;

ReturnsLAST_NAME SALES TAX----------- ---------- --------Teplow 23445.67 1172.28Abbey 6969.96 348.50Porter 6989.99 349.50Martin 2345.45 117.27Laursen 34.34 1.71Bambi 1234.55 61.73

Page 43: Chapter 3 Query Language Database Application SAK 3408

ORDER BY - Optional

Used to specify sorting order

Can sort

by multiple fieldsin ascending or descending order

Page 44: Chapter 3 Query Language Database Application SAK 3408

Select Statement - Sortingselect state_name, last_name, sales

from customer, state

where customer.state_cd = state.state_cd and state_cd in (‘CA’,’MA’,’NJ’)

order by state_name;

Returns

STATE_NAME LAST_NAME SALES------------------- ------------------------- --------

California Abbey 6969.96

California Porter 6989.99

Massachusetts Teplow 23445.67

New Jersey McGraw 123.45

Page 45: Chapter 3 Query Language Database Application SAK 3408

Overview

Select Statements continuedTable JoinsDistinctGroup ByHavingDecode functionSequence numbersSubqueries

InsertUpdateDelete

Page 46: Chapter 3 Query Language Database Application SAK 3408

Select - Table Join SELECT state_name, last_name, sales

FROM customer, state

WHERE customer.state_cd = state.state_cd;

Returns

STATE_NAME LAST_NAME SALES-------------- -------------------------

California Abbey 6969.96

California Porter 6989.99

Massachusetts Teplow 23445.67

New Jersey McGraw 123.45

Page 47: Chapter 3 Query Language Database Application SAK 3408

Table Joins

Process of combining data from two or more tables, normally using primary and/or foreign keys.

Basic types of joins:

Equijoin (Equal or Inner Join)

Left join (Outer Join)

Right join

Page 48: Chapter 3 Query Language Database Application SAK 3408

Equi Joins

Most common type of join

Look for records which have matching values of the join fields

Page 49: Chapter 3 Query Language Database Application SAK 3408

Join ProcessingTable joins are done by matching the first

record from the primary table’s first record with each record in the secondary table, then matching the second record in the primary table with each record in the secondary table.

Continue until each record from the primary table has been combined with each record from the secondary table. This is called a Cartesian product.

Page 50: Chapter 3 Query Language Database Application SAK 3408

Join Processing - cont

Oracle then goes through the Cartesian product, discarding combined records that have non-matching join fields.

The remaining records are returned.

Page 51: Chapter 3 Query Language Database Application SAK 3408

Cartesian Product Example

Select ename, emp.deptno dept.deptno, dnameFROM emp, dept

Note: This example is only showing a few fields from the first three employees but all fields and all records are used to create the Cartesian product. The Cartesian product has 56 rows (14 employees * 4 depts)

ename emp.deptno dept.deptno dnameSmith 20 10 AccountingSmith 20 20 ResearchSmith 20 30 SalesSmith 20 40 OperationsAllen 30 10 AccountingAllen 30 20 ResearchAllen 30 30 SalesAllen 30 40 OperationsWard 30 10 AccountingWard 30 20 ResearchWard 30 30 SalesWard 30 40 Operations

Page 52: Chapter 3 Query Language Database Application SAK 3408

Cartesian Product Example cont

ename emp.deptno dept.deptno dname

Smith 20 10 AccountingSmith 20 20 ResearchSmith 20 30 SalesSmith 20 40 OperationsAllen 30 10 AccountingAllen 30 20 ResearchAllen 30 30 SalesAllen 30 40 OperationsWard 30 10 AccountingWard 30 20 ResearchWard 30 30 SalesWard 30 40 Operations

Page 53: Chapter 3 Query Language Database Application SAK 3408

Cartesian Product Example cont.

ename emp.deptno dept.deptno dname

Smith 20 20 Research

Allen 30 30 SalesWard 30 30 Sales

Page 54: Chapter 3 Query Language Database Application SAK 3408

Left Joins

Used when you may not have matching data in the secondary table, but still want to include the data you have in the primary table.

NOTE: The primary table is normally listed on the left side of the equation, the secondary on the right side.

Page 55: Chapter 3 Query Language Database Application SAK 3408

Left Join Example

Create a brand new dept (deptno=50), but there are no employees in that dept yet

Select dname, dept.deptno, emp.deptno, enameFROM dept, empWHERE + dept.deptno = emp.deptno;

dname dept.deptno emp.deptno ename

Research 20 20 SmithSales 30 30 AllenSales 30 30 WardFinance 50

Page 56: Chapter 3 Query Language Database Application SAK 3408

Right Joins

Not as common as left joins. Used when you may have data in the secondary table with no matching data in the primary table.

Page 57: Chapter 3 Query Language Database Application SAK 3408

Right Join ExampleAssign an employee to a brand new

dept, deptno=50, but there’s no record in dept yet

Select ename, dept.deptno deptno, dnameFROM emp, deptWHERE + emp.deptno = dept.deptno;ename emp.deptno dept.deptno dname

Smith 20 20 Research

Allen 30 30 Sales

Ward 30 30 SalesWilson 50

Page 58: Chapter 3 Query Language Database Application SAK 3408

Advanced Select - Distinct

Used to omit duplicate values in a select statement

Select distinct(student_id)From course_sectionWhere term = '1001';

Page 59: Chapter 3 Query Language Database Application SAK 3408

DISTINCT – exampleSELECT Category FROM Animal;

CategoryFishDogFishCatCatDogFishDogBirdDogFishCatDog

SELECT DISTINCT Category FROM Animal;

CategoryBirdCatDogFish

Page 60: Chapter 3 Query Language Database Application SAK 3408

Advanced Select- Group By Clause

Allow you to group data together for summary calculations

avg (column)

count (*)

max (column)

min (column)

sum (column)

Page 61: Chapter 3 Query Language Database Application SAK 3408

Advanced Select- Group By Clause

Group by clause must be included in a select statement that uses a group function (count, sum, min, max, and avg)All fields in the select clause which are not part of a group function must be included in the Group By clause.

Page 62: Chapter 3 Query Language Database Application SAK 3408

Advanced Select- Group By Clause

avg (column)

1 select deptno, avg(sal)2 from scott.emp3* group by deptno;

DEPTNO AVG(SAL) -------------- ---------------- 10 2916.6667 20 2175 30 1566.6667

Page 63: Chapter 3 Query Language Database Application SAK 3408

Advanced Select - Having Clause

Allows you to specify conditions for a record set instead of a single recordMust have a group by clause

1 select deptno, avg(sal)2 from scott.emp3 group by deptno4* having avg(sal) > 2000

DEPTNO AVG(SAL)------------- ---------------- 10 2916.6667 20 2175

Page 64: Chapter 3 Query Language Database Application SAK 3408

Where vs HavingWhere - a constraint on individual records.Having - a constraint on a group of records.They can be used together in the same SQL statement

Select enameFrom empWhere upper(loc) = 'NEW YORK'Group by deptnoHaving avg(sal) > 2000

Page 65: Chapter 3 Query Language Database Application SAK 3408

Subquery Characteristics

Can be used in the SELECT, CREATE, INSERT, UPDATE and DELETE statements

Can be used as part of a condition in the WHERE clause

Can be used in multiple AND or OR predicates of the same SQL statement

Page 66: Chapter 3 Query Language Database Application SAK 3408

Subquery Characteristics - Cont

Is enclosed in parenthesis and must appear on the right in a WHERE clause condition

May or may not retrieve data from a table used in the main, or outer, SQL statement in which it’s embedded.

Cannot include an ORDER BY clause

Page 67: Chapter 3 Query Language Database Application SAK 3408

Subquery Characteristics - Cont

The number of rows returned by the subquery must match the number expected by the main query

The number of columns returned must also match the number expected

Page 68: Chapter 3 Query Language Database Application SAK 3408

Combining Subqueries

Multiple subqueries can be used to check for more than one condition in a statement.

Same or different types can be nested.

NOTE: Nested subqueries are executed from the most deeply nested to the least deeply nested subquery.

Page 69: Chapter 3 Query Language Database Application SAK 3408

Show all customers who have placed an order

SELECT CUSTOMER_NAME FROM CUSTOMER_TWHERE CUSTOMER_ID IN

(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);

Subquery - Example

Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query

Page 70: Chapter 3 Query Language Database Application SAK 3408

Subquery for CalculationWhich cats sold for more than the average sale price of cats?

Assume we know the average price is $170.Usually we need to compute it first.SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePriceFROM AnimalINNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalIDWHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice>170));

SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePriceFROM AnimalINNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalIDWHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice> ( SELECT AVG(SalePrice)

FROM AnimalINNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalIDWHERE (Animal.Category=“Cat”)

) ) );

Page 71: Chapter 3 Query Language Database Application SAK 3408

Using IN with a Sub-query

List all customers who bought items for cats.SELECT Customer.LastName, Customer.FirstName,

SaleItem.ItemIDFROM (Customer

INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID)

INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleIDWHERE (SaleItem.ItemID In

(SELECT ItemID FROM Merchandise WHERE Category="Cat")

);

Page 72: Chapter 3 Query Language Database Application SAK 3408

Which animals have not been sold?Start with list of all animals.Subtract out list of those who were sold.

SELECT Animal.AnimalID, Animal.Name, Animal.CategoryFROM AnimalWHERE (Animal.AnimalID Not In (SELECT AnimalID From SaleAnimal));

Using NOT IN with a Sub-query

Page 73: Chapter 3 Query Language Database Application SAK 3408

Subqueries vs Table Joins

Always use subqueries instead of table joins when possible. Subqueries are significantly less resource intensive.Table joins are only required when the Select clause contains fields from multiple tables.

Page 74: Chapter 3 Query Language Database Application SAK 3408

UNION, INTERSECT, EXCEPT

T1 T2

A B C

SELECT EID, NameFROM EmployeeEastINTERSECTSELECT EID, NameFROM EmployeeWest

List the name of any employee who has worked for both the East and West regions.

Page 75: Chapter 3 Query Language Database Application SAK 3408

UNION Operator

Offices in Los Angeles and New York.Each has an Employee table (East and West).Need to search data from both tables.Columns in the two SELECT lines must match.

SELECT EID, Name, Phone, Salary, ‘East’ AS OfficeFROM EmployeeEastUNIONSELECT EID, Name, Phone, Salary, ‘West’ AS OfficeFROM EmployeeWest

EID Name Phone Salary Office352 Jones 3352 45,000 East876 Inez 8736 47,000 East372 Stoiko 7632 38,000 East

890 Smythe 9803 62,000 West361 Kim 7736 73,000 West

Page 76: Chapter 3 Query Language Database Application SAK 3408

Reflexive Join

Need to connect a table to itself.Common example: Employee(EID, Name, . . ., Manager)

A manager is also an employee.Use a second copy of the table and an alias.

SELECT Employee.EID, Employee.Name, Employee.Manager, E2.Name

FROM Employee INNER JOIN Employee AS E2

ON Employee.Manager = E2.EID

EID Name . . . Manager115 Sanchez 765462 Miller 115523 Hawk 115765 Munoz 886

Employee

EID Name Manager Name115 Sanchez 765 Munoz462 Miller 115 Sanchez523 Hawk 115 Sanchez

SQL

Result

Page 77: Chapter 3 Query Language Database Application SAK 3408

CASE Function

Used to change data to a different context.Example: Define age categories for the animals.

Less than 3 monthsBetween 3 months and 9 monthsBetween 9 months and 1 yearOver 1 year

Select AnimalID,CASE

WHEN Date()-DateBorn < 90 Then “Baby”WHEN Date()-DateBorn >= 90 AND Date()-DateBorn < 270 Then “Young”WHEN Date()-DateBorn >= 270 AND Date()-DateBorn < 365 Then “Grown”ELSE “Experienced”

ENDFROM Animal;

Not available in Microsoft Access. It is in SQL Server and Oracle.

Page 78: Chapter 3 Query Language Database Application SAK 3408

Inequality Join

AR(TransactionID, CustomerID, Amount, DateDue)

AccountsReceivableCategorize by Days Late

30, 90, 120+Three queries?New table for business rules

LateCategory(Category, MinDays, MaxDays, Charge, …)

Month 30 90 3%Quarter 90 120 5%Overdue 120 9999 10%

SELECT *FROM AR INNER JOIN LateCategory ON ((Date() - AR.DateDue) >= LateCategory.MinDays) AND ((Date() - AR.DateDue) < LateCategory.MaxDays)