; SUMMARY      Unit test
;
; DESCRIPTION  This is one of the unit tests
;
; STATUS       These tests have been properly validated.
;
; KEYWORDS     unit test acceptance
;

; <COMMON>
connect star
set qualify_counts noparent
;
;  Sampling of tests for 3.5 features.
;
;
; <SECTION=COUNT()>
;
;Tests optimized count()
;
; <TEST=C1>
;1
select count(*) from products
; <TEST=C2>
;2
select count(sales_tax) from orders
; <TEST=C3>
;3
select count(*) from activity \
  where initials = 'EFR' or tickler in ('MF', 'PC', 'RC')
; Test reuse
; <TEST=C3r>
;4
select count(*) from activity \
  where initials = 'EFR' or tickler in ('MF', 'PC', 'RC')
; <TEST=C4>
;5
select count(p.product_no) \
  from orders o, products p \
  where o.product_no = p.product_no and \
        (o.quantity >= 10 or p.wholesale > 1000.00)
; <TEST=C5>
;6
select count(*) from orders group by acct
;
;
;Tests un-optimized count()
;
; <TEST=C6>
;7
select count(*) from pmt_methods
; <TEST=C7>
;8
select count(mailflag) from prospects
; <TEST=C8>
;9
select count(p.product_no), count(o.acct) \
  from orders o join products p using (product_no) \
  where (o.quantity >= 10 or p.wholesale > 1000.00)
; Test reuse
; <TEST=C8r>
;10
select -1 * (count(*) + 5) from activity \
  where tickler like 'C%'
; <TEST=C9>
;11
select -1 * (count(*) + 5) from activity \
  where tickler like 'C%'
; <TEST=C10>
;12
select 'counted ', count(quantity) from orders \
  where order_date > '01-01-1996' or quantity between 1 and 5 \
        or sales_tax < 4.00
;
;
; <SECTION=COUNT(DISTINCT)>
;
;Tests optimized count(distinct col)
;
; <TEST=CD1>
;13
select count(distinct cost) from products
; <TEST=CD2>
;14
select count(distinct order_date) from orders \
  where order_date > '01-01-1996' or quantity between 1 and 5 \
        or sales_tax < 4.00
; Test reuse
; <TEST=CD2r>
;15
select count(distinct order_date) from orders \
  where order_date > '01-01-1996' or quantity between 1 and 5 \
        or sales_tax < 4.00
; <TEST=CD3>
;16
select count(distinct profession), '1' from prospects \
  where cr_limit > 0
; <TEST=CD4>
;17
select division, dept, count(distinct cost) from products \
  group by division, dept
; <TEST=CD5>
;18
select count(distinct order_date) from orders \
  where order_date > '01-01-1996' or quantity between 1 and 5 \
        or sales_tax < 4.00 \
  group by acct
; <TEST=CD6>
;19
select count(distinct total) from orders \
  group by order_date, discount having count(distinct total) > 1
; Test reuse
; <TEST=CD6r>
;20
select count(distinct total) from orders \
  group by order_date, discount having count(distinct total) > 1
; <TEST=CD7>
; permuted sort>
;21
select every 5 discount, order_date, count(distinct total) from orders \
  where tax_state in ('CO', 'CA', 'AZ') and \
        (discount >= 10 or (quantity < 100 and amount > 100)) \
  group by discount, order_date having count(distinct total) > 1 \
  order by 1, 2, 3
;
;
;Tests un-optimized count(distinct col)
;
; <TEST=CD8>
;22
select count(distinct dt) from activity
; <TEST=CD9>
;23
select count(distinct zip) from prospects \
  where cust_since <= 1992 and last_order >= '07-31-1998'
; <TEST=CD10>
; DB2 aborts
; <COMPARE='DBMS="NOT DB2"'>
;24
select (1.0 / count(distinct acct)) * 100 from orders
; <END_COMPARE>
;
;Multiple counts per statement
; <TEST=MC1>
;25
select count(*), count(distinct acct), count(distinct discount_rt) from prospects
; <TEST=MC2>
;26
select count(tax_state), count(distinct source) from orders
; <TEST=MC3>
;27
select count(distinct o.acct), count(o.amount), count(distinct s.description) \
  from orders o join statuses s using (status) \
  where o.pmt_method <= 2 and o.total >= 2000.00 \
        and s.description = 'Shipped' and o.source = 6
; <TEST=MC4>
;28
select count(distinct status), count(*), count(distinct status), \
  sum(distinct discount) \
  from orders \
  where ((order_date between '01-01-1996' and '12-31-1996' \
  and quantity > 10) or \
  (order_date between '01-01-1995'and '12-31-1995' \
  and (quantity > 10 or total > 10000))) and tax_state like 'CO'
