繁体   English   中英

ProgrammingError:“日期”列不存在

[英]ProgrammingError: column “date” doesn't exist

我是SQL的新手,目前正尝试解决数据表问题。

我有一个数据 ,现在需要首先查找日期,在该日期上请求会导致错误。 它们从日志数据库中作为时间戳拉出。 之后,检查状态where not status = '200 OK'并显示超过1%的请求导致错误的日期, having count(*) > 0.01order by num desc having count(*) > 0.01

def number_one_error():
    """
        Percentage of errors from requests
        Counting errors and timestamps
        Output:
            number one errors
    """
    db = psycopg2.connect(database=dbname)
    c = db.cursor()
    c.execute('''
select date
from (select date(log.time) AS date_column,
        count (*) as request_error
        from log where not status = '200 OK'
    group by log.time) as oneerror
join (select date(log.time) AS date_column,
        count(*) as requests
    from log
    group by log.time) as total
on oneerror.date_column = total.date_column
where (round(((oneerror.request_error)/all_requests),3> 0.01))
              ''')
    number_one_error = c.fetchall()
    db.close()


psycopg2.ProgrammingError: column "date" does not exist
LINE 2: select date

您指的是指出的错误列,如果希望将列命名为date ,则也可以在外部查询中使用别名

SELECT oneerror.date_column AS date

以下建议无关紧要,因为实际上数据库是PostgreSql(使用psycopg2是线索)时,问题已用mysql标记。

当我更仔细地查询查询时,我发现其中存在一些错误,您正在对timestamp类型进行GROUP BY ,因为很显然您希望每个日期都需要数据,而根本不需要联接。 这是我的版本,我认为它会更好。 我将错误请求和所有请求都计入同一查询中,其中COUNT(CASE...)将仅计算错误请求。 请注意,我在GROUP BYHAVING中都使用了别名

SELECT date_column as date, 100 * ROUND(error/ok, 3) as percent
FROM (SELECT DATE(time) as date_column, 
      COUNT(*) as ok, 
      COUNT(CASE WHEN status != '200 OK' THEN 1 ELSE NULL END) as error
      FROM log
      GROUP BY date_column
) s
HAVING (percent > 1)

我认为错误很明显。 您没有名为date列。

我怀疑您想要:

select oneerror.date_column

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM