如何对数据进行分组?

xuo3flqw  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(380)

当我从多个表中查询数据时,会出现以下情况:

+-------------+------+-----------+-----------+-------------+--------+
| ComponentId | TxId | AccountNo |  BillNo   | RevenueCode | Amount |
+-------------+------+-----------+-----------+-------------+--------+
|           1 |   27 |    080200 | 080200600 | PT3819      |   1.00 |
|           1 |   28 |    060005 | 320128254 |             |   5.00 |
|           1 |   29 |    201816 | 201830029 | 960245      |   1.00 |
|           1 |   30 |    770304 | 201999999 | 71494       |  13.00 |
|           1 |   30 |    770304 | 201999999 | 71413       |  13.00 |
+-------------+------+-----------+-----------+-------------+--------+

我的问题是,如何将具有相同txid的行分组,即使它们具有相同的revenuecode,以便只显示1行而不是显示2行。我试过使用group by,但是没有用。

z5btuh9x

z5btuh9x1#

如果您有一个自动递增列,那么一个选择最小值的关联子查询将起作用

drop table if exists t;
create table t(id int auto_increment primary key,ComponentId int, TxId int, AccountNo int,  BillNo int,   RevenueCode varchar(10), Amount decimal(10,2));
insert into t (ComponentId , TxId , AccountNo ,  BillNo   , RevenueCode , Amount)
values
(           1 ,   27 ,    080200 , 080200600 , 'PT3819'      ,   1.00 ),
(           1 ,   28 ,    060005 , 320128254 , null          ,   5.00 ),
(           1 ,   29 ,    201816 , 201830029 , '960245'      ,   1.00 ),
(           1 ,   30 ,    770304 , 201999999 , '71494'       ,  13.00 ),
(           1 ,   30 ,    770304 , 201999999 , '71413'       ,  13.00 );

select * from t where id = (select min(id) from t t1 where t1.TxId = t.TxId)

+----+-------------+------+-----------+-----------+-------------+--------+
| id | ComponentId | TxId | AccountNo | BillNo    | RevenueCode | Amount |
+----+-------------+------+-----------+-----------+-------------+--------+
|  1 |           1 |   27 |     80200 |  80200600 | PT3819      |   1.00 |
|  2 |           1 |   28 |     60005 | 320128254 | NULL        |   5.00 |
|  3 |           1 |   29 |    201816 | 201830029 | 960245      |   1.00 |
|  4 |           1 |   30 |    770304 | 201999999 | 71494       |  13.00 |
+----+-------------+------+-----------+-----------+-------------+--------+
4 rows in set (0.00 sec)

如果你没有自动增量标识,我会考虑添加一个。
你也许可以通过txid逃脱一个小组。

drop table if exists t;
create table t(ComponentId int, TxId int, AccountNo int,  BillNo int,   RevenueCode varchar(10), Amount decimal(10,2));
insert into t (ComponentId , TxId , AccountNo ,  BillNo   , RevenueCode , Amount)
values
(           1 ,   27 ,    080200 , 080200600 , 'PT3819'      ,   1.00 ),
(           1 ,   28 ,    060005 , 320128254 , null          ,   5.00 ),
(           1 ,   29 ,    201816 , 201830029 , '960245'      ,   1.00 ),
(           1 ,   30 ,    770304 , 201999999 , '71494'       ,  13.00 ),
(           1 ,   30 ,    770304 , 201999999 , '71413'       ,  13.00 );

select * from t group by txid;

+-------------+------+-----------+-----------+-------------+--------+
| ComponentId | TxId | AccountNo | BillNo    | RevenueCode | Amount |
+-------------+------+-----------+-----------+-------------+--------+
|           1 |   27 |     80200 |  80200600 | PT3819      |   1.00 |
|           1 |   28 |     60005 | 320128254 | NULL        |   5.00 |
|           1 |   29 |    201816 | 201830029 | 960245      |   1.00 |
|           1 |   30 |    770304 | 201999999 | 71494       |  13.00 |
+-------------+------+-----------+-----------+-------------+--------+
4 rows in set (0.00 sec)

但我不建议这样做,因为没有聚合函数的groupby是一个非标准的(用sql术语来说)mysql“特性”。如果你错误地得到一个只满的组,请查看手册或用谷歌搜索“解决方案”的错误
群concat

select ComponentId , TxId , AccountNo ,  BillNo   , group_concat(RevenueCode) Revcodes , Amount 
from t group by txid;

+-------------+------+-----------+-----------+-------------+--------+
| ComponentId | TxId | AccountNo | BillNo    | Revcodes    | Amount |
+-------------+------+-----------+-----------+-------------+--------+
|           1 |   27 |     80200 |  80200600 | PT3819      |   1.00 |
|           1 |   28 |     60005 | 320128254 | NULL        |   5.00 |
|           1 |   29 |    201816 | 201830029 | 960245      |   1.00 |
|           1 |   30 |    770304 | 201999999 | 71494,71413 |  13.00 |
+-------------+------+-----------+-----------+-------------+--------+
4 rows in set (0.00 sec)
jaql4c8m

jaql4c8m2#

select distinct t.ComponentId, t.TxId, t.AccountNo, t.BillNo,
STUFF((select '|' + m.RevenueCode from t m where m.TxId  = t.TxId FOR XML PATH 
('')),1,1,'') as RevenueCode ,
STUFF((select '|' + m.Amount from t m where m.TxId  = t.TxId FOR XML PATH ('')),1,1,'') 
as Amount from t

相关问题