SQL - 用Empty替换重复值

时间:2018-03-27 10:05:56

标签: mysql sql


我有一张看起来像这样的桌子

Company  Date       Paper  Condition
Company1 19-12-2007 PaperA Release Second Term
Company1 19-12-2007 PaperA Add Third Term
Company1 19-12-2007 PaperA Append First Term

我试图用空替换副本,这将导致

Company  Date       Paper  Condition
Company1 19-12-2007 PaperA Release Second Term
                           Add Third Term
                           Append First Term

到目前为止,我所尝试过的只是使用distinct并且它无法按预期工作。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT Case When Rank=1 then
            `Company`
       Else ''
       End as Company,
       Case When Rank=1 then
            `Date`
       Else ''
       End as `Date`,
       Case When Rank=1 then
            `Paper`
       Else ''
       End as `Paper`, 
       `Condition` 
  FROM (SELECT t.*,
               CASE 
                 WHEN @Company != t.Company OR @Date != t.`Date` OR  @Paper != t.`Paper`
                     THEN @rownum := 1 
                 ELSE @rownum := @rownum + 1 
               END AS rank,
               @Company := t.Company AS var_category,
               @Date := t.`Date` AS var_Date,
               @Paper := t.`Paper` AS var_Paper 
          FROM Table1 t
          JOIN (SELECT @rownum := null, @Company := '') r ) x

输出

Company     Date        Paper   Condition
Company1    19-12-2007  PaperA  Release Second Term
                                Add Third Term
                                Append First Term

现场演示

  

http://sqlfiddle.com/#!9/42c636/5