選擇第一個和最后一個元素的最有效方法,SQLite?

[英]Most efficient way to select 1st and last element, SQLite?


What is the most efficient way to select the first and last element only, from a column in SQLite?

從SQLite中的列中選擇第一個和最后一個元素的最有效方法是什么?

6 个解决方案

#1


The first and last element from a row?

一行中的第一個和最后一個元素?

SELECT column1, columnN
FROM mytable;

I think you must mean the first and last element from a column:

我認為你必須指的是列中的第一個和最后一個元素:

SELECT MIN(column1) AS First,
       MAX(column1) AS Last
FROM mytable;

See http://www.sqlite.org/lang_aggfunc.html for MIN() and MAX().

有關MIN()和MAX()的信息,請參見http://www.sqlite.org/lang_aggfunc.html。

I'm using First and Last as column aliases.

我使用First和Last作為列別名。

#2


if it's just one column:

如果它只是一列:

SELECT min(column) as first, max(column) as last FROM table

if you want to select whole row:

如果要選擇整行:

SELECT 'first',* FROM table ORDER BY column DESC LIMIT 1
UNION 
SELECT 'last',* FROM table ORDER BY column ASC LIMIT 1

#3


The most efficient way would be to know what those fields were called and simply select them.

最有效的方法是知道這些字段被調用的內容並簡單地選擇它們。

SELECT `first_field`, `last_field` FROM `table`;

#4


Probably like this:

可能是這樣的:

SELECT dbo.Table.FirstCol, dbo.Table.LastCol FROM Table

You get minor efficiency enhancements from specifying the table name and schema.

通過指定表名和模式,您可以獲得較小的效率增強。

#5


min()/max() approach is wrong. It is only correct, if the values are ascending only. I needed something liket this for currency rates, which are random raising and falling.

min()/ max()方法是錯誤的。如果值僅是升序,則它是正確的。我需要一些類似於貨幣匯率的東西,這是隨機的上漲和下跌。

This is my solution:

這是我的解決方案:

select st.*
from stats_ticker st,
    (
        select min(rowid) as first, max(rowid) as last --here is magic part 1
        from stats_ticker
        -- next line is just a filter I need in my case. 
        -- if you want first/last of the whole table leave it out.
        where timeutc between datetime('now', '-1 days') and datetime('now')
        ) firstlast
WHERE 
    st.rowid = firstlast.first     --and these two rows do magic part 2
    OR st.rowid = firstlast.last
ORDER BY st.rowid;

magic part 1: the subselect results in a single row with the columns first,last containing rowid's.

魔法第1部分:子選擇導致單行首先包含列,最后包含rowid。

magic part 2 easy to filter on those two rowid's.

魔法部分2容易過濾那兩個rowid的。

This is the best solution I've come up so far. Hope you like it.

這是我到目前為止提出的最佳解決方案。希望你喜歡。

#6


We can do that by the help of Sql Aggregate function, like Max and Min. These are the two aggregate function which help you to get last and first element from data table .

我們可以借助Sql Aggregate函數來實現,比如Max和Min。這兩個聚合函數可以幫助您從數據表中獲取最后一個元素。

Select max (column_name ), min(column name) from table name

Max will give you the max value means last value and min will give you the min value means it will give you the First value, from the specific table.

Max會給你最大值意味着最后一個值而min會給你最小值意味着它會從特定表中給你第一個值。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/30/d0316e6bb18072cb67ae91aeeb127ca8.html



 
  © 2014-2022 ITdaan.com 联系我们: