Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

sql server - how to show column value only one time if it is repeated and blank until different value comes in sql

enter image description here

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.

enter image description here

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You could do it like this:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...