sql tips ims user group meeting fall 2002. aggregation: count provides a count of all (distinct)...

20
SQL Tips IMS User Group Meeting Fall 2002

Upload: karen-smith

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

SQL Tips

IMS User Group Meeting

Fall 2002

 

Page 2: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: COUNT

Provides a count of all (distinct) values in a particular column or table. The column can be either alpha or numeric. Null values in the column are included in the count.

SyntaxCOUNT({* | [DISTINCT|ALL] expr})

Page 3: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: Count

SELECTSELECT catalog_nbr catalog_nbr,, COUNTCOUNT (*)(*) ASAS Num_of_Stu Num_of_Stu FROMFROM PS_DWSA_STDNT_ENRLPS_DWSA_STDNT_ENRL WHEREWHERE acad_group acad_group == 'TCHE''TCHE' ANDAND acad_career acad_career == 'UGRD''UGRD' ANDAND stdnt_enrl_status stdnt_enrl_status == 'E''E' ANDAND subject subject == 'FSOS''FSOS'GROUPGROUP BYBY catalog_nbr catalog_nbr

CATALOG_NBR NUM_OF_STU1101 5042101 573101 91

… …

Page 4: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Provides a sum of all (distinct) values in a particular column. The column must be numeric. Null values in the column are not included in the sum.

SyntaxSUM([DISTINCT|ALL] n)

Aggregation: SUM

Page 5: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: SUM

SELECTSELECT acad_career acad_career,, subject subject,, SUMSUM ((unt_takenunt_taken)) units_taken units_taken

FROMFROM PS_DWSA_STDNT_ENRLPS_DWSA_STDNT_ENRL WHEREWHERE subject subject == 'SOIL''SOIL'GROUPGROUP BYBY acad_career acad_career,, subject subject

ACAD_CAREER SUBJECT UNITS_TAKENGRAD SOIL 205UGRD SOIL 1027

Page 6: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: HAVING

Use the HAVING clause to restrict which groups of rows defined by the GROUP BY clause are returned by the query.

Page 7: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: HAVING

SELECT catalog_nbr, class_section, COUNT (*) AS num_of_stu

FROM PS_DWSA_STDNT_ENRL

WHERE acad_group = 'TCHE'

AND acad_career = 'UGRD'

AND stdnt_enrl_status = 'E'

AND subject = 'FSOS'

AND component_main = 'DIS'

GROUP BY catalog_nbr, class_section

HAVING COUNT (*) > 50

CATALOG_NBR CLASS_SECTION NUM_OF_STU1101 3 511101 4 51

Page 8: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Case/Decode/NVL

• Case and Decode statements both perform procedural logic inside a SQL statement without having to resort to PL/SQL .

• All of these queries will return a list of student names with their secondary email addresses unless they didn’t report a secondary address, then it will return their primary email address.

• It is best to use the CASE statement when comparing ranges or more complex logic.

Page 9: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Case/Decode/NVL

CASE

SELECT DISTINCT a.NAME, CASE WHEN a.emailid_2 IS NULL THEN a.emailid_1

ELSE a.emailid_2 END email_add FROM PS_DWSA_DEMO_ADDR a,

PS_DWSA_PROG_DTL b WHERE a.emplid = b.emplid

AND b.acad_prog = '32UGR'

Page 10: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

DECODE

SELECT DISTINCT a.NAME,

DECODE (a.emailid_2, NULL, a.emailid_1, a.emailid_2) email_add

FROM PS_DWSA_DEMO_ADDR a, PS_DWSA_PROG_DTL b

WHERE a.emplid = b.emplid

AND b.acad_prog = '32UGR'

Case/Decode/NVL

Page 11: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

NVL

SELECT DISTINCT a.NAME,

NVL (a.emailid_2, a.emailid_1) email_add

FROM PS_DWSA_DEMO_ADDR a, PS_DWSA_PROG_DTL b

WHERE a.emplid = b.emplid

AND b.acad_prog = '32UGR'

Case/Decode/NVL

Page 12: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

SELECT a.NAME, (CASE WHEN a.vac_hrs_taken_ytd <= 40 THEN 'GET

A LIFE' WHEN a.vac_hrs_taken_ytd BETWEEN 41 AND

100 THEN 'NEED A BREAK?' WHEN a.vac_hrs_taken_ytd >= 101 THEN

'WELL RESTED' END ) mental_wellbeing FROM PS_DWPY_VAC_SICK a WHERE a.deptid = '831A' AND a.fisc_yr = '2003' AND a.pay_period = '06' AND a.empl_status = 'A'ORDER BY 2

Case/Decode/NVL

Page 13: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Case/Decode/NVL

Syntax Logic

DECODE(F1, E2,E3, E4)

If F1 = E2 THEN E3 ELSE E4

NVL(E1, E2) If E1 IS NULL THEN E2 ELSE E1

CASE WHEN E1 THEN E2 ELSE E3 END

If E1 TRUE THEN E2 ELSE E3

Page 14: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

Aggregation: ROLLUP

The use of a ROLLUP clause in the GROUP BY part of the SQL expression displays subtotals and grand totals depending on its use.

Page 15: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

SELECT NVL (catalog_nbr, 'GRAND_TOTAL') catalog_nbr, class_section,

SUM (unt_taken)

total_units, COUNT (*) num_of_stu

FROM PS_DWSA_STDNT_ENRL

WHERE acad_group = 'TCHE'

AND acad_career = 'UGRD'

AND stdnt_enrl_status = 'E'

AND subject = 'FSOS'

GROUP BY ROLLUP (catalog_nbr, class_section)

Aggregation: ROLLUP

CATALOG_NBR CLASS_SECTION TOTAL_UNITS NUM_OF_STU1101 1 0 2521101 2 150 50

… … … …5426 1 21 75426 21 7

GRAND_TOTAL 2722 1178

Page 16: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

INLINE VIEWS

You can use a SQL statement in the FROM clause of a SQL statement. This is called an inline view. Oracle treats the data set that is returned from the inline view as if it were a table.

Page 17: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

SELECT a.NAME, a.office1_phone FROM PS_DWHR_DEMO_ADDR a, (SELECT x.emplid FROM PS_DWHR_JOB x WHERE x.deptid = '831A'

AND x.status_flg = 'C' AND x.job_terminated ='N') b

WHERE a.emplid = b.emplid;

INLINE VIEWS

NAME OFFICE1_PHONEWorker,John D 612/625-2845Worker,Mary J 612/625-2059

Page 18: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

ROUND

Returns a number rounded to m places right of the decimal point; if m is omitted, to 0 places. m can be negative to round off digits left of the decimal point. m must be an integer.

SyntaxROUND(n[,m])

Page 19: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

SELECTSELECT ROUNDROUND ((AVGAVG ((eng_act_scoreeng_act_score),), 11))

FROMFROM PS_DWAD_UGRS_SCRSPS_DWAD_UGRS_SCRS

WHEREWHERE eng_act_score eng_act_score !=!= 00

ROUND

NOTE: There has been an issue with the Web Query tool involving the selection of NUMBER fields that don’t have the scale and precision defined. The error message is returned as ‘ERROR: 007~ASP 0101~UNEXPECTED ERROR~THE FUNCTION RETURNED |.’. Use of the ROUND function will alleviate the issue.

AVG_ENG_ACT22.1

WITH:AVG_ENG_ACT

22.092173

WITHOUT:

Page 20: SQL Tips IMS User Group Meeting Fall 2002. Aggregation: COUNT Provides a count of all (distinct) values in a particular column or table. The column can

A text file with these SQL Tips will be available under the

Information section at http://dw.umn.edu