Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

mysql - SQL - Replacing Duplicate Value With Empty


I Have a table that look like this

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

Im trying to replace duplicate with empty which will result in

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

So Far all i've tried is using distinct and it does not work as expected. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

Output

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

Live Demo

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...