; <TEST=MC5>
;29
select count(f.description), count(distinct f.description), max(p.last_order), \
  count(s.description), min(distinct income) \
  from prospects p, professions f, states s, zipcodes z \
  where (p.profession = f.profession and p.state = s.state \
        and p.zip = z.zip) and \
        ((p.state in ('KS', 'WA', 'CA', 'NE', 'OR', 'CO', 'UT', 'IL') \
        and (s.region = 'MW' or s.region = 'SW')) or (p.cust_since > 1995 \
        and (((f.description = 'Legal' or f.description = 'Finance') \
        and income < 7) or \
        (f.description in ('Educator', 'Clerical/Administrative', 'Retired') \
        and income > 7))))
; <TEST=MC6>
;30
select sum(total), avg(amount), count(1), min(quantity), avg(total), count(*) \
  from orders where discount != 0 \
  group by acct order by 4, 1 with ask_retrieval_threshold = 10
; <TEST=MC7>
;31
select count(distinct margin), count(distinct cost) from products \
  where category > 10
; <TEST=MC8>
;32
select count(distinct profession), count(distinct cust_since), \
       count(distinct last_order) from prospects
; <TEST=MC9>
;33
select count(distinct quantity), count(distinct sales_tax), \
  count(distinct amount), count(distinct acct) from orders
; <TEST=MC10>
;34
select count(distinct total), count(distinct product_no), count(distinct quantity), \
  count(distinct order_date) from orders \
  where tax_state in ('CO', 'CA', 'AZ') and \
        (discount >= 10 or (quantity < 100 and amount > 100)) \
  group by acct
;
;
; <SECTION=KEYWD_OPTIMIZATION>
;
;Tests use of KEYWD option (dist ;np col; group by col; count() group by col)
; order by is supported ONLY if the order column is the select column
;
; KEYWD
; <TEST=K1>
;  NP c string>
;35
select distinct zip from prospects
; <TEST=K2>
; odx date
;36
select top 25% distinct dt from activity
; Test reuse
; <TEST=K2r>
;37
select top 25% distinct dt from activity
; <TEST=K3>
;  int 1
;38
select distinct category from products order by category
; KWCOUNT
; <TEST=K4>
;  float
;39
select count(*) from orders group by sales_tax
; <TEST=K5>
;
;40
select category, count(*) from products group by category
; KWPCOUNT
; <TEST=K6>
;  int 4 si
;41
select count(distinct acct) from orders group by source
; <TEST=K7>
;  odx date si
;42
select quantity, count(order_date), count(distinct order_date) from orders \
 group by quantity
; Test reuse
; <TEST=K7r>
;43
select quantity, count(order_date), count(distinct order_date) from orders \
 group by quantity
;Make sure we don't try on non-NP colums
; <TEST=K8>
;44
select distinct city from prospects
; <TEST=K9>
;45
select city, count(*) from prospects group by city
; <TEST=K10>
;46
select tax_state, count(distinct acct) from orders group by tax_state
;
;
; <SECTION=DISTINCT(COL-LIST)>
;
;Tests optimized "distinct imsam-col(s) order by imsam-col(s)"
; uses agg index; only works if all the select items are order items
; and order by is same order as components
;
; <TEST=DO1>
;47
select distinct profession, income from prospects order by 1, 2
; <TEST=DO2>
;48
select top 10 distinct total, status from orders order by 2, 1
; Test reuse
; <TEST=DO2r>
;49
select top 10 distinct total, status from orders order by 2, 1
; <TEST=DO3>
;50
select distinct order_date, discount, quantity, total from orders \
 order by 1,2,3,4
;
;Tests distinct col-list (un-optimized)
;
; <TEST=DC1>
;51
select distinct * from prospects
; <TEST=DC2>
;52
select distinct discount_rt from prospects
; Test reuse
; <TEST=DC2r>
;53
select distinct discount_rt from prospects
; <TEST=DC3>
;54
select distinct tickler, 0, initials from activity \
 where initials = 'EFR' or tickler in ('MF', 'PC', 'RC')
; <TEST=DC4>
;55
select distinct division, dept from products \
 where margin > 30 \
 order by 2,1
; expressions
; <TEST=DE1>
;56
select distinct cost * 100 from products
; <TEST=DE2>
;57
select distinct discount / 100.0 from orders \
 where (tax_state in ('CO', 'CA', 'AZ') and \
       discount >= 10) or (quantity < 100 and amount > 100)
; Test reuse
; <TEST=DE2r>
;58
select distinct discount / 100.0 from orders \
 where (tax_state in ('CO', 'CA', 'AZ') and \
       discount >= 10) or (quantity < 100 and amount > 100)
; outside the aggregation
; <TEST=DE3>
;59
select distinct region, avg(discount_rt) / 100.0 from prospects \
 group by region \
