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
299 views
in Technique[技术] by (71.8m points)

sql - shows blanks for repeating values in a result set

In the query below, for o.EventSetName, o.EventSetDisplay, o.EventSetDescription - any time the 3 columns in the result have duplicate ROWS - only the first such row should be shown and blank for the rest of the duplicate rows.....

here is the sql:

 Select  distinct top 100000 o.EventSetName,       
                             o.EventSetDisplay,
                             o.EventSetDescription,
                             o.ChildSetName,
                             ROW_NUMBER() Over (Order By f.ChildSetName) RN,
                             f.DocumentDispSequence,
                             f.SectionDispSequence,
                             o.ObsSetDispSequence,
                             null                          
                      From   ##ObsSetLevel o,
                             ##Final f
                      Where  f.ChildSetName = o.EventSetName and 
                             o.EventSetName = @variableName
                      Order By RN asc, f.DocumentDispSequence asc, f.SectionDispSequence asc, o.ObsSetDispSequence asc

I dont have a reporting tool so for now a lot of the reporting logic needs to be done in the stored proc itself...

So instead of:

val 1   val2    val3  val7
val 1   val2    val3  val8
val 1   val2    val3  val 10
val 1   val2    val3  x
val 1   val2    val3  y

I should get the resullt with blanks for the first 3 columns for rows 2,3,4 and 5

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Wrap your existing query in CTE adding ROW_NUMBER OVER PARTITION BY your columns, which will create RNs for each group of values. In outer query just use CASE to select values where GRP_RN = 1 and empty string otherwise.

WITH CTE AS 
(
    Select  distinct top 100000 
        o.EventSetName,       
        o.EventSetDisplay,
        o.EventSetDescription,
        o.ChildSetName,
        ROW_NUMBER() Over (Order By f.ChildSetName) RN,
        f.DocumentDispSequence,
        f.SectionDispSequence,
        o.ObsSetDispSequence,
        null  as NullColumnNeedsName,
        ROW_NUMBER() OVER (PARTITION BY o.EventSetName, o.EventSetDisplay,o.EventSetDescription ORDER BY f.ChildSetName) GRP_RN
    From   ##ObsSetLevel o,
    INNER JOIN ##Final f ON f.ChildSetName = o.EventSetName and o.EventSetName = @variableName
)
SELECT
    CASE WHEN GRP_RN = 1 THEN o.EventSetName ELSE '' AS EventSetName,
    CASE WHEN GRP_RN = 1 THEN o.EventSetDisplay ELSE '' AS EventSetDisplay,
    CASE WHEN GRP_RN = 1 THEN o.EventSetDescription ELSE '' AS EventSetDescription,
    other columns
FROM CTE  
Order By RN asc, DocumentDispSequence asc, SectionDispSequence asc, o.ObsSetDispSequence asc

PS: I have also corrected your use of old-style joins. That usage is outdated more than 20 years ago with introduction of SQL-92 standards. You should avoid using them.


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

...