我在Stackoverflow中发现了大量关于如何选择组中第一行和最后一行的示例,但我无法根据需要调整它们。我对MySQL的有限知识并没有帮助。
某些数据(date_time,val1和val2)随机保存。我需要将它们分组15分钟然后计算,每15分钟间隔一次:
这是我的数据,期望的结果和迄今为止的最佳努力......
Try this :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ghostWhiteColor"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<com.hbb20.CountryCodePicker
android:id="@+id/ccp"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:ccp_showFullName="false"
app:ccp_showNameCode="false"
app:ccp_showPhoneCode="true" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/register_password"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="-2dp"
android:hint="@string/hint_phone"
android:inputType="phone"
android:padding="10dp"
android:singleLine="true"
app:met_floatingLabel="normal"
app:met_helperText="Enter your phone number."
app:met_helperTextAlwaysShown="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<com.rey.material.widget.Button
android:id="@+id/button_register_accept"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="@color/holo_blue_light"
android:padding="10dp"
android:text="@string/btn_accept"
android:textColor="@color/ghostWhiteColor" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
......这里是同一个SQLFiddle_grouping_15_min
的小提琴有人可以帮我使用语法吗?
答案 0 :(得分:1)
SELECT a.*
FROM `test-table` a
JOIN
( SELECT FLOOR(UNIX_TIMESTAMP(date_time)/900) timeslice
, MIN(date_time) min_date_time
, MAX(date_time) max_date_time
FROM `test-table`
GROUP
BY timeslice
) b
ON b.timeslice = FLOOR(UNIX_TIMESTAMP(a.date_time)/900)
AND a.date_time IN(b.min_date_time,b.max_date_time);
+---------------------+------+------+
| date_time | val1 | val2 |
+---------------------+------+------+
| 2017-11-01 00:00:00 | 100 | 200 |
| 2017-11-01 00:14:00 | 100 | 210 |
| 2017-11-01 00:15:00 | 100 | 240 |
| 2017-11-01 00:28:00 | 120 | 230 |
| 2017-11-01 00:30:00 | 110 | 270 |
| 2017-11-01 00:44:59 | 130 | 265 |
| 2017-11-01 00:50:00 | 120 | 290 |
| 2017-11-01 00:58:00 | 80 | 320 |
+---------------------+------+------+
答案 1 :(得分:0)
我找到了一个简单的解决方案,语法简单明了。对于像我这样的新手来说,这可能是最简单的:
SELECT FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(`date_time`)/900)*900) AS _timeslice,
COUNT(*) AS _count,
min(`val1`) as _min_val1,
avg(`val1`) as _avg_val1,
max(`val1`) as _max_val1,
SUBSTRING_INDEX(GROUP_CONCAT(CAST(`val2` AS CHAR) ORDER BY date_time), ',', 1) AS _first_val2,
SUBSTRING_INDEX(GROUP_CONCAT(CAST(`val2` AS CHAR) ORDER BY date_time DESC), ',', 1) AS _last_val2
FROM `test-table`
GROUP BY _timeslice;
处小提琴