having REGION not in ('MW','SW') and avg(discount_rt) / 100.0 <> 0.0
; <TEST=DE4>
;60
select distinct sum(total) as sumtotal from orders \
 group by acct \
having sumtotal < 100.00
; <TEST=DE5>
;61
select distinct min(discount_rt) from prospects \
 where cust_since <= 1992 and last_order >= '07-31-1998' \
 order by 1
;
; multiple tables
; <TEST=DMT1>
;62
select distinct contact, tickler, initials from activity a join prospects p \
  using (acct)
; <TEST=DMT2>
;63
select distinct contact, source from prospects p, orders o \
 where o.acct = p.acct and \
       p.last_order <= '12-31-1996' and \
       ((p.region = 'MW' or o.tax_state in ('CO', 'KS', 'NE', 'IL')) \
        and (o.total >= 1000.00 or o.quantity > 100)) \
 order by contact
; <TEST=DMT3>
;64
select top 3 distinct cost * quantity, total  from products p, orders o \
 where p.product_no = o.product_no and \
       (o.quantity >= 10 or p.wholesale > 1000.00) \
 order by total
; <TEST=DMT4>
;65
select distinct category, avg(wholesale), avg(total) from products p, orders o \
 where p.product_no = o.product_no and \
       (o.quantity >= 10 or p.wholesale > 1000.00) \
 group by category
; <TEST=DMT5>
;66
select distinct sum(p.discount_rt/100.0 * o.amount) from orders o join prospects p \
 on p.acct = o.acct \
having sum(p.discount_rt/100.0 * o.amount) between 1000.00 and 10000.00
; Test reuse
; <TEST=DMT5r>
;67
select distinct sum(p.discount_rt/100.0 * o.amount) from orders o, prospects p \
 where p.acct = o.acct \
having sum(p.discount_rt/100.0 * o.amount) between 1000.00 and 10000.00
;
;
; <SECTION=AGG(DISTINCT)>
;
;Tests sum/avg(distinct col)
;
; <TEST=AGD1>
;68
select avg(distinct cr_limit), count(distinct cr_limit) from prospects
; <TEST=AGD2>
;69
select sum(distinct quantity) from orders \
 where amount < 10 \
 group by pmt_method
; Test reuse
; <TEST=AGD2r>
;70
select sum(distinct quantity) from orders \
 where amount < 10 \
 group by pmt_method
; <TEST=AGD3>
;71
select 100 * avg(distinct total) bigtotal, pmt_method, source \
  from orders \
 group by source, pmt_method \
having bigtotal between 10000 and 1000000.00
; <TEST=AGD4>
;72
select avg(cost) - avg(distinct wholesale) neg from products \
 where (description like '%Computer%' or category < 30) and wholesale > 100 \
 group by category \
having neg > -10.00
; <TEST=AGD5>
;73
select sum(distinct cr_limit * 1000) * (1 - avg(distinct discount_rt / 100.0)) \
  from prospects where cust_since <= 1992 and last_order >= '07-31-1998'
; <TEST=AGD6>
;74
select (discount/100.0) * sum(total), quantity * sum(distinct amount) \
  from orders \
 group by discount, quantity
; <TEST=AGD7>
;75
select quantity * avg(amount) - quantity * avg(distinct amount), \
       count(amount), count(distinct amount) \
  from orders where amount < 10 \
 group by quantity, pmt_method
; <TEST=AGD8>
;76
select sum(distinct amount - ((discount/100.0) * amount)) + avg(total) / quantity \
  from orders \
 where amount < 100.00 or quantity between 5 and 10 \
 group by status, quantity
; <TEST=AGD9>
;77
select $round(quantity * sum(distinct amount - ((discount/100.0) * amount)) \
       - avg(distinct total) / quantity, 2), count(total), count(*), \
       status, quantity, acct \
  from orders \
 group by status, quantity, acct
; <TEST=AGD10>
;78
select quantity * sum(distinct amount - ((discount/100.0) * amount)) \
       - avg(distinct total) / quantity, count(total), count(*), \
       status, quantity, acct \
  from orders \
 where amount < 100.00 or quantity between 5 and 10 \
 group by status, quantity, acct \
having count(*) > 100 \
 order by 6,5,4,3,2,1
; Test reuse
; <TEST=AGD10r>
;79
select quantity * sum(distinct amount - ((discount/100.0) * amount)) \
       - avg(distinct total) / quantity, count(total), count(*), \
       status, quantity, acct \
  from orders \
 where amount < 100.00 or quantity between 5 and 10 \
 group by status, quantity, acct \
having count(*) > 100 \
 order by 6,5,4,3,2,1
