PREDICTION function – Oracle
This example returns the average age of customers who are likely to use an affinity card.
SELECT gender, COUNT(*) AS cnt, ROUND(AVG(age)) AS avg_age
FROM mining_data
WHERE PREDICTION(DT_SH_Clas_sample COST MODEL
USING marital_status, education, household_size) = 1
# The PREDICTION function takes into account only the marital_status, education, and household_size predictors.
GROUP BY gender
ORDER BY gender;
C CNT AVG_AGE
- ———- ———-
F 170 38
M 685 42
Analytics Functions – Oracle
RATIO_TO_REPORT is an analytic function. It computes the ratio of a value to the sum of a set of values. If expr evaluates to null, then the ratio-to-report value also evaluates to null.
SELECT ename, sal, ratio_to_report(sal) over (partition by deptno)
FROM emp
It returns the employee name, salary, and the ration of the salary to the sum salary of all employees partitioned by department no.
WIDTH_BUCKET lets you construct equiwidth histograms, in which the histogram range is divided into intervals that have identical size
SELECT customer_id, cust_last_name, credit_limit,
WIDTH_BUCKET(credit_limit, 100, 5000, 10) “Credit Group”
FROM customers WHERE nls_territory = ‘Canada’
ORDER BY “Credit Group”;
sudo vi /etc/NetworkManager/nm-system-settings.conf
and set managed=true
Soundex – Python
Soundex algorithm:
1. Keep the first letter.
2. Convert the remaining letters to digits, according to mapping below:
abcdefghijklmnopqrstuvwxyz => 91239129922455912623919292
The phonetic representation is defined in The Art of Computer Programming, Volume 3: Sorting and Searching, by Donald E. Knuth, as follows:

[From Oracle 10g documentation]
Retain the first letter of the string and remove all other occurrences of the following letters: a, e, h, i, o, u, w, y.
Assign numbers to the remaining letters (after the first) as follows:
b, f, p, v = 1
c, g, j, k, q, s, x, z = 2
d, t = 3
l = 4
m, n = 5
r = 6
If two or more letters with the same number were adjacent in the original name (before step 1), or adjacent except for any intervening h and w, then omit all but the first.
Return the first four bytes padded with 0.
3. Remove consecutive duplicates.
4. Remove all 9s.
5. If the result is shorter than four characters (the first letter plus three digits), pad the result with trailing zeros.
6. if the result is longer than four characters, discard everything after the fourth character.
import string
charToSoundex = string.maketrans(string.letters, "91239129922455912623919292" * 2)
def soundex(source):
if not isalpha(source):
return "0000"
s = source[0].upper() + source[1:].translate(charToSoundex)
s2 = s[0]
for d in s[1:]:
if s2[−1] != d:
s2 += d
s3 = re.sub('9', '', s2)
while len(s3) < 4:
s3 += "0"
return s3[:4]
Hierarchical Query – Oracle
Oracle’s START WITH and CONNECT BY clauses in the SELECT statement traverse a hierarchy.
Without this feature, n number of self-joins (n = max_levels) would be required.
ORDER SIBLINGS BY ename will preserve the hierarchy and also sort the ename within each level.
Prior to Oracle 10g, a circular loop in the hierarchy would return an error, ” ORA-01436: CONNECT BY loop in user data”.
In Oracle 10g, It can be avoided by the NOCYCLE
connect by nocycle prior empno = mgr
Syntax
Oracle 9i

Oracle 10g

Oracle 11g

Data-centric Programming
Busywork code is not important. Data is important. And data is not difficult. It’s only data. If you have too much, filter it. If it’s not what you want, map it. Focus on the data; leave the busywork behind
– Dive into Python
Here are some of my examples
def odd(i):
return i % 2
a = range(10)
b = filter(odd, a)
a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b
[1, 3, 5, 7, 9]
x=[1,-2,3,4,-34]
y = filter(lambda z: z > 0, x)
y
[1,3,4]
# list comprehension
d = [i*2 for i in a]
d
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
map(len,["Oracle","MySQL"])
[6,5]
You could do the same thing with a for loop. Functions like filter are much more expressive. Not only is it easier to write, it’s easier to read, too. Reading the for loop is like walking in woods, you see all the details, but you won’t see the bigger picture until you are out of the woods.
c = []
for n in a:
if odd(n):
c.append(n)
c
[1, 3, 5, 7, 9]
Copying a block of text – vi
1. Go to the the top line to be copied
2. ma: Mark this line as ‘a’
3. Go to the bottom line to be copied
4. y’a: yank the text from the current cursor location to the mark “a”
5. p: paste the text
Visual mode
1. Go to the the top line to be copied
2. v: start visual mode
3. Go to the bottom line to be copied
4. y: yank the highlighted text
5. p: paste the text
The Zen of Python
Online methods doc – Python
cat methods_doc.py
author: Mark Pilgrim
def info(object, spacing=10, collapse=1): """Print methods and doc strings. Takes module, class, list, dictionary, or string.""" methodList = [e for e in dir(object) if callable(getattr(object, e))] processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList])
from methods_doc import info
info([])
info({})
info(1)
Unicode in Python
nihao=U'''\u3053\u3093\u306B\u3061\u306F .\u4F60\u597D''' print nihao # こんにちは # 你好