标题实际上并没有完全描述问题,即:我有一个日期表 -
1. 2011-07-01 13:01:48
2. 2011-07-01 13:09:36
3. 2011-07-01 13:21:24
4. 2011-07-01 13:35:12
5. 2011-07-01 13:49:23
6. 2011-07-01 13:57:47
7. 2011-07-01 14:05:12
8. 2011-07-01 14:12:45
9. 2011-07-01 14:31:48
10. 2011-07-01 14:47:31
Run Code Online (Sandbox Code Playgroud)
等等.我需要的是每小时获得三个随机日期,例如:
1. 2011-07-01 13:01:48
2. 2011-07-01 13:21:24
3. 2011-07-01 13:49:23
4. 2011-07-01 14:05:12
5. 2011-07-01 14:12:45
6. 2011-07-01 14:47:31
Run Code Online (Sandbox Code Playgroud)
我怎么能在mysql中做到这一点?
我想在花车中有千个分隔符.我在做的是:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> print '{0:n}'.format(123456.0)
123,456
Run Code Online (Sandbox Code Playgroud)
当整数部分有7位或更多位数时,它不起作用:
>>> print '{0:n}'.format(1234567.0)
1.23457e+06
Run Code Online (Sandbox Code Playgroud)
我找到的解决方法是在格式化之前将浮点数转换为整数:
>>> print '{0:n}'.format(int(1234567.0))
1,234,567
Run Code Online (Sandbox Code Playgroud)
是否有一个格式字符串可以处理所有浮点数而无需先将其转换为整数?
我有一个代表产品用法的表,有点像日志.产品使用记录为多个时间戳,我想使用时间范围表示相同的数据.
它看起来像这样(PostgreSQL 9.1):
userid | timestamp | product
-------------------------------------
001 | 2012-04-23 9:12:05 | foo
001 | 2012-04-23 9:12:07 | foo
001 | 2012-04-23 9:12:09 | foo
001 | 2012-04-23 9:12:11 | barbaz
001 | 2012-04-23 9:12:13 | barbaz
001 | 2012-04-23 9:15:00 | barbaz
001 | 2012-04-23 9:15:01 | barbaz
002 | 2012-04-24 3:41:01 | foo
002 | 2012-04-24 3:41:03 | foo
Run Code Online (Sandbox Code Playgroud)
我想要折叠与前一次运行的时间差小于增量(例如:2秒)的行,并获取开始时间和结束时间,如下所示:
userid | begin | end | product
----------------------------------------------------------
001 | 2012-04-23 9:12:05 …
Run Code Online (Sandbox Code Playgroud) 每当我想要运用某些代码路径时,否则只能在难以复制的情况下达到condition
:
if (condition) { code to be tested }
Run Code Online (Sandbox Code Playgroud)
我or
有一个true
价值:
if (true || condition) { code to be tested }
Run Code Online (Sandbox Code Playgroud)
有更优雅的方法吗?
这些是可能的输出格式 ps h -eo etime
21-18:26:30
15:28:37
48:14
00:01
Run Code Online (Sandbox Code Playgroud)
如何解析它们几秒钟?
egreped
到一行,因此不需要循环.我在psycopg2中执行一个链接到PostgreSQL数据库的查询.这是有问题的代码:
with open('dataFile.txt', 'r') as f:
lines = f.readlines()
newLines = [line[:-1] for line in lines]
curr=conn.cursor()
lineString = ','.join(newLines)
curr.execute("SELECT fields.fieldkey FROM fields LEFT JOIN zone ON zone.fieldkey=fields.fieldkey WHERE zone.zonekey = %s;", (newLines[0]))
rows = curr.fetchall()
Run Code Online (Sandbox Code Playgroud)
连接到DB没有问题,行[0]的类型肯定是字符串,我检查了.我的字符串格式的语法有问题吗?
我得到的错误,澄清是这样的:
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud) 我正在做一个ajax POST请求,我得到了这个异常:
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] self.raise_routing_exception(req)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] File "/usr/lib/python2.6/site-packages/flask/app.py", line 1439, in raise_routing_exception
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] raise FormDataRoutingRedirect(request)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] FormDataRoutingRedirect: A request was sent to this URL (http://example.com/myurl) but a redirect was issued automatically by the routing system to "http://example.com/myurl/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL …
Run Code Online (Sandbox Code Playgroud) 我在WTForms
表格中有这个字段
name = StringField('Name', validators = [Optional(), Length(max = 100)])
Run Code Online (Sandbox Code Playgroud)
当字段提交为空form.name.data
时,将按预期包含空字符串.
有没有办法让它返回None
而不是空字符串?这只是因为null
在数据库中处理非常方便,如下所示update
:
update t
set
name = coalesce(%(name)s, name),
other = coalesce(%(other)s, other)
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我不需要检查字段是否为空,并在SQL代码中的Python代码中采取相应的操作.在null
与coalesce
解决了容易.
我想PL/pgSQL
通过psql
命令行将参数传递给匿名块,然后在条件中检查该参数。
SQL 的相关部分在这里:
do $$
begin
if (':para' = 1) then
-- statements;
end if;
end $$
;
Run Code Online (Sandbox Code Playgroud)
我这样称呼这个脚本:
psql -d dbname -v para=1 < script.sql
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ERROR: invalid input syntax for integer: ":para"
LINE 1: SELECT (':para' = 1)
^
QUERY: SELECT (':para' = 1)
CONTEXT: PL/pgSQL function inline_code_block line 3 at IF
Run Code Online (Sandbox Code Playgroud)
我尝试使用 case/when 范式,这也不起作用。
我猜psql
参数与 PL/pgSQL 不兼容?最终,我的目标是,delete
如果我1
作为psql
参数传递,则运行单个语句,如果传递0
.
目前我有这个相当大的查询
count()
按事件名称和日期分组的事件,将每日、每周、每月计数聚合到中间表中。avg()
按事件分组来选择每个中间表的平均计数,对结果进行联合,并且因为我想为每天、每周、每月设置一个单独的列,将填充值 0 放入空列中。查询虽然很大,但我觉得我正在做很多重复的工作。有什么办法可以更好地执行此查询或使其更小吗?我以前没有真正做过这样的查询,所以我不太确定。
WITH monthly_counts as (
SELECT
event,
count(*) as count
FROM tracking_stuff
WHERE
event = 'thing'
OR event = 'thing2'
OR event = 'thing3'
GROUP BY event, date_trunc('month', created_at)
),
weekly_counts as (
SELECT
event,
count(*) as count
FROM tracking_stuff
WHERE
event = 'thing'
OR event = 'thing2'
OR event = 'thing3'
GROUP BY event, date_trunc('week', created_at)
),
daily_counts as (
SELECT
event,
count(*) as count
FROM tracking_stuff
WHERE …
Run Code Online (Sandbox Code Playgroud)