; <TEST=AGD11>
;80
select quantity * sum(distinct amount - ((discount/100.0) * amount)) \
       - avg(distinct total) / quantity, count(total), count(*), \
       status, quantity, acct \
  from orders \
 where amount < 100.00 or quantity between 5 and 10 \
 group by status, quantity, acct \
 order by 6,5,4,3,2,1
;
; multiple tables
; <TEST=AGDMT1>
;81
select sum(distinct quantity + 1000), avg(distinct quantity *100), \
       0.5 * sum(distinct cr_limit), avg(distinct cr_limit) \
  from orders o, prospects p \
 where o.acct = p.acct and \
       (order_date > '01-01-1996' or quantity between 1 and 5 \
       or sales_tax < 4.00) and company like '%Systems%' \
 group by cr_limit \
having sum(cr_limit) < 10
; Test reuse
; <TEST=AGDMT1r>
;82
select sum(distinct quantity + 1000), avg(distinct quantity *100), \
       0.5 * sum(distinct cr_limit), avg(distinct cr_limit) \
  from orders o, prospects p \
 where o.acct = p.acct and \
       (order_date > '01-01-1996' or quantity between 1 and 5 \
       or sales_tax < 4.00) and company like '%Systems%' \
 group by cr_limit \
having sum(cr_limit) < 10
; <TEST=AGDMT2>
;83
select sum(distinct cr_limit) * (discount / 100.0), \
       $trunc(quantity * sum(distinct wholesale - ((discount/100.0) * amount)), 2), \
       - avg(distinct total) / quantity, count(total), count(*) \
  from orders, prospects, products \
 where orders.acct = prospects.acct and \
       products.product_no = orders.product_no \
 group by quantity, discount \
 order by 2
;
;
; <SECTION=WHERE-CLAUSE-OPT(no_aggs)>
;
;Tests where-clause optimization (without aggregations)
;
; <TEST=WO1>
;84
select p.product_no \
  from orders o, products p \
  where o.product_no = p.product_no and \
        (o.quantity >= 10 or p.wholesale > 1000.00)
; <TEST=WO2>
;85
select top 5% acct, product_no, order_date from orders \
  where discount = 0 or quantity <= 5 or total < 22.5 \
  order by 1, 2, 3
; <TEST=WO3>
;86
select acct, initials, dt from activity \
  where (initials not like 'SH%' and initials not like '%J%') or tickler in ('MF', 'PC', 'RC')
; Test reuse
; <TEST=WO3r>
;87
select acct, initials, dt from activity \
  where (initials not like 'SH%' and initials not like '%J%') or tickler in ('MF', 'PC', 'RC')
; <TEST=WO4>
;88
select product_no from products \
  where description like '%Computer' or (category < 30 and wholesale > 100)
; <TEST=WO5>
;89
select order_date, ((amount * quantity) - total) from orders \
  where ((order_date between '01-01-1996' and '12-31-1996' and quantity > 10) or \
        (order_date between '01-01-1995' and '12-31-1995' and (quantity > 10 or total > 10000))) \
        and tax_state = 'CO'
; <TEST=WO6>
;90
select d.description, p.description, s.description \
  from orders o, products p, dates d, statuses s \
 where (o.product_no = p.product_no and o.order_date = d.dt \
        and o.status = s.status) and \
        (s.description = 'Cancelled' or d.yr < 1991 \
        or o.discount = 0 or p.margin <= 30) order by d.dt, p.description
; <TEST=WO7>
;91
select a.initials, t.description from activity a join ticklers t \
  on (a.tickler = t.tickler) \
  where a.dt >= '01-01-1998' and t.description like '%Catalog'
; <TEST=WO8>
;92
select contact, f.description, m.description \
  from prospects p, mailings m, mailflags f \
 where (p.mailing = m.mailing and p.mailflag = f.mailflag) and \
        m.description = 'All' and f.description like '%catalogs%' \
        and f.description not like '%flyers%' \
 order by p.acct, m.mailing desc
; <TEST=WO9>
;93
select tax_state, region, last_order \
  from orders o, prospects p \
  where p.acct = o.acct and \
        p.last_order <= '12-31-1996' and \
        ((p.region = 'MW' or o.tax_state in ('CO', 'KS', 'NE', 'IL')) \
         and (o.total >= 1000.00 or o.quantity > 100)) \
  order by region desc, tax_state
; Test reuse
; <TEST=WO9r>
;94
select tax_state, region, last_order \
  from orders o, prospects p \
  where p.acct = o.acct and \
        p.last_order <= '12-31-1996' and \
        ((p.region = 'MW' or o.tax_state in ('CO', 'KS', 'NE', 'IL')) \
         and (o.total >= 1000.00 or o.quantity > 100)) \
  order by region desc, tax_state
