SQL400 Tips and Tricks - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

SQL400 Tips and Tricks

Description:

Books A Million. Why? Industry standard / Cross platform. Learn four statements / Big payback ... condition is not used for a cross join. Percent of Total ... – PowerPoint PPT presentation

Number of Views:620
Avg rating:3.0/5.0
Slides: 57
Provided by: gob3
Category:
Tags: books | million | sql400 | tips | tricks

less

Transcript and Presenter's Notes

Title: SQL400 Tips and Tricks


1
SQL/400Tips and Tricks
  • Bill Fortenberry
  • Books A Million

2
Why?
  • Industry standard / Cross platform
  • Learn four statements / Big payback
  • Interactive SQL
  • Embedded SQL / Dynamic SQL

3
  • CUSTOMER LISTING
  • STATE AL
  • BY DESC YTD ORDERS
  •  
  • CUST NAME ORDERS SALES REP
  • ------------ ------ --------------
  • ABC Corp. 75 Freddy Smith
  • Smith Smith 63 Joe Jackson
  • DEF Company 55
  • EF Inc. 40 Freddy Smith

4
  • Select cusmst.cuname
  • cusmst.cuytdc
  • Ifnull(repmst.rename,
  • )
  • From cusmst left outer join repmst
  • on cusmst.curep repmst.rerep
  • Where custat StateID
  • Order by 2 desc, 1

5
  • Select cusmst.cuname
  • cusmst.cuytdd
  • Ifnull(repmst.rename
  • )
  • From cusmst left outer join repmst
  • on cusmst.curep repmst.rerep
  • Where custat StateID
  • Order by 2 desc, 1

6
  • Select cusmst.cuname
  • cusmst.cuytdd /
  • cusmst.cuytdd
  • Ifnull(repmst.rename,
  • )
  • From cusmst left outer join repmst
  • on cusmst.curep repmst.rerep
  • Where custat StateID
  • Order by 2 desc, 1

7
  • Select cusmst.cuname
  • cusmst.cuytdd /
  • cusmst.cuytdd
  • Ifnull(repmst.rename,
  • )
  • From cusmst left outer join repmst
  • on cusmst.curep repmst.rerep
  • Where custat StateID
  • Order by 3, 1

8
  • Select from invmst
  • Select inumbr, idept, idesc
  • from invmst
  • Select inumbr, idept, idesc
  • from invmst
  • where istat A

9
  • Select inumbr, idept, idesc
  • from invmst
  • where istat A
  • order by inumbr
  • Select inumbr, idept, upper(idesc),
  • qtyohd avgcst as invval
  • from invmst
  • where istat A and ibuyer 173
  • order by idept desc, inumbr

10
  • Delete from invmst
  • Delete from invmst
  • where idept 12

11
  • Insert into cumast
  • (cunbr, cuname, cucity)
  • Values
  • (678987, Atlas Inc., Birmingham)

12
  • Insert into cumast
  • (cunbr, cuname, cucity)
  • select cwidno, cwcnam, cutown
  • from newcus
  • where cutown Atlanta

13
  • Update cumast
  • set cuname Acme Builders,
  • cudisc cudisc .01
  • where custid 123456
  • Update customer
  • set row (Atlas, 13246,
  • Meridian)
  • where custid 2354

14
Interactive SQL
  • Allows prompting for statement syntax.
  • Good learning tool.
  • Reduces the need for fixit programs.
  • Programming tool for Embedded SQL.
  • Allows you to quickly destroy data.

15
Convert Select to Delete
  • 1. Press F9 to retrieve the select command.
  • Select from ordhdr where ohornm 123456
  • 2. Type DELETE over SELECT .
  • Delete from ordhdr where ohornm 123456
  • 3. Press enter.

16
Convert Select to Insert
  • 1. Press F9 to retrieve the select command.
  • Select from orddtl where odornm 123456
  • 2. Press F15 to push the select statement down a
    line, then type the insert command.
  • insert into mylib/orddtl
  • Select from orddtl where odornm 123456
  • 3. Press enter.

17
Convert Select to Update
  • 1. Press F9 to retrieve the select command.
  • Select from orddtl where odornm 123456
  • 2. Type UPDATE over SELECT and use the delete key
    to remove FROM.
  • update orddtl where odornm 123456
  • 3. Place the cursor on the W in where, and press
    F15 twice. This will split the statement at the
    where clause and push it down two lines.
  • update orddtl
  •  
  • where odornm 123456
  • 4. Use the blank line for your set clause.
  • update orddtl
  • set odstat P
  • where odornm 123456

