简体   繁体   English

如何使用sqlite3选择多个​​联接/分组依据?

[英]How to do multiple join / group by selects using sqlite3?

I have a sqlite3 database with one table called orig: 我有一个带一个名为orig的表的sqlite3数据库:

CREATE TABLE orig (sdate date, stime integer, orbnum integer);

What I want to do is select the first date/time for each orbnum. 我要做的是为每个Orbnum选择第一个日期/时间。 The only problem is that stime holds the time as a very awkward integer. 唯一的问题是stime将时间保存为一个非常尴尬的整数。

Assuming a six-digit number, the first two digits show the hour, the 3./4. 假设一个六位数字,前两位数字表示小时,即3./4。 show the minutes, and the last two digits show the seconds. 显示分钟,最后两位数字显示秒。 So a value of 12345 is 1:23:45, whereas a value of 123456 is 12:34:56. 因此,值12345是1:23:45,而值123456是12:34:56。

I figured I'd do this using two nested join/group statements, but somehow I cannot get it to work properly. 我以为我会使用两个嵌套的join / group语句来执行此操作,但是以某种方式我无法使其正常工作。 Here's what I've got so far: 到目前为止,这是我得到的:

select s.orbnum, s.sdate, s.stime
from (
    select t.orbnum, t.sdate, t.stime, min(t.sdate) as minsdate
    from (
        select orbnum, sdate, stime, min(stime) as minstime
        from scia group by orbnum, sdate
    ) as t inner join orig as s on s.stime = t.minstime and s.sdate = t.sdate and s.orbnum = t.orbnum
) as d inner join scia as s on s.stime = d.stime and s.sdate = minsdate and s.orbnum = d.orbnum
where s.sdate >= '2002-08-01' limit 0,200;

This is the error I get: 这是我得到的错误:

Error: no such column: t.orbnum

I'm sure it's just some stupid mistake, but actually, I'm quite new to SQL ... 我敢肯定这只是一些愚蠢的错误,但是实际上,我对SQL还是很新的...

Any help is greatly appreciated :) 任何帮助是极大的赞赏 :)

Edit: 编辑:

After fixing the obvious typo, the query runs -- but returns an empty result set. 修正明显的拼写错误后,查询将运行-但返回空结果集。 However, the table holds ~10yrs of data, with about 12 orbnums per day and about 4-5 different times per orbnum. 但是,该表保存着约10年的数据,每天大约有12个Orbnum,每个Orbnum大约有4-5次不同的时间。 So I guess there's some mistake in the logic of the query ... 所以我想查询逻辑上有些错误...

In your last join, you have d , which is the result of your double nested select, and you join s on it. 在上一个联接中,您有d ,这是双嵌套select的结果,并在其上联接s From there, t is not visible. 从那里看不到t That's why you get the “no such column: t.orbnum” error. 这就是为什么您会收到“无此列:t.orbnum”错误的原因。 Maybe you meant s.orbnum = d.orbnum ? 也许您的意思是s.orbnum = d.orbnum吗?

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

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