;
;
; <SECTION=WHERE-CLAUSE-OPT-$ODXID>
;
; Tests where-clause optimization following an explicit qualify
;
; <TEST=QWO1>
;95
qualify prospects where company = 'systems'
select company from prospects where cust_since = '1998' with odxid
; try a distinct
; <TEST=QWO2>
;96
qualify prospects where company = 'systems'
select distinct region from prospects where cust_since = '1998' with odxid
;<TEST=QWO3>
;97
qualify activity where initials <> 'SHIP'
select min(dt) from activity where tickler = 'RC' with odxid
; Test reuse
; <TEST=QWO3r>
;98
qualify activity where initials <> 'SHIP'
select min(dt) from activity where tickler = 'RC' with odxid
; try an ims agg
; <TEST=QWO4>
;99
qualify orders where discount > 10
select top 10% product_no, sum(amount), count(*), avg(quantity) from orders \
 where discount > 0 \
 group by product_no \
  with odxid
; try an ims qualify
; <TEST=QWO5>
;100
qualify orders where pt = 'H*' with ask
select distinct acct, total from orders where tax_state = 'TX' with odxid
; try an 'or'
; <TEST=QWO6>
;101
qualify orders where discount > 10
select product_no from orders \
 where $rowid in $odxid or \
       (order_date between '02-14-1996' and '12-07-1996' and \
        not (amount < 100))
;
;
; <SECTION=HAVING>
; (in addition to those tested above)
;
; <TEST=H1>
;102
select count(*) from orders group by sales_tax having count(*) > 1
;
; non-optimized
; <TEST=H2>
;103
select min(cust_since), last_order, contact from prospects \
 where (contact not like 'Clint%') \
 group by last_order, contact having min(cust_since) >= 1997
;
; not in select list
; <TEST=H3>
;104
select max(cust_since), contact from prospects \
where (contact not like 'Clint%') \
group by contact having min(cust_since) >= 1997
;
; multiple-tbl
; <TEST=H4>
;105
select dt, prospects.acct, sum(prospects.cr_limit) \
  from prospects, activity \
 where prospects.acct = activity.acct \
 group by dt, prospects.acct \
having activity.dt > '01-01-1991' and prospects.acct < 10 \
       and min(cr_limit) > 0
;
; grp col optimized
; <TEST=H5>
;106
select every 10 dt, prospects.acct, sum(prospects.cr_limit) \
  from prospects, activity \
 where prospects.acct = activity.acct \
 group by dt, prospects.acct \
having activity.dt > '01-01-1991' and prospects.acct < 10 \
order by 1, 2, 3
; Test reuse
; <TEST=H5r>
;107
select every 10 dt, prospects.acct, sum(prospects.cr_limit) \
  from prospects, activity \
 where prospects.acct = activity.acct \
 group by dt, prospects.acct \
having activity.dt > '01-01-1991' and prospects.acct < 10 \
order by 1, 2, 3
;
; mst-optimized
; <TEST=H6>
;108
select min(quantity) from orders \
       having min(quantity) = 1
; <TEST=H7>
;109
select acct, avg(amount) from orders \
       group by acct \
       having avg(amount) <= 1000 and avg(amount) >= 10
;
; "having" not in sel list
; <TEST=H8>
;110
select discount, sum(total) \
  from orders \
 where pmt_method <= 5 \
 group by discount \
having avg(quantity) > 10
;
; mix, complex
; <TEST=H9>
;111
select discount, avg(total) * count(*), min(quantity) mq, max(sales_tax) ms \
  from orders \
 group by discount \
having (sum(amount * quantity) > 100.00 or count(*) < 10) and \
       ms / mq < 100.0
; Test reuse
; <TEST=H9r>
;112
select discount, avg(total) * count(*), min(quantity) mq, max(sales_tax) ms \
  from orders \
 group by discount \
having (sum(amount * quantity) > 100.00 or count(*) < 10) and \
       ms / mq < 100.0
; <TEST=H10>
;113
select count(quantity), 100 * $round(avg(total * 0.90), 1), max(wholesale) \
  from orders o, products p \
 where o.product_no = p.product_no and \
        (o.amount < 100 or o.quantity between 5 and 10 \
        or p.description like '%Computer') \
 group by category \
having count(quantity) < 100 or \
       sum(wholesale * quantity) >= sum(amount * quantity) or \
       category not in (1,2,3,8,9,10,21,22,23)
;
; on date
; <TEST=H11>
;114
select acct, min(order_date) from orders where pmt_method >= 5 \
 group by acct \
having max(order_date) = '10-20-1996'
;
;
;
; <SECTION=ORDER_BY>
; (in addition to those tested above)
;
; <TEST=O1>
;115
select acct from prospects order by acct desc
; <TEST=O2>
;116
select quantity from orders \
  where discount >= 20 and total > 1000 and not quantity < 15 \
  order by quantity desc
