簡體   English   中英

合並同一表SQL中的行

[英]combining rows in same table sql

樣本數據

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

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
  • 輸寶箱1234可獲得67-
  • 80失去1234大街,失去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

這可以為您提供所需的信息,但請在上面的問題下方閱讀我的評論/問題。 有一個模棱兩可的“多選一排”的情況需要澄清。 在這種模棱兩可的情況下,您暗示規則要求提供最小的非空白帳戶名,此代碼可以做到這一點,但是您可以看到它如何要求以一種方式處理帳戶名,並以acct(#)處理並解決其他問題。方式。 我認為您正朝着基於難以記住的規則提供結果的應用程序前進。 這樣的時髦規則最終都會被報告為缺陷,即使您發布了上述處理規則。 因此,您可能需要增強上游捕獲該數據的過程,以提供更嚴格的數據。

SQLFIDDLE鏈接 -簡而言之,內部查詢填充缺少的值,然后外部結果集提供不同的行。 我用不為空的空白值進行了測試。 我確實努力地添加了代碼來處理空值,但是我沒有使用空值對其進行測試,因此,如果可以使用生產環境,則建議對它進行測試。

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

嘗試這個:

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小提琴演示

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小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM