如何分组数据?

时间:2018-07-25 05:44:46

标签: mysql sql database

当我从多个表中查询数据时,我就有这个了:

+-------------+------+-----------+-----------+-------------+--------+
| 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 |
+-------------+------+-----------+-----------+-------------+--------+

我的问题是,即使他们具有相同的RevenueCode,我如何将具有相同TxId的行分组,以便代替出现2行,而只出现1行。我尝试使用GROUP BY,但无法正常工作。

2 个答案:

答案 0 :(得分:1)

如果您有一个auto_increment列,则可以使用相关子查询来选择分钟数

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)

如果您没有auto_increment ID,我会考虑添加一个。

您也许可以在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)

但是我不建议这样做,因为没有聚合函数的分组方式是非标准的(以sql术语而言)mysql'feature'。如果收到only_full_group_by错误,请查看手册或在Google上查找“解决方案”错误

团体联合

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)

答案 1 :(得分:0)

using (var memoryStream = new MemoryStream())
{
    await formFile.CopyToAsync(memoryStream);
    byte[] bytes = memoryStream.ToArray();

    // do what you want with the bytes
}