; <TEST=O3>
;117
select quantity from orders \
  where discount >= 20 and total > 1000 and not quantity < 15 \
  order by order_date desc
; <TEST=O4>
;118
select pmt_method, discount, quantity, amount from orders \
  where total > 1000 \
 order by 2 desc, 1, 4 desc
; Test reuse
; <TEST=O4r>
;119
select pmt_method, discount, quantity, amount from orders \
  where total > 1000 \
 order by 2 desc, 1, 4 desc
; <TEST=O5>
;120
select top 10 * from activity \
  where dt >= '01-01-1996' and initials != 'EFR' \
  order by initials desc, tickler desc, dt, acct
; <TEST=O6>
;121
select product_no, acct, min(total) from orders \
  group by acct, product_no \
  order by product_no, acct
; see 'where clause' tests for order-by testing on multiple tables
; Test reuse
; <TEST=O6r>
;122
select product_no, acct, min(total) from orders \
  group by acct, product_no \
  order by product_no, acct
;
;
; Test 'order by' optimized by imsam
;
; <TEST=OI1>
;123
select last_order from prospects \
       where last_order > '01-01-1990' \
       order by last_order
; <TEST=OI2>
;124
select profession from prospects \
       where profession > 1 \
       order by profession
;
;
;
; <SECTION=STARSCHEMA_SCENARIOS>
; (may or may not use starschema opt)
;
; <TEST=SS1>
;125
select p.contact, sum(o.total) from prospects p, orders o \
       where o.acct = p.acct \
       group by p.contact
; <TEST=SS2>
;126
select r.description, avg(o.amount), min(o.total) from products r, orders o \
       where r.product_no = o.product_no and \
        (o.quantity >= 10 and r.wholesale > 1000.00) \
       group by r.description
; Test reuse
; <TEST=SS2r>
;127
select r.description, avg(o.amount), min(o.total) from products r, orders o \
       where r.product_no = o.product_no and \
        (o.quantity >= 10 and r.wholesale > 1000.00) \
       group by r.description
; <TEST=SS3>
;128
select p.company, r.division, count(o.amount), sum(o.amount) \
       from orders o, prospects p, products r \
       where p.acct = o.acct and o.product_no = r.product_no \
       group by r.division, p.company \
       having count(o.amount) > 10 and sum(o.amount) > 500.00
; <TEST=SS4>
;129
select avg(amount), avg(total), count(*) \
       from orders o, dates d, products r, prospects p \
       where o.order_date = d.dt and o.product_no = r.product_no and \
             p.acct = o.acct and \
       p.discount_rt != 0 and (r.description like '%Computer%' or quantity > 50) \
       group by d.yr, r.category \
       having count(*) > 2.0  \
       order by 3 desc, 1 asc
; <TEST=SS5>
;130
select o.status, r.dept, avg(o.amount), avg(o.quantity * 1.0) \
       from prospects p, orders o, products r \
       where o.acct = p.acct and o.product_no = r.product_no and  \
       p.state = 'CO' and o.total > 1000.00 and o.source between 1 and 4 \
       and r.cost between 1000.00 and 50000.00 \
       group by o.status, r.dept
;
;
; <SECTION=SNOWFLAKE_SCENARIOS>
;
;no special optimization performed (besides where/imslink)
; <TEST=SF1>
;131
select d.yr, ca.description, avg(amount), avg(total) \
       from orders o, dates d, products r, categories ca \
       where o.order_date = d.dt and o.product_no = r.product_no and  \
       ca.category = r.category and \
       discount != 0 and (r.description like '%Computer%' or quantity > 50) \
       group by d.yr, ca.description \
       having count(*) > 2.0  \
       order by 3 desc, 1 asc
; <TEST=SF2>
;132
select sum(o.total), min(o.order_date) \
  from orders o, products p, divisions d, depts e \
  where (p.division = d.division and p.dept = e.dept and o.product_no = p.product_no) and \
        (d.description = 'Furniture' or e.description in ('Desks', 'Chairs'))
; <TEST=SF3>
;133
select s.description, di.description, max(o.discount), \
        count(distinct o.discount) numdiscounts \
  from orders o, products r, dates d, statuses s, divisions di \
  where (o.product_no = r.product_no and o.order_date = d.dt \
        and o.status = s.status and r.division = di.division) and \
        (s.description = 'Cancelled' or d.yr < 1991 \
        or o.discount = 0 or r.margin <= 30) \
        group by s.description, di.description \
        having numdiscounts > 1 order by 2, 1 desc
; <TEST=SF4>
;134
select d.description, $round(sum(o.amount * r.wholesale) * 100) \
  from orders o, products r, divisions d \
  where r.division = d.division and o.product_no = r.product_no \
  group by d.description