18
Static SQL
  • C/Exec SQL
  • C Update cumast
  • C set cuname Acme Builders
  • C where cunmbr 12345
  • C/End-Exec
  • C/Exec SQL
  • C Update cumast
  • C set cuname NewName
  • C where cunmbr Customer
  • C/End-Exec

19
RPG code to delete records
  • C OrderNum chain rordhdr
  • C if found
  • C delete rordhdr
  • C endif
  • C
  • C OrderNum setll rorddtl
  • C OrderNum reade rorddtl
  • C dow not eof
  • C delete rorddtl
  • C OrderNum reade rorddtl
  • C enddo

20
SQL code to delete records
  • C/Exec SQL
  • C Delete from ordhdr
  • C where ohornm OrderNum
  • C/End-Exec
  •  
  • C/Exec SQL
  • C Delete from orddtl
  • C where odornm OrderNum
  • C/End-Exec

21
Selecting one row from a table
  • C/Exec SQL
  • C Select idept, idescr
  • C Into DeptNum, SkuDescr
  • C From invmst
  • C Where inumbr SkuNumber
  • C/End-Exec

22
Selecting multiple rows
  • Declare the cursor
  • C/Exec SQL
  • C Declare c1 Cursor for
  • C Select inumbr, idept, idescr
  • C From invmst
  • C Where idept DeptNumbr
  • C/End-Exec
  • Open the cursor
  • C/Exec SQL
  • C Open c1
  • C/End-Exec

23
  • Fetch rows into host fields until
  • SQLStt indicates an error
  • C dow SQLStt '00000'
  • C/Exec SQL
  • C fetch next
  • C from c1
  • C into inumbr, idept, idescr
  • C/End-Exec

24
  • If the status field is OK
  • then process this record.
  • C if SQLStt '00000'
  • C exsr ProcessRecord
  • C endif
  • fetch next row
  • C enddo

25
  • Close the cursor
  • C/Exec SQL
  • C close c1
  • C/End-Exec

26
Calculated Column Values
  • Select skunum,
  • upper(idesc),
  • qtyoh,
  • avgcst,
  • qtyoh avgcst
  • from invmst

27
Using a literal column value
  • insert into taxdpt
  • select 123, txdept, txflag
  • from taxdpt
  • where txstrn 734

28
Using row functions in RPG
  • C eval JDEdate 102003
  • C/Exec SQL
  • C set ISOdate DATE(DIGITS(DECIMAL(
  • C JDEdate1900000,7,0)))
  • C/End-Exec

29
Counting the records in a file
  • select count() from invmst
  • COUNT ( )
  • 530,610

30
Department Subtotals
  • select idept, count()
  • from invmst
  • group by idept
  • order by idept
  • Department COUNT ( )
  • 1 30,610
  • 2 26,416
  • 3 74,297
  • 5 23,272
  • 6 40,789

31
Limiting groups with Having
  • select idept, count()
  • from invmst
  • group by idept
  • having count()
  • order by idept

32
Selecting records with a subquery
  • select cunmbr, cuname
  • from cusmst
  • where cuytds (select avg(cuytds)
  • from cusmst)

33
Using a correlated subquery
  • select cunmbr, cuname
  • from cusmst as c1
  • where cuytds (select avg(cuytds)
  • from cusmst as c2
  • where c2.custat
  • c1.custat)

34
The In Predicate
  • Select from orddtl
  • where odsku in (select skunbr
  • from invmst
  • where idept 4)

35
The Exists Predicate
  • update cuswrk
  • set curnme
  • (select rpname from slsrep
  • where cuswrk.repnum
  • slsrep.repnum)
  • where exists (select from slsrep
  • where cuswrk.repnum
  • slsrep.repnum)

36
Old Join Syntax
  • select ordhdr.ohornm,
  • ordhdr.ohcusn,
  • cusmst.cuname
  • from ordhdr, cusmst
  • where ordhdr.ohstat O and
  • ordhdr.ohcusn cusmst.cusnmr

37
New Join Syntax
  • select ordhdr.ohornm,
  • ordhdr.ohcusn,
  • cusmst.cuname
  • from ordhdr join cusmst
  • on ordhdr.ohcusn cusmst.cusnmr
  • where ordhdr.ohstat O

38
Join Types
  • JOIN or INNER JOIN
  • Each row in table to left will be joined with one
    or more rows in table to right using the join
    condition. Any rows in left table that do not
    have corresponding rows in the right table will
    not be in the result table. Produces the same
    results as separating the tables with a comma and
    using the where clause to provide the join
    condition.

39
Join Types
  • LEFT JOIN or LEFT OUTER JOIN
  • Each row in table to left will be joined with one
    or more rows in table to right using the join
    condition. Any rows in left table that do not
    have corresponding rows in the right table will
    be in the result table with null values for
    fields from the right table.

