Skip to content
Jul 30 10

PREDICTION function – Oracle

by admin

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

Jul 30 10

Analytics Functions – Oracle

by admin

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”;

Jul 29 10

Network Manager Applet missing from Notification Area – Ubuntu

by admin

sudo vi /etc/NetworkManager/nm-system-settings.conf
and set managed=true

Jul 28 10

Soundex – Python

by admin

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:
DEK
[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]
Jul 28 10

Hierarchical Query – Oracle

by admin

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

Jul 25 10

Data-centric Programming

by admin

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]
Jul 24 10

Copying a block of text – vi

by admin

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

Jul 20 10

The Zen of Python

by admin
oracle@oxford:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
Jul 8 10

Online methods doc – Python

by admin

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)
Jul 6 10

Unicode in Python

by admin
nihao=U'''\u3053\u3093\u306B\u3061\u306F
.\u4F60\u597D'''
print nihao
# こんにちは
# 你好