; <TEST=SF5>
;135
select g.description, d.description, avg(o.total) \
  from orders o, products r, depts d, prospects p, genders g \
  where r.dept = d.dept and o.product_no = r.product_no and \
       p.acct = o.acct and p.gender = g.gender and \
       d.description in ('Computers', 'Printers', 'Fax Machines', 'Telephones') \
  group by g.description, d.description
;
;
; <SECTION=SELECT_INTO>
;
;TABLE TYPE1 - checking for select into, select from the table created.
;The drop is performed after the pseudocolumns test.
;
;select into a temp table
; <TEST=SI1>
;136
select top 15 p.acct, o.acct as o_acct, sum(o.acct) as sum_acct, o.product_no, o.total, o.tax_state \
   into op_temp \
   from orders o join prospects p using (acct) \
   group by p.acct, o_acct, o.product_no, o.total, o.tax_state \
   order by o.product_no desc
;
;select from it
; <TEST=SI2>
;137
select o_acct, sum_acct \
   from op_temp \
   where tax_state not in ('CA') \
   order by 2
;
;TABLE TYPE2 - checking for select into, select from.
;The drop is performed after the pseudocolumns test, so that the table will be released.
;
;select into a $tempdb table
; <TEST=SI3>
;138
select distinct p.*, o.* \
   into $tempdb.op_temp2 physical "op_temp2.dat" \
   from orders o join prospects p on o.acct = p.acct \
   where o.total > 10.00 or o.amount < 500.00
;
;select from it
; <TEST=SI4>
;139
select distinct op.acct1 from $tempdb.op_temp2 op \
   join products p on op.product_no = p.product_no \
   where p.product_no > 'D'
;
; <SECTION=DROP_TABLES>
;
;Drop tables from the select into's above
; <TEST=DT1>
;140
drop table $tempdb.op_temp2
; <TEST=DT2>
;141
drop table op_temp
;
; <SECTION=PSEUDOCOLUMNS>
; <TEST=PC1>
;142
select count(*), avg(distinct p.discount_rt) \
  from products p join orders o using (product_no) \
  where p.discount_rt > 70 or o.acct > 2 \
  group by p.discount_rt \
  order by 2
;
; <SECTION=FUNCTIONS>
; <TEST=F1>
;144
select company, $soundex(company) sndx, $mod(income, acct) \
   from prospects \
   where $soundex(contact) not in ($soundex('John Moore'), $soundex('Mary Kate Rosak')) \
   order by sndx
;
; <TEST=F2>
;145
select *, $current_row from mfrs \
  where $soundex(description) = $soundex('Smeed') or \
        $soundex(description) = $soundex('Xerox') \
  order by mfr
;
;
; <SECTION=PROBLEM_QUERIES>
;   The next 3 are problems that pop up from time to time:
; <TEST=PRBQ1>
;146
select acct, order_date, quantity, total from orders \
  where ((order_date between '01-01-1996' and '12-31-1996' and quantity > 10) or \
  (order_date between '01-01-1995'and '12-31-1995' \
  and (quantity > 200 or total > 1000000))) and tax_state = 'CO'
; <TEST=PRBQ2>
;147
select count(*) from activity a, prospects p \
  where a.acct = p.acct and (p.company like '%Systems%')
; <TEST=PRBQ3>
;148
select count(a.dt) from orders o, activity a \
  where o.acct = a.acct and \
  (a.dt between '07-31-1997' and '12-31-1997' \
   or o.order_date between '07-31-1997' and '12-31-1997')
;
;   Double-check tidmap works (for oracle)
;
; <TEST=PRBQ4>
;149
select yr, jan_total from orders_rollup where num_orders between 20 and 40
; <TEST=PRBQ5>
;150
select jan_total, retail, yr from orders_rollup where jan_total > 0
;
;   Check date aggs for y2k
;
; <TEST=PRBQ6>
;151
select count(distinct dt) from dates group by mo
; <TEST=PRBQ7>
;152
select min(dt), max(dt) from dates \
  where yr between 1993 and 2003
;
;   Check required_optimization, ask_retrieval_threshold
; <SECTION=REQ_OPT>
; <TEST=RO1>
;153
select top 5 acct, quantity, total from orders order by 1,2,3 \
  with required = 'no_unoptimized_criteria \
   no_sequential_table_join no_xref_files \
   no_unoptimized_aggregation no_cartesian_products'
;
; <TEST=RO2>
;154  /* should fail except on nonopt */
select O.ACCT, A.ACCT \
  from  ORDERS O, ACTIVITY A, PROSPECTS P, INITIALS I \
  where O.ACCT = P.ACCT and A.ACCT = P.ACCT and A.INITIALS = I.INITIALS and \
        O.ACCT = 3 and A.ACCT = 3 \
  with required='no_xref_files'
