sqlite2:连接另一个表中每列的最大值(子查询参考)?
我正在使用以下数据库:
CREATE TABLE datas (d_id INTEGER PRIMARY KEY, name_id numeric, countdata numeric);
INSERT INTO datas VALUES(1,1,20); //(NULL,1,20);
INSERT INTO datas VALUES(2,1,47); //(NULL,1,47);
INSERT INTO datas VALUES(3,2,36); //(NULL,2,36);
INSERT INTO datas VALUES(4,2,58); //(NULL,2,58);
INSERT INTO datas VALUES(5,2,87); //(NULL,2,87);
CREATE TABLE names (n_id INTEGER PRIMARY KEY, name text);
INSERT INTO names VALUES(1,'nameA'); //(NULL,'nameA');
INSERT INTO names VALUES(2,'nameB'); //(NULL,'nameB');
我想做的是选择 names
的所有值(行) - 将附加 datas
的所有列,对于 datas
.countdata
为 n_id
最大值的行(当然,其中 name_id = n_id
)。
我可以通过以下查询到达那里:
sqlite> .header ON
sqlite> SELECT * FROM names AS n1
LEFT OUTER JOIN (
SELECT d_id, name_id, countdata FROM datas AS d1
WHERE d1.countdata IN (
SELECT MAX(countdata) FROM datas
WHERE name_id=1
)
) AS p1 ON n_id=name_id;
n1.n_id|n1.name|p1.d_id|p1.name_id|p1.countdata
1|nameA|2|1|47
2|nameB|||
...但是 - 显然 - 它仅适用于单行(由 name_id=1
显式设置的行)。
问题是,每当我尝试以某种方式引用“当前”n_id 时,SQL 查询都会失败:
sqlite> SELECT * FROM names AS n1
LEFT OUTER JOIN (
SELECT d_id, name_id, countdata FROM datas AS d1
WHERE d1.countdata IN (
SELECT MAX(countdata) FROM datas
WHERE name_id=n1.n_id
)
) AS p1 ON n_id=name_id;
SQL error: no such column: n1.n_id
有什么方法可以在 Sqlite2 中实现我想要的功能吗?
预先感谢,
干杯!
I'm using the following database:
CREATE TABLE datas (d_id INTEGER PRIMARY KEY, name_id numeric, countdata numeric);
INSERT INTO datas VALUES(1,1,20); //(NULL,1,20);
INSERT INTO datas VALUES(2,1,47); //(NULL,1,47);
INSERT INTO datas VALUES(3,2,36); //(NULL,2,36);
INSERT INTO datas VALUES(4,2,58); //(NULL,2,58);
INSERT INTO datas VALUES(5,2,87); //(NULL,2,87);
CREATE TABLE names (n_id INTEGER PRIMARY KEY, name text);
INSERT INTO names VALUES(1,'nameA'); //(NULL,'nameA');
INSERT INTO names VALUES(2,'nameB'); //(NULL,'nameB');
What I would like to do, is to select all values (rows) of names
- to which all columns of datas
will be appended, for the row where datas
.countdata
is maximum for n_id
(and of course, where name_id = n_id
).
I can somewhat get there with the following query:
sqlite> .header ON
sqlite> SELECT * FROM names AS n1
LEFT OUTER JOIN (
SELECT d_id, name_id, countdata FROM datas AS d1
WHERE d1.countdata IN (
SELECT MAX(countdata) FROM datas
WHERE name_id=1
)
) AS p1 ON n_id=name_id;
n1.n_id|n1.name|p1.d_id|p1.name_id|p1.countdata
1|nameA|2|1|47
2|nameB|||
... however - obviously - it only works for a single row (the one explicitly set by name_id=1
).
The problem is, the SQL query fails whenever I try to somehow reference the "current" n_id
:
sqlite> SELECT * FROM names AS n1
LEFT OUTER JOIN (
SELECT d_id, name_id, countdata FROM datas AS d1
WHERE d1.countdata IN (
SELECT MAX(countdata) FROM datas
WHERE name_id=n1.n_id
)
) AS p1 ON n_id=name_id;
SQL error: no such column: n1.n_id
Is there any way of achieving what I want in Sqlite2??
Thanks in advance,
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,好吧 - 这根本不是小事,但这里有一个解决方案:
好吧,希望这最终能帮助别人,:)
干杯!
注释:请注意,仅调用 max(countdata) 就会完全搞砸
d_id
:因此要获得正确的对应
d_id
,我们必须执行max ()
分别对datas
- 然后与完整的datas
执行某种相交(除了 sqlite 中的相交要求有相同数量的两个数据集中的列,这里的情况并非如此 - 即使我们这样做,如上所示,d_id
也会是错误的,因此 intersect 将不起作用)。实现此目的的一种方法是使用某种临时表,然后利用多表 SELECT 查询,以便在完整
数据
和通过max(countdata)返回的子集之间设置条件
,如下所示:或者,我们可以重写上面的内容,使用 SELECT 子查询(子选择?),如下所示:
Oh, well - that wasn't trivial at all, but here is a solution:
Well, hope this ends up helping someone, :)
Cheers!
Notes: note that just calling max(countdata) screws up competely
d_id
:so to get correct corresponding
d_id
, we must domax()
ondatas
separately - and then perform sort of an intersect with the fulldatas
(except that intersect in sqlite requires that there are equal number of columns in both datasets, which is not the case here - and even if we made it that way, as seen aboved_id
will be wrong, so intersect will not work).One way to do that is in using a sort of a temporary table, and then utilize a multiple table SELECT query so as to set conditions between full
datas
and the subset returned viamax(countdata)
, as shown below:or, we can rewrite the above so a SELECT subquery (sub-select?) is used, like this: