Postgres时差

时间:2015-09-13 07:08:12

标签: postgresql timestamp datepart

我正在尝试使用postgresql从表(login_history为t1)中检索分钟的时差。

当我尝试这段代码时

((date_part('hour', timestamp '2014-04-25 09:44:21')- date_part('hour', timestamp '2014-04-25 08:32:21'))*60 +(date_part('minutes', timestamp '2014-04-25 09:44:21')- date_part('minutes', timestamp '2014-04-25 08:32:21'))) as TimeNew

工作正常。 但是当我尝试使用此代码从表t1中检索信息时

((date_part('hour', timestamp t1.login_date)- date_part('hour', timestamp t1.logout_date))*60 +
(date_part('minutes', timestamp t1.login_date)- date_part('minutes', timestamp t1.logout_date))
) as TimeNew

它会抛出此错误

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "t1"

由于

2 个答案:

答案 0 :(得分:5)

我会使用减去两个时间戳得到的间隔来表示更简单的表达式:

select extract (epoch from (timestamp '2014-04-25 09:44:21' - timestamp '2014-04-25 08:32:21'))::integer/60

(给出72)

或您的桌子:

select extract (epoch from (t1.logout_date - t1.login_date))::integer/60

如果你需要施放:

select extract (epoch from (t1.logout_date::timestamp - t1.login_date::timestamp))::integer/60

或查看to_timestamp函数以进行自定义字符串解析:http://www.postgresql.org/docs/9.4/static/functions-formatting.html

答案 1 :(得分:0)

我需要在t1之前从查询中删除public void total() { foreach (DataGridViewRow item in tbl_Purchase.Rows) { int n = item.Index; int tPrice=0; int tQuantity=0; int tAmount=0 tPrice += (int.Parse(tbl_Purchase.Rows[n].Cells[3].Value.ToString())); tQuantity += (int.Parse(tbl_Purchase.Rows[n].Cells[4].Value.ToString())); tAmount += (int.Parse(tbl_Purchase.Rows[n].Cells[5].Value.ToString())); l_Price.Text = (Convert.ToString(tPrice)); l_Quantity.Text = (Convert.ToString(tQuantity)); l_Amount.Text = (Convert.ToString(tAmount)); } } 并且查询有效。