如果列值重复且为空,如何仅显示一次列值,直到 sql 中出现不同的值 [英] how to show column value only one time if it is repeated and blank until different value comes in sql

查看:20
本文介绍了如果列值重复且为空,如何仅显示一次列值,直到 sql 中出现不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含类别和预算类型列的简单表格如上图所示,我只想显示 CATEGORY 一次并代替接下来的两个 CATEGORY使用select *查询时会出现这样的空白.

we have a simple table containing category and budget type column as show in the picture above I want to show CATEGORY only once and in place of next two CATEGORY there will be blank like this when I use select * query.

推荐答案

可以这样做:

SELECT
    CASE WHEN ROW_NUMBER() OVER(PARTITION BY Category ORDER BY BudgetType) = 1 
    THEN Category ELSE NULL END AS 'Category Caption'
    , Category
    , BudgetType
FROM yourTable
ORDER BY Category, BudgetType

但正如 Mikael 所提到的,在大多数情况下,这真的不是应该做的.试想一下,例如,客户可能想要更改类别内的排序,那么每个类别中的第一"行将是不同的.

But as Mikael mentioned, this is - in most cases - really not the way it should be done. Just think that the client may, for example, want to change the sorting within the categories, then the 'first' row within each category will be a different one.

这篇关于如果列值重复且为空,如何仅显示一次列值,直到 sql 中出现不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