-1

我有表受众:id(int),unixtime(int),国家(int),城市(int),观众(int)。

表格的任命 - 为不同城市和国家的一些抽象受众保留统计数据。每天在不同时间针对不同城市和国家收集数次统计数据。

在撰写查询时需要帮助,该查询应显示每个跨国和城市的受众

  1. 第一个和最后一个日期
  2. 在特定的日期期间

输出应包含

  • country1, city1, unixtime, 观众 - min(unixtime)
  • country1, city1, unixtime, 观众 - max(unixtime)
  • country1, city2, unixtime, 观众 - min(unixtime)
  • 国家 1、城市 2、unixtime、观众 - max(unixtime)
  • ...

或者:

  • country1, city1, begin_unixtime, begin_audience, end_unixtime, end_audience
  • country1、city2、begin_unixtime、begin_audience、end_unixtime、end_audience
  • ...

在此先感谢您的帮助。

4

1 回答 1

0

Try this solution:

SELECT b.country,
       b.city,
       b.unixtime AS begin_unixtime,
       b.audience AS begin_audience,
       c.unixtime AS end_unixtime,
       c.audience AS end_audience       
FROM   (
       SELECT   MIN(id) AS minid,
                MAX(id) AS maxid
       FROM     audience
       WHERE    unixtime BETWEEN <start> AND <end>
       GROUP BY country,
                city
       ) a
JOIN   audience b ON b.id = a.minid
JOIN   audience c ON c.id = a.maxid

In this solution, the min/max unixtime and audience are based on the min/max id (first and last row inserted between specified dates), assuming it's a unique auto-incrementing int.

Replace <start> and <end> with your date range.

于 2012-09-03T18:07:42.213 回答