在同一个表sql中组合行

时间:2014-11-17 20:46:22

标签: mysql sql max

SAMPLE_DATA

id_type,seq_no,acct_name,_acct#,address
12345,67,jiimm,,167 s.40th st
12345,67,jiimm joe the 3rd,,167 s.40th st
12345,67,jiimm
12345,67,,0981_1,po box 1234
12345,80,Lee,,1234 street ave
12345,80,Lee
12345,80,,588_1,109 road st

CODE

SELECT `ID`_type,
       seq_no,
       MAX(`acct_name`) AS acct_name,
       MAX(`acct_#`) AS acct_#,
       address
FROM `test_table`
GROUP BY `ID`_type,
         seq_no;

我想根据id_type和seq_no合并行。我使用max来合并行,但由于MAX acct#,我覆盖了任何现有的地址和acct_names。

我的结果

id_type,seq_no.,acct_name,_acct#,address
12345,67,jiimm joe the 3rd,0981_1,167 s.40th st
12345,80,Lee,588_1,109 road st
  • 丢失po框1234为67 -
  • 失去1234街道大道80,失去了jiimm -

期望的结果

12345,80,Lee,588_1,109 road st
12345,80,Lee,588_1,1234 street ave    
12345,67,jiimm,0981_1,167 s.40th st
12345,67,jiimm,0981_1,po box 1234
12345,67,jiimm joe the 3rd,0981_1,167 s.40th st

3 个答案:

答案 0 :(得分:1)

这可以为您提供所需内容,但请阅读上述问题下方的评论/问题。有一个含糊不清的“从许多方面挑选一排”的情况需要澄清。在这种模糊的情况下,你暗示规则要求提供这个代码所做的最小非空帐户名,但是你可以看到它需要如何以一种方式处理帐户名,并处理acct(#)并解决不同的问题。办法。我认为你正在寻找能够根据难以记住的规则提供结果的应用程序。即使你发布了所述处理规则,像这样的时髦规则也会被报告为缺陷。因此,您可能希望增强捕获此数据的上游流程,以提供更有纪律的数据。

SQLFIDDLE link - 简而言之,内部查询填充缺失值,然后外部结果集传递不同的行。我测试了这个空白值不为空。我确实快速添加代码来处理空值,但我没有使用空值测试它,所以我建议测试它,如果这是生产将使用。

select distinct * from (

  select     d.id_type, d.seq_no
            ,coalesce( nullif( acct_name, ''), min_acct_name ) as merged_acct_name
            ,coalesce( nullif( acct, ''),      max_acct      ) as merged_acct
            ,coalesce( nullif( address, ''),   max_address )   as merged_address
  from       test_table  d
  left join  ( select   id_type, seq_no
                       ,max( acct )      as max_acct
                       ,max( address )   as max_address
               from     test_table 
               group by id_type, seq_no
             ) as max_
        on   max_.id_type = d.id_type and max_.seq_no = d.seq_no
        and  (   coalesce( d.acct,'' )      = '' 
              or coalesce( d.address,'' )   = '' )
  left join  ( select   id_type, seq_no
                       ,min( acct_name ) as min_acct_name
               from     test_table 
               where    coalesce( acct_name, '' ) <> ''
               group by id_type, seq_no
             ) as min_
        on   min_.id_type = d.id_type and min_.seq_no = d.seq_no
        and  coalesce( d.acct_name,'' ) = ''
  ) as t

order by id_type, seq_no desc, merged_acct_name, merged_acct, merged_address

答案 1 :(得分:0)

试试这个:

select    distinct t1.id_type, t1.seq_no
          ,coalesce( t1.acct_name, t2.acct_name ) as merged_acct_name
          ,coalesce( t1.acct,      t2.acct      ) as merged_acct
          ,coalesce( t1.address,   t2.address )   as merged_address
from       test_table  t1
left join  test_table  t2
  on t1.id_type = t2.id_type
  and t1.seq_no = t2.seq_no
where concat(coalesce( t1.acct_name, t2.acct_name ) 
          ,coalesce( t1.acct,      t2.acct      )
          ,coalesce( t1.address,   t2.address )  ) is not null
order by t1.id_type, t1.seq_no;

或者:

select    distinct t1.id_type, t1.seq_no
          ,coalesce( t1.acct_name, t2.acct_name ) as merged_acct_name
          ,coalesce( t1.acct,      t3.acct      ) as merged_acct
          ,coalesce( t1.address,   t4.address )   as merged_address
from       test_table  t1
left join  test_table  t2
  on t1.id_type = t2.id_type
  and t1.seq_no = t2.seq_no
left join  test_table  t3
  on t1.id_type = t3.id_type
  and t1.seq_no = t3.seq_no
left join  test_table  t4
  on t1.id_type = t4.id_type
  and t1.seq_no = t4.seq_no
where concat(coalesce( t1.acct_name, t2.acct_name ) 
          ,coalesce( t1.acct,      t3.acct      )
          ,coalesce( t1.address,   t4.address )  ) is not null
order by t1.id_type, t1.seq_no;

SQL Fiddle Demo

答案 2 :(得分:0)

SELECT
  D1.id_type
  , D1.seq_no
  , IFNULL(D1.acct_name, (SELECT MIN(acct_name) FROM data D WHERE D.id_type = D1.id_type AND D.seq_no = D1.seq_no)) t
  , IFNULL(D1.acct_no, (SELECT MAX(acct_no) FROM data D WHERE D.id_type = D1.id_type AND D.seq_no = D1.seq_no)) s
  , D1.address
FROM data D1
WHERE D1.address IS NOT NULL
ORDER BY id_type, seq_no DESC, acct_name
;

返回

| ID_TYPE | SEQ_NO |                 T |      S |         ADDRESS |
|---------|--------|-------------------|--------|-----------------|
|   12345 |     80 |               Lee |  588_1 |     109 road st |
|   12345 |     80 |               Lee |  588_1 | 1234 street ave |
|   12345 |     67 |             jiimm | 0981_1 |     po box 1234 |
|   12345 |     67 |             jiimm | 0981_1 |   167 s.40th st |
|   12345 |     67 | jiimm joe the 3rd | 0981_1 |   167 s.40th st |

除了第三和第四行的顺序之外,这与您的预期输出一致。但是,对于大量数据,MAXMIN将越来越有可能获得有限的帮助。

SQL Fiddle