查询隐藏重复的行列数据。不想删除重复的行

问题描述 投票:0回答:1

我想要一个 SQL Server 查询来隐藏重复的行列数据。我不想删除重复的行,而是有条件地将数据显示为空白。

当我运行这个 SQL 查询时:

select 
    [Vch No.], [Vch Type], [Vch Ref],
    [Date], [Party Name], [Sales Ledger],
    [Amt], [GST Ledger], [TaxAmount], [Total]
from 
    [AccountData]

我得到这个输出:

但是,我需要这种格式的输出:

在第二个打印屏幕中,我不知道如何显示 [Vch Ref]、[Date]、[Party Name]、[Sales Ledger]、[Amt] 和 Total 的值。

sql sql-server sql-server-2008
1个回答
2
投票

这看起来像是一个疯狂的解决方案,但您可以使用窗口函数

ROW_NUMBER()
并使用
CASE
表达式检查行号是否高于 1 来实现它,例如:

select 
    [Vch No.],
    [Vch Type],
    case when rn > 1 then '' else [Vch Ref] end as [Vch Ref],
    case when rn > 1 then '' else [Date] end as [Date],
    case when rn > 1 then '' else [Party Name] end as [Party Name],
    case when rn > 1 then '' else [Sales Ledger] end as [Sales Ledger],
    case when rn > 1 then '' else [Amt] end as [Amt],
    [GST Ledger],
    [TaxAmount],
    case when rn > 1 then '' else [Total] end as [Total]
from (  
    select 
        [Vch No.],
        [Vch Type],
        [Vch Ref],
        [Date],
        [Party Name],
        [Sales Ledger],
        [Amt],
        [GST Ledger],
        [TaxAmount],
        [Total], 
        row_number() over (partition by [Vch No.],[Vch Type],[Vch Ref],[Date],[Party Name],[Sales Ledger],[Amt],[GST Ledger],[TaxAmount],[Total] order by [Vch No.]) rn
    from [AccountData]
)x

查看数据类型,如果有

Amt
是INT,如果你想获得空白值,你应该将其转换为字符串。

© www.soinside.com 2019 - 2024. All rights reserved.