SQL Tips

advertisement
SQL Tips
1. Counts
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.
SELECT
FROM
WHERE
AND
AND
AND
GROUP BY
catalog_nbr, COUNT (*) as Num_of_Stu
ps_dwsa_stdnt_enrl
acad_group = 'TCHE'
acad_career = 'UGRD'
stdnt_enrl_status = 'E'
subject = 'FSOS'
catalog_nbr
CATALOG_NBR NUM_OF_STU
1101
504
2101
57
3101
91
…
…
Syntax
COUNT({* | [DISTINCT|ALL] expr})
2. Sums
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
SELECT
FROM
WHERE
GROUP BY
acad_career, subject, SUM (unt_taken) units_taken
ps_dwsa_stdnt_enrl
subject = 'SOIL'
acad_career, subject
ACAD_CAREER SUBJECT UNITS_TAKEN
GRAD
SOIL
205
UGRD
SOIL
1027
Syntax
SUM([DISTINCT|ALL] n)
3. Having
Use the HAVING clause to restrict which groups of rows defined by the GROUP BY clause are
returned by the query
SELECT
FROM
WHERE
AND
AND
AND
AND
GROUP BY
HAVING
catalog_nbr, class_section, COUNT (*) AS num_of_stu
ps_dwsa_stdnt_enrl
acad_group = 'TCHE'
acad_career = 'UGRD'
stdnt_enrl_status = 'E'
subject = 'FSOS'
component_main = 'DIS'
catalog_nbr, class_section
COUNT (*) > 50
CATALOG_NBR CLASS_SECTION NUM_OF_STU
1101
3
51
1101
4
51
4. Case/Decode/NVL
Case and Decode statements both perform procedural logic inside a SQL statement without
having to use PL/SQL.
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'
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'
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'
All of these queries will return a list of student names with there 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.
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
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
5. Rollup
The use of a ROLLUP clause in the GROUP BY part of the SQL expression displays subtotals
and grand totals depending on it’s use.
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)
CATALOG_NBR CLASS_SECTION TOTAL_UNITS NUM_OF_STU
1101
1
0
252
1101
2
150
50
…
…
…
…
5426
1
21
7
5426
21
7
GRAND_TOTAL
2722
1178
6. Inline Views
You can use a SQL statement in the FROM clause of a SQL statement. This is called a inline
view. Oracle treats the data set that is returned from the inline view as if it were a table.
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;
NAME
Worker,John D
Worker,Mary J
OFFICE1_PHONE
612/625-2845
612/625-2059
7. 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
SELECT ROUND(AVG(ENG_ACT_SCORE), 4)
FROM PS_DWAD_UGRS_SCRS
WHERE ENG_ACT_SCORE != 0
Syntax
ROUND(n[,m])
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 specified. The error message is return as ‘ERROR:
007~ASP 0101~UNEXPECTED ERROR~THE FUNCTION RETURNED |.’. Use of the ROUND
function will alleviate the issue.
Download