使用varchars和null值进行分组

时间:2014-06-04 06:21:51

标签: mysql

我需要一些帮助。我有一个很大的桌子,上面有帐户信息和发票。该表是以每发票方式构建的,这意味着如果客户有多个待处理的发票,则会有与发票待处理的行数相同的行。我需要把它转过来#39;我需要一条单线'每个客户,并将所有发票信息作为添加的列(除了客户的个人信息之外)。我已设法创建一个视图,将以有序的方式显示信息有点'方式:

+------------------------------------------------+---------------------+-------------+
| cust  | invoice1            | invoice2            | invoice3 | invoice4 | invoice5 |
+------------------------------------------------+---------------------+-------------+
| Lady  | 1000000000002069080 | NULL                | NULL     | NULL     | NULL     |
| Lady  | NULL                | 1000000000001957290 | NULL     | NULL     | NULL     |
| Boy   | 1000000000002116078 | NULL                | NULL     | NULL     | NULL     |
| Gent  | 1000000000002068468 | NULL                | NULL     | NULL     | NULL     |
| Waldo | 1000000000002094726 | NULL                | NULL     | NULL     | NULL     |
| Waldo | NULL                | 1000000000002097744 | NULL     | NULL     | NULL     |
| John  | NULL                | 1000000000001991541 | NULL     | NULL     | NULL     |
| John  | 1000000000002104255 | NULL                | NULL     | NULL     | NULL     |
| Peter | 1000000000002066307 | NULL                | NULL     | NULL     | NULL     |
| Peter | NULL                | 1000000000001954615 | NULL     | NULL     | NULL     |
+-------+---------------------+---------------------+----------+----------+----------+

我需要最终结果如下:

+------------------------------------------------+---------------------+-------------+
| cust  | invoice1            | invoice2            | invoice3 | invoice4 | invoice5 |
+------------------------------------------------+---------------------+-------------+
| Lady  | 1000000000002069080 | 1000000000001957290 | NULL     | NULL     | NULL     |
| Boy   | 1000000000002116078 | NULL                | NULL     | NULL     | NULL     |
| Gent  | 1000000000002068468 | NULL                | NULL     | NULL     | NULL     |
| Waldo | 1000000000002094726 | 1000000000002097744 | NULL     | NULL     | NULL     |
| John  | 1000000000002104255 | 1000000000001991541 | NULL     | NULL     | NULL     |
| Peter | 1000000000002066307 | 1000000000001954615 | NULL     | NULL     | NULL     |
+-------+---------------------+---------------------+----------+----------+----------+

但我不知道如何使用GROUP BY可以帮助我,因为值设置为varchar并且不能使用SUM来添加字段,这是我在这里读到的建议。

这是'表' (查看)定义:

+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| cust           | varchar(100) | YES  |     |         |       |
| invoice1       | varchar(25)  | YES  |     | NULL    |       |
| invoice2       | varchar(25)  | YES  |     | NULL    |       |
| invoice3       | varchar(25)  | YES  |     | NULL    |       |
| invoice4       | varchar(25)  | YES  |     | NULL    |       |
| invoice5       | varchar(25)  | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

如何将这些字段放在一个不太重的查询中?我挥动了大约需要操作的10万条记录。有什么建议吗?

我使用MySQL和MyISAM作为引擎,我不能改变SQL引擎。

2 个答案:

答案 0 :(得分:3)

也许是这样的:

SELECT
    tbl.cust,
    MAX(tbl.invoice1) AS invoice1,
    MAX(tbl.invoice2) AS invoice2,
    MAX(tbl.invoice3) AS invoice3,
    MAX(tbl.invoice4) AS invoice4,
    MAX(tbl.invoice5) AS invoice5
FROM
    Table1 as tbl
GROUP BY
    tbl.cust

<强>更新

M Khalid Junaid:非常善于提供sql fiddler link

修改

您还可以考虑是否要使用MINMAX。如果每invoice只有一个cust。然后MIN聚合将提供相同的输出:

SELECT
    tbl.cust,
    MIN(tbl.invoice1) AS invoice1,
    MIN(tbl.invoice2) AS invoice2,
    MIN(tbl.invoice3) AS invoice3,
    MIN(tbl.invoice4) AS invoice4,
    MIN(tbl.invoice5) AS invoice5
FROM
    Table1 as tbl
GROUP BY
    tbl.cust

答案 1 :(得分:0)

我喜欢使用IFNULL来处理空值,如下所示:

select cust, 
  sum(ifnull(invoice1,0)) i1, 
  sum(ifnull(invoice2,0)) i2, 
  sum(ifnull(invoice3,0)) i3, 
  sum(ifnull(invoice4,0)) i4, 
  sum(ifnull(invoice5,0)) i5
from table
group by cust;

如果您不想添加发票,那么您可以按顺序列出(无法包含空值):

select cust, 
  group_concat(invoice1) i1, 
  group_concat(invoice2) i2, 
  group_concat(invoice3) i3, 
  group_concat(invoice4) i4, 
  group_concat(invoice5) i5
from table
group by cust;