40
Join Types
  • EXCEPTION JOIN
  • Only rows in the left table that have no
    corresponding row in the right table using the
    join condition will be in the result table. Nulls
    are returned for any fields from the right table.

41
Join Types
  • CROSS JOIN
  • Each row in the left table will be joined with
    each row in the right table. The ON condition is
    not used for a cross join.

42
Percent of Total Report
  • IVDEPT IVSKU IVONHD IVACST
  • 3 928564 126 .95
  • 1 214843 26 3.45
  • 1 245279 78 2.33
  • 3 983521 39 3.78
  • 6 394856 132 .28

43
Department Totals Select Statement
  • select ivdept , sum(ivonhdivacst)
  • from invmst
  • group by ivdept

44
  • with depttotals (deptnum, depttot)
  • as (select ivdept,sum(ivonhdivacst)
  • from invmst
  • group by ivdept)
  • select i.ivdept, i.ivsku, i.ivonhd,
  • i.ivacst, d.depttot
  • from invmst as i
  • join depttotals as d
  • on i.ivdeptd.deptnum
  • order by 1,2

45
The final result table
  • IVDEPT IVSKU IVONH IVACST DEPTTOT
  • 1 214843 26 3.45 271.44
  • 1 245279 78 2.33 271.44
  • 3 928564 126 .95 267.12
  • 3 983521 39 3.78 267.12
  • 6 394856 132 .28 36.96

46
The Union Clause
  • Select Test, inumbr, ivohnd
  • from testlib/invmst
  • Union
  • Select Live, inumbr, ivohnd
  • from livelib/invmst

47
Inventory File
  • IVDEPT IVSKU IVONHD IVACST
  • 3 928564 126 .95
  • 1 214843 26 3.45
  • 1 245279 78 2.33
  • 3 983521 39 3.78
  • 6 394856 132 .28

48
Detail Lines
  • select 'd', ivdept,
  • ivsku,(ivonhdivacst)
  • from invmst
  •  
  • TYPE IVDEPT IVSKU AMOUNT
  • d 3 928564 119.70
  • d 1 214843 89.70
  • d 1 245279 181.74
  • d 3 983521 147.42
  • d 6 394856 36.96

49
Department Totals
  • select 's',ivdept,0,
  • sum(ivonhdivacst)
  • from invmst
  • group by 's',ivdept
  •  
  • TYPE IVDEPT IVSKU SUM
  • s 1 0 271.44
  • s 3 0 267.12
  • s 6 0 36.96

50
Grand Total
  • select 't',999,0,
  • sum(ivonhdivacst)
  • from invmst
  •  
  • Type IVDEPT IVSKU SUM
  • t 999 0 575.52

51
The complete select statement
  • select 'd', ivdept, ivsku,(ivonhdivacst)
  • from invmst
  • union all
  • select 's',ivdept,0,sum(ivonhdivacst)
  • from invmst
  • group by 's',ivdept
  • union all
  • select 't',999,0,sum(ivonhdivacst)
  • from invmst
  • order by 2,1,3

52
The final result table
  • TYPE IVDEPT IVSKU ONHAND
  • d 1 214843 89.70
  • d 1 245279 181.74
  • s 1 0 271.44
  • d 3 928564 119.70
  • d 3 983521 147.42
  • s 3 0 267.12
  • d 6 394856 36.96
  • s 6 0 36.96
  • t 999 0 575.52

53
Dynamic SQL
  • Price Download History Inquiry Screen
  • Sku Number ..........
  • Store ..........
  • Price Event ..........
  • From Date ..........
  • To Date ..........
  • The Sku number must be entered.
  • All other fields are optional.

54
  • basic statement and inumbr search clause
  • C eval sqlstm 'select from
    sdipluh where
  • C plusku '
  • C trim(editc(inumbr'3'))
  • store
  • C if store 0
  • C eval sqlstmsqlstm' and plustr'
  • C trim(editc(store'3'))
  • C endif

55
  • event number
  • C if event 0
  • C eval sqlstmsqlstm' and
    pluevt'
  • C trim(editc(event'3'))
  • C endif
  • dates
  • C if datef 0
  • C eval sqlstmsqlstm
  • C ' and plucur between '
  • C trim(editc(datef'3'))
  • C ' and '
  • C trim(editc(datet'3'))
  • C endif
  • add order by clause
  • C eval sqlstmsqlstm' order by
    1,2,3'

56
The Prepare Statement
  • prepare the cursor
  • C/Exec SQL
  • C Prepare dynsqlstm
  • C from sqlstm
  • C/End-Exec
Write a Comment
User Comments (0)
About PowerShow.com