;
; <TEST=RO3>
;155
select count(distinct total), count(distinct product_no), count(distinct quantity), \
  count(distinct order_date) from orders \
  where tax_state in ('CO', 'CA', 'AZ') and \
        (discount < 10 or (quantity > 15 and amount > 100)) \
  group by acct with ask_retrieval_threshold = 20
;
;  'Kellogg' optimization:  all select items come from child; not all parents linked
; <SECTION=MULTIFIND>
;
;2 parents, neither linked
; <TEST=MF1>
;156
select         sum(amount), sum(total), min(quantity) \
  from         pmt_methods pmt, \
               states sts, \
               orders ord \
  where        pmt.pmt_method = ord.pmt_method and \
               sts.state = ord.tax_state and \
               pmt.description in ('Cash', 'Check') and \
               sts.description = 'Texas'
;2 parents, one linked
; <TEST=MF2>
;157
select         count(amount) \
  from         pmt_methods pmt, \
               products prd, \
               orders ord \
  where        pmt.pmt_method = ord.pmt_method and \
               ord.product_no = prd.product_no and \
               pmt.description in ('Cash', 'Check') and \
               prd.margin < 50.0 \
  group by     acct
;3 parents, 1 linked
; <TEST=MF3>
;158
select         order_date, sum(amount), sum(total), min(quantity), count(*) \
  from         pmt_methods pmt, \
               states sts, \
               products prd, \
               orders ord \
  where        pmt.pmt_method = ord.pmt_method and \
               sts.state = ord.tax_state and \
               prd.product_no = ord.product_no and \
               pmt.description in ('Cash', 'Check') and \
               prd.category > 30 and \
               sts.description = 'Texas' \
  group by     ord.order_date
;4 parents, 2 linked, and qualify in child
; <TEST=MF4>
;159
select         sum(amount), sum(total), min(quantity), count(*) \
  from         prospects prs, \
               pmt_methods pmt, \
               states sts, \
               products prd, \
               orders ord \
  where        prs.acct = ord.acct and \
               pmt.pmt_method = ord.pmt_method and \
               sts.state = ord.tax_state and \
               prd.product_no = ord.product_no and \
               prs.company like '%Systems%' and \
               pmt.description in ('Cash', 'Check') and \
               prd.category > 50 and \
               sts.description = 'Colorado' and \
               ord.quantity > 5
;same as 156 but count(*) only
; <TEST=MF5>
;160
select         count(*) \
  from         pmt_methods pmt, \
               states sts, \
               orders ord \
  where        pmt.pmt_method = ord.pmt_method and \
               sts.state = ord.tax_state and \
               pmt.description in ('Cash', 'Check') and \
               sts.description = 'Texas'
;
; long in-clauses turned into multifind
; <TEST=MF6>
;161
select sum(total) from orders o, products p where o.product_no = p.product_no  \
and (category in (11,12,13,14,16,18,20,21,22,23,24,25,26,30,31,40,41,42,43,50,51,52,53,60,61,62,80,81,90,91,92,93,94,95) \
or   description in ( 'Desk', 'Chair', 'File', 'Phone', 'Credenza', 'Computer', 'Copier', 'Printer', 'Deskjet', 'Laserjet', 'Cellular', 'Cordless', 'Fax', 'Paper', 'Pens', 'Pencils', 'Markers', 'Erasers' ) ) \
and (order_date in ('01-20-1994','02-20-1994','03-20-1994','04-20-1994','05-20-1994','06-20-1994','07-20-1994','08-20-1994','09-20-1994','10-20-1994','11-20-1994','12-20-1994','01-20-1995','02-08-1995','02-20-1995','03-20-1995','04-20-1995','05-20-1995','06-20-1995','07-20-1995','08-20-1995','09-20-1995','10-20-1995','11-20-1995','12-20-1995','02-20-1996','04-20-1996','05-20-1996','06-23-1996','08-20-1996','09-20-1996','09-20-1993','10-20-1993','11-20-1993','12-20-1993','10-20-1996','02-14-2001') \
and quantity in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59) \
and  acct in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) and (tax_state = 'CO' or tax_state = 'TX'))
;
; <SECTION=PARTITIONED_QUALIFY>
; <TEST=PQ1>
;162
;Character, single column
; No group by
select max(total) from orders where status in ('CN', 'OR', 'SH', 'BO')
;
; <TEST=PQ2>
;163
; Group by
select product_no, min(total) from orders where product_no in ('P900', 'XPCP', 'S100', 'A940', 'G523') group by product_no
;
; <TEST=PQ3>
select status, product_no, sum(amount) from orders \
where status in ('SH','CN') and \
product_no = 'A940' group by status, product_no
;
;<COMMON>
disconnect

