English 中文(简体)
按条款列出非组别一栏(小幅)
原标题:Include non-aggregate column in group by clause (with a slight wrinkle)

我有一个这样的表:

timestamp                value           person
===============================================
2010-01-12 00:00:00       33              emp1
2010-01-12 11:00:00       22              emp1
2010-01-12 09:00:00       16              emp2
2010-01-12 08:00:00       16              emp2
2010-01-12 12:12:00       45              emp3
2010-01-12 13:44:00       64              emp4
2010-01-12 06:00:00       33              emp1
2010-01-12 15:00:00       12              emp5

我想找到与每个人相关的最高价值。 显而易见的问题是:

select person,max(value) from table group by person

现在,我想列入与每个最高点(价值)相关的时间段。 我无法在上述询问中使用时间段一栏,因为众所周知,这个栏按条款在小组中获胜。 因此,我写道:

select x.timestamp,x.value,x.person from table as x,
(select person,max(value) as maxvalue from table group by person order by maxvalue 
 desc) as y
where x.person = y.person
and x.value = y.maxvalue

This works -- to an extent. I now see:

timestamp                value           person
===============================================
2010-01-12 13:44:00       64              emp4
2010-01-12 12:12:00       45              emp3
2010-01-12 06:00:00       33              emp1
2010-01-12 00:00:00       33              emp1
2010-01-12 08:00:00       16              emp2
2010-01-12 09:00:00       16              emp2
2010-01-12 15:00:00       12              emp5

现在,问题在于,我从头一和时二的所有条目来看都达到了同样的最高点。

我只想看到最新时间。 我想:

timestamp                value           person
===============================================
2010-01-12 13:44:00       64              emp4
2010-01-12 12:12:00       45              emp3
2010-01-12 06:00:00       33              emp1
2010-01-12 09:00:00       16              emp2
2010-01-12 15:00:00       12              emp5

What kind of query would I have to write? Is it possible to extend the nested query I wrote to achieve what I want or does one have to rewrite everything from the scratch?

如果它很重要,因为我正在使用Sqlite,那么,时间灯实际上被储存为陪审日。 我利用时间(日期)职能,把他们重新转化为每一次调查中的严格代表性。

最佳回答

你们几乎在那里:

SELECT max(x.timestamp) AS timestamp, x.value, x.person
     , y.max_value, y.ct_value, y.avg_value
FROM   table AS x
JOIN  (
    SELECT person
         , max(value) as max_value
         , count(value) as ct_value
         , avg(value) as avg_value
    FROM   table
    GROUP  BY person
    ) AS y ON (x.person, x.value) = (y.person, y.max_value)
GROUP BY x.person, x.value, y.max_value, y.ct_value, y.avg_value
-- ORDER  BY x.person, x.value

不能将<代码>max(x.timestamp)与同样的nes格中计算,因为你不希望每人的绝对最高值,而是随附最高值。 因此,您不得不在下一个问答阶段再加一时间。

Compute max(x.timestamp) before you convert it to its string representation - though your format would sort correctly, too. But that that should perform better.

请注意,我如何改变你与[内部]的条件和(简化)加入的条件。 同样,更像标准灵活、更可读。

所有这一切可以在一个定点一级进行,其功能为:“条形”、“条形”和“条形”——第1条——价值(+/条)”——这些功能在所有大的建筑工程中实施,但并非在结构上实施。


Edit

在征求意见后增加的合计数。

问题回答

暂无回答




相关问题
sqlite3 is chopping/cutting/truncating my text columns

I have values being cut off and would like to display the full values. Sqlite3 -column -header locations.dbs " select n.namelist, f.state, t.state from names n left join locations l on l.id = n.id ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Metadata for columns in SQLite v2.8 (PHP5)

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)? sqlite_fetch_column_types (OO:...

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签