Python SQL Query会给出错误的结果

时间:2017-11-22 13:12:52

标签: python sql oracle

我想在Oracle中创建2个视图并保护它们。到目前为止,视图中的值工作得很好并且是正确的。问题是,当我后来想要解决这些值时,我会得到随机错误的值。

所以我得到了所有的值,从ID和下层和上层边界(对于那种情况我认为不重要,因为值是正确的),但是大约40%auf我的下限值和上限值,所以2 Select查询中的值是错误的。是否有可能Python在短时间内无法处理如此多的SQL命令?

代码:

dsn= cx_Oracle.makedsn("*****.******",1521,"orcl")
db= cx_Oracle.connect('******','******',dsn)
db.autocommit = True
_tabelle ='******'
cur = db.cursor()


 cur.execute("CREATE VIEW convert_cat_%s (quotient, %s, rnum) AS SELECT (COUNT(*)/(SELECT COUNT(*) FROM %s ) ) as quotient, %s, row_number() over ( order by (COUNT(*)/(SELECT COUNT(*) FROM %s ) ) desc ) as rnum FROM     %s  GROUP BY %s ORDER BY quotient desc" %convert_cat)
    cur.execute("CREATE VIEW boarders_%s AS Select b.ID,a.unten as lower ,a.oben as upper FROM( SELECT t3.RNUM, t3.%s, lag(t3.com_Pr,1,0) OVER (order by rnum asc) as unten , t3.com_PR as oben FROM( SELECT t1.rnum, t1.%s , SUM(t2.quotient) as com_Pr FROM CONVERT_CAT_%s t1 INNER JOIN CONVERT_CAT_%s t2 ON t1.rnum >= t2.rnum group by t1.rnum, t1.%s, t1.quotient ORDER BY RNUM asc ) t3) a INNER JOIN %s b ON b.%s = a.%s order by ID asc" %calculate)
    get_id=(name_convert_column)
    #That Query gives back wrong values, even if the view itself, created in 
    #the row before is correct
    cur.execute("Select id, lower, upper FROM boarders_%s ORDER BY id asc" % get_id)

    lower = {}
    upper ={}
    for row in cur:
        lower.update({row[0]: float(row[1])})
        upper.update({row[0]: float(row[2])})
        print row[0], row[1],row[2]
    for key in lower:
        key =key
        lower_boarder = lower.get(key)
        upper_boarder = upper.get(key)
        print key, lower_boarder,upper_boarder

它不是完整的代码,但其余的并不重要我猜:) 如果你能以任何方式帮助我,为什么有时会出现随机错误的值,我将不胜感激。如果我多次运行该脚本,过了一会儿,结果是正确的:/

编辑:

整个代码遍历表格,并为多个分类列执行显示的代码。对于4/5列,它可以完美地工作。具有错误值的行是具有非常小的边界的值。视图计算累积概率,并在多个范围内分离[0; 1]的范围。 F.E. (一个简单的例子): 我有100个人,70个男人,30个女人 [0; 0.3] - 女性 [0.3; 1] - 男人

错误的列有很多类别,因此范围非常小,寄宿生彼此非常接近(逗号后3-4位)....

也许这些信息会有帮助吗?

0 个答案:

没有答案