我是SQL的新手,目前正尝试解决数据表问题。
我有一个数据table,现在需要首先查找请求导致错误的日期。它们从日志数据库中作为时间戳拉出。然后,检查状态where not status = '200 OK'
,并显示having count(*) > 0.01
,order by num desc
上超过1%的请求导致错误的日期。
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
答案 0 :(得分:1)
您所指出的列是错误的,如果您希望将列命名为date
,则也可以在外部查询中使用别名
SELECT oneerror.date_column AS date
以下建议无关紧要,因为问题实际上是用mysql标记的,而实际上数据库是PostgreSql(使用psycopg2
是线索)
当我仔细查看您的查询时,发现其中存在一些错误,很明显您希望按日期查询数据,并且联接是,因此您正在对GROUP BY
类型执行timestamp
完全没有必要。这是我的版本,我认为它会更好。我将错误请求和所有请求都计入同一查询中,其中COUNT(CASE...)
将仅计算错误请求。请注意,我在GROUP BY
和HAVING
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)
答案 1 :(得分:0)
我认为错误很明显。您没有名为date
的列。
我怀疑您想要
select oneerror.date_column