通过在sql中运行分组

时间:2018-01-26 13:20:30

标签: mysql

我正在桌面上查询以下查询。

表:

+----+-------------+--------------+----------------------------+------------+--------+
| id | Qty_holding | Qty_reserved | created                    | tokenid_id | uid_id |
+----+-------------+--------------+----------------------------+------------+--------+
|  1 |          10 |            0 | 2018-01-18 10:52:14.957027 |          1 |      1 |
|  2 |          20 |            0 | 2018-01-18 11:20:08.205006 |          8 |      1 |
|  3 |         110 |            0 | 2018-01-18 11:20:21.496318 |         14 |      1 |
|  4 |          10 |            0 | 2018-01-23 14:26:49.124607 |          1 |      2 |
|  5 |           3 |            0 | 2018-01-23 15:00:26.876623 |         11 |      2 |
|  6 |           7 |            0 | 2018-01-23 15:08:41.887240 |         11 |      2 |
|  7 |          11 |            0 | 2018-01-23 15:22:48.424224 |         11 |      2 |
|  8 |          15 |            0 | 2018-01-23 15:24:03.419907 |         11 |      2 |
|  9 |          19 |            0 | 2018-01-23 15:24:26.531141 |         11 |      2 |
| 10 |          23 |            0 | 2018-01-23 15:27:11.549538 |         11 |      2 |
| 11 |          27 |            0 | 2018-01-23 15:27:24.162944 |         11 |      2 |
| 12 |   7.7909428 |   0.11459088 | 2018-01-23 15:27:24.168643 |          1 |      2 |
| 13 |           3 |            0 | 2018-01-23 15:36:51.412340 |         14 |      2 |
| 14 |   7.5585988 |   0.11459088 | 2018-01-23 15:36:51.417177 |          1 |      2 |
| 15 |           6 |            0 | 2018-01-24 08:43:46.635069 |         14 |      2 |
| 16 |   7.3262548 |   0.11459088 | 2018-01-24 08:43:46.639984 |          1 |      2 |
| 17 |           9 |            0 | 2018-01-24 10:09:08.207816 |         14 |      2 |
| 18 |   7.0939108 |   0.11459088 | 2018-01-24 10:09:08.212842 |          1 |      2 |
| 19 |           6 |            3 | 2018-01-24 13:43:08.929586 |         14 |      2 |
| 20 |           3 |            6 | 2018-01-24 14:49:56.960112 |         14 |      2 |
| 21 |           0 |            9 | 2018-01-24 14:50:33.423671 |         14 |      2 |
| 22 |          30 |            9 | 2018-01-24 14:51:14.865453 |         14 |      2 |
| 23 |   4.7704708 |   0.11459088 | 2018-01-24 14:51:14.870256 |          1 |      2 |
| 24 |          27 |           12 | 2018-01-24 14:56:56.914009 |         14 |      2 |
| 25 |          24 |           15 | 2018-01-24 14:57:56.475939 |         14 |      2 |
| 26 |          21 |           15 | 2018-01-24 14:58:06.750903 |         14 |      2 |
| 27 |          18 |           15 | 2018-01-24 15:02:43.203878 |         14 |      2 |
| 28 |   4.7705074 |   0.11459088 | 2018-01-24 15:02:43.224901 |          1 |      2 |
| 29 |          24 |            0 | 2018-01-24 15:03:40.421943 |         11 |      2 |
| 30 |   4.9535074 |   0.11459088 | 2018-01-24 15:03:40.441552 |          1 |      2 |
| 31 |           1 |            0 | 2018-01-26 10:35:33.173801 |         18 |      2 |
| 32 |          10 |           15 | 2018-01-26 12:46:03.780807 |         14 |      2 |
+----+-------------+--------------+----------------------------+------------+--------+

查询1:

select uid_id
     , tokenid_id
     , max(created) as max_created 
  from accounts_userholding 
 group 
    by uid_id
     , tokenid_id 

+--------+------------+----------------------------+
| uid_id | tokenid_id | max_created                |
+--------+------------+----------------------------+
|      1 |          1 | 2018-01-18 10:52:14.957027 |
|      1 |          8 | 2018-01-18 11:20:08.205006 |
|      1 |         14 | 2018-01-18 11:20:21.496318 |
|      2 |          1 | 2018-01-24 15:03:40.441552 |
|      2 |         11 | 2018-01-24 15:03:40.421943 |
|      2 |         14 | 2018-01-26 12:46:03.780807 |
|      2 |         18 | 2018-01-26 10:35:33.173801 |
+--------+------------+----------------------------+

查询2:

select uid_id
     , Qty_holding
     , Qty_reserved tokenid_id
     , max(created) as max_created 
  from accounts_userholding 
 group 
    by uid_id
     , tokenid_id 

+--------+-------------+--------------+------------+----------------------------+
| uid_id | Qty_holding | Qty_reserved | tokenid_id | max_created                |
+--------+-------------+--------------+------------+----------------------------+
|      1 |          10 |            0 |          1 | 2018-01-18 10:52:14.957027 |
|      1 |          20 |            0 |          8 | 2018-01-18 11:20:08.205006 |
|      1 |         110 |            0 |         14 | 2018-01-18 11:20:21.496318 |
|      2 |          10 |            0 |          1 | 2018-01-24 15:03:40.441552 |
|      2 |           3 |            0 |         11 | 2018-01-24 15:03:40.421943 |
|      2 |           3 |            0 |         14 | 2018-01-26 12:46:03.780807 |
|      2 |           1 |            0 |         18 | 2018-01-26 10:35:33.173801 |
+--------+-------------+--------------+------------+----------------------------+

上面的Qty_holding值与最新日期不对应。例如,对于tokenid_id 14和uid_id,因为2最新记录是

| 32 |          10 |           15 | 2018-01-26 12:46:03.780807 |         14 |      2 |

但是上面的查询将qty_holding设为3。

有关mysql功能的任何见解都会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

根据经验:在SELECT中将普通列与聚合函数混合使用时,需要使用GROUP BY。如果在SELECT中没有普通的列和聚合函数,请不要使用GROUP BY。

放入GROUP BY的东西全部来自SELECT但是聚合函数(和可能的常量)。

例如,如果您有查询:

select a, substring(b,3), 'x', max(y)
from yourtable

您需要使用GROUP BY。你省略'x',因为它是一个常数,你省略了聚合函数。剩下的就转到了GROUP BY。

select a, substring(b,3), 'x', max(y)
from yourtable
group by a, substring(b,3)

以前的MySQL版本允许非常自由地使用GROUP BY,结果往往只是错误/不正确的代码。