Sqlite3列分区(Sqlite3 column division)

编程入门 行业动态 更新时间:2024-10-23 21:40:12
Sqlite3列分区(Sqlite3 column division)

我在sqlite3 db文件中有以下列(FirstCol,SecondCol,ThirdCol):

1 Inside 100 1 Outside 200 2 Inside 46 2 Outside 68

第一列的类型为INT,第二列的类型为TEXT,第三列的类型为INT。

对于每个FirstCol值(在这种情况下只有1和2),我需要获得与Outside / Inside相关的值的结果,即200/100,其中FirstCol = 1,68/46,其中FirstCol = 2。

我不介意这是通过单个查询还是通过创建新表来完成的,我只需要该结果。

谢谢。

I have the following columns (FirstCol, SecondCol, ThirdCol) in a sqlite3 db file:

1 Inside 100 1 Outside 200 2 Inside 46 2 Outside 68

First column has type INT, second has type TEXT and third one has type INT.

For each FirstCol value (in this case just 1 and 2) i need to obtain the result of the value associated with Outside/Inside, which is to say 200/100 where FirstCol=1 and 68/46 where FirstCol=2.

I don't mind whether this is done with a single query or by creating a new table, i just need that result.

Thanks.

最满意答案

您必须使用相关子查询查找不同行的值:

SELECT FirstCol, (SELECT ThirdCol FROM MyTable WHERE FirstCol = T.FirstCol AND SecondCol = 'Outside' ) / (SELECT ThirdCol FROM MyTable WHERE FirstCol = T.FirstCol AND SecondCol = 'Inside' ) AS Result FROM (SELECT DISTINCT FirstCol FROM MyTable) AS T;

You have to look up the values from different rows with correlated subqueries:

SELECT FirstCol, (SELECT ThirdCol FROM MyTable WHERE FirstCol = T.FirstCol AND SecondCol = 'Outside' ) / (SELECT ThirdCol FROM MyTable WHERE FirstCol = T.FirstCol AND SecondCol = 'Inside' ) AS Result FROM (SELECT DISTINCT FirstCol FROM MyTable) AS T;

更多推荐

本文发布于:2023-08-04 11:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分区   division   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!