在 sql 的列中显示类似数据中的空数据

Show empty data in similar data in columns in sql

我有 table 其中我有这样的数据,其中 firstcol 有可以重复的数据

with myTable ( firstcol,secondcol,thirdcol,fourthCol) as 
(  
    select  'sameText1' ,'anytext','anytext1','anytext2' union all
    select  'sameText1' ,'anytext','anytext1','anytext2' union all
    select  'sameText2' ,'anytext','anytext1','anytext2' union all
    select  'sameText2' ,'anytext','anytext1','anytext2' union all
    select  'sameText3' ,'anytext','anytext1','anytext2' union all
    select  'sameText3' ,'anytext','anytext1','anytext2' union all
    select  'sameText3' ,'anytext','anytext1','anytext2' union all
    select  'sameText3' ,'anytext','anytext1','anytext2'
)
select  firstcol,secondcol,thirdcol,fourthCol  from myTable

现在我的要求是显示类似这样的数据

with myDesiredTable ( firstcol,secondcol,thirdcol,fourthCol) as 
(  
    select  'sameText1' ,'anytext','anytext1','anytext2' union all
    select  '' ,'anytext','anytext1','anytext2' union all
    select  'sameText2' ,'anytext','anytext1','anytext2' union all
    select  '' ,'anytext','anytext1','anytext2' union all
    select  'sameText3' ,'anytext','anytext1','anytext2' union all
    select  '' ,'anytext','anytext1','anytext2' union all
    select  '' ,'anytext','anytext1','anytext2' union all
    select  '' ,'anytext','anytext1','anytext2'
)
select  firstcol,secondcol,thirdcol,fourthCol  from myDesiredTable 

显示空而不是类似的字符串 不确定在 SQL

中我该怎么做

尝试使用 ROW_NUMBER 来实现:

;WITH CTE_result
AS
( 
   SELECT firstcol, secondcol, thirdcol, fourthcol,
     ROW_NUMBER() OVER (PARTITION BY firstcol ORDER BY firstcol) rownum
  FROM my_table
)
SELECT
  CASE rownum
    WHEN 1 THEN firstcol
    ELSE ''
  END AS firstcol,
  secondcol,
  thirdcol,
  fourthcol
FROM CTE_result;

因为我们 partitioning 它比 firstcol,我们将有 1 次出现和 >1 次出现。因此我们使用 CASE WHEN 并使用 ROWNUM 来获得想要的结果。

db<>fiddle link here