在 Postgres 中避免多个子查询的语法

发布于 2024-10-29 08:43:48 字数 1393 浏览 4 评论 0原文

我是 postgres 的新手,所以请放轻松。

我正在尝试编写一个查询,以便对于任何用户,我都可以在其名称出现在同一批处理戳中的日志中之前一分钟和之后一分钟提取所有日志文件(包括他们的活动和其他人的活动) 。

chat.batchstamp is varchar
chat.datetime is timestamp
chat.msg is text
chat.action is text    (this is the field with the username)

这是我想要使用的单独命令,我只是不知道如何将它们组合在一起,以及这是否真的是正确的路径。

SELECT batchstamp, datetime, msg FROM chat WHERE action LIKE 'username';

预期输出:

batchstamp   datetime      msg  
abc          2010-12-13 23:18:00 System logon          
abc          2010-12-13 10:12:13 System logon    
def          2010-12-14 11:12:18 System logon

SELECT * FROM chat WHERE datetime BETWEEN datetimefrompreviousquery -interval '1 分钟' AND datetimefrompreviousquery +interval '1 分钟';

您能帮我解释一下我应该做什么来提供上一个查询的数据吗?查询到第二个查询?我研究过子查询,但是我需要运行两个子查询吗?我应该建立一个临时表吗?

完成这一切后,如何确保查询匹配的时间在同一个批处理标记内?

如果您能够为我指明正确的方向,那就太好了。如果您能够提供查询,那就更好了。如果我的解释没有意义,也许我已经看这个太久了。

感谢您抽出时间。

根据下面的 nate c 代码,我使用了这个:

SELECT * FROM chat, 
( SELECT batchstamp, datetime FROM chat WHERE action = 'fakeuser' ) 
AS log WHERE chat.datetime BETWEEN log.datetime - interval '1 minute' AND log.datetime + '1 minute';

它似乎不会返回“fakeuser”的每次点击,当它返回时,它会从每个“batchstamp”中提取日志,而不仅仅是找到“fakeuser”的日志。我要进行另一个嵌套查询吗?这种类型的程序叫什么,以便我可以进一步研究它?

再次感谢。

I'm new to postgres, so please take it easy on me.

I'm trying to write a query so that for any user, I can pull ALL of the log files (both their activity and the activity of others) for one minute prior and one minute after their name appears in the logs within the same batchstamp.

chat.batchstamp is varchar
chat.datetime is timestamp
chat.msg is text
chat.action is text    (this is the field with the username)

Here are the separate commands I want to use, I just don't know how to put them together and if this is really the right path to go on this.

SELECT batchstamp, datetime, msg FROM chat WHERE action LIKE 'username';

Anticipated output:

batchstamp   datetime      msg  
abc          2010-12-13 23:18:00 System logon          
abc          2010-12-13 10:12:13 System logon    
def          2010-12-14 11:12:18 System logon

SELECT * FROM chat WHERE datetime BETWEEN datetimefrompreviousquery - interval '1 minute' AND datetimefrompreviousquery + interval '1 minute';

Can you please help explain to me what I should do to feed data from the previous query in to the second query? I've looked at subqueries, but do I need to run two subqueries? Should I build a temporary table?

After this is all done, how do I make sure that the times the query matches are within the same batchstamp?

If you're able to point me in the right direction, that's great. If you're able to provide the query, that's even better. If my explanation doesn't make sense, maybe I've been looking at this too long.

Thanks for your time.

Based on nate c's code below, I used this:

SELECT * FROM chat, 
( SELECT batchstamp, datetime FROM chat WHERE action = 'fakeuser' ) 
AS log WHERE chat.datetime BETWEEN log.datetime - interval '1 minute' AND log.datetime + '1 minute';

It doesn't seem to return every hit of 'fakeuser' and when it does, it pulls the logs from every 'batchstamp' instead of just the one where 'fakeuser' was found. Am I in for another nested query? What's this type of procedure called so I can further research it?

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2024-11-05 08:43:48

您的第一个查询可以进入 from 子句,并用“(”括号和“as alias”名称括起来。之后,您可以像在查询的其余部分中引用普通表一样引用它。

SELECT
* 
FROM chat,
(
    SELECT
        batchstamp,
        datetime, 
        msg
    FROM log
    WHERE action LIKE 'username'
) AS log

WHERE chat.datetime BETWEEN
    log.datetime - interval '1 minute'
    AND log.datetime + interval '1 minute';

这应该可以帮助您开始。

You first query can go in the from clause with '(' brackets around it and 'as alias' name. After that you can reference it as you would a normal table in the rest of the query.

SELECT
* 
FROM chat,
(
    SELECT
        batchstamp,
        datetime, 
        msg
    FROM log
    WHERE action LIKE 'username'
) AS log

WHERE chat.datetime BETWEEN
    log.datetime - interval '1 minute'
    AND log.datetime + interval '1 minute';

That should get you started.

尴尬癌患者 2024-11-05 08:43:48

一位同事提出了以下解决方案,它似乎提供了我正在寻找的结果。感谢大家的帮助。

SELECT batchstamp, datetime, msg INTO temptable FROM chat WHERE action = 'fakeusername';

select a.batchstamp, a.action, a.datetime, a.msg
FROM chat a, temptable b
WHERE a.batchstamp = b.batchstamp
and (
a.datetime BETWEEN b.datetime - interval '1 minute'
AND b.datetime + interval '1 minute'
) and a.batchstamp = '2011-3-1 21:21:37'
group by a.batchstamp, a.action, a.datetime, a.msg
order by a.datetime;

A colleague at work came up with the following solution which seems to provide the results I'm looking for. Thanks for everyone's help.

SELECT batchstamp, datetime, msg INTO temptable FROM chat WHERE action = 'fakeusername';

select a.batchstamp, a.action, a.datetime, a.msg
FROM chat a, temptable b
WHERE a.batchstamp = b.batchstamp
and (
a.datetime BETWEEN b.datetime - interval '1 minute'
AND b.datetime + interval '1 minute'
) and a.batchstamp = '2011-3-1 21:21:37'
group by a.batchstamp, a.action, a.datetime, a.msg
order by a.datetime;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文