Sql group rows with same value, and put that value into header?

前端 未结 5 2047
梦毁少年i
梦毁少年i 2020-12-11 04:50

I want to group rows with SQL, my result set is following

name  size  date
data1  123  12/03/2009
data1 &nb

5条回答
  •  孤城傲影
    2020-12-11 05:32

    Shaping the data ahead of time simplifies things for the application developer, especially if all they're doing is displaying a static report with no interactive editing, sorting, or paging --a common enough occurrence.

    Lieven's solution (selecting the columns distinctively while inserting nulls for other columns, then checking for the nulls of the other columns) is the only solution here that actually works. Kind of.

    data1   NULL    NULL
    NULL    123 2011-05-24 19:42:29.577
    NULL    124 2011-05-24 19:42:29.577
    data2   NULL    NULL
    NULL    323 2011-05-24 19:42:29.577
    NULL    333 2011-05-24 19:42:29.577
    NULL    444 2011-05-24 19:42:29.577
    NULL    673 2011-05-24 19:42:29.577
    

    --unless the column you're nullalizing already has nulls, in which case we're back to square 0. Add one more row.

    INSERT INTO @Table VALUES ('data2', NULL, getdate())
    

    Now run the query again. Bummer dude.

    data1   NULL    NULL
    NULL    123 2011-05-24 19:53:36.437
    NULL    124 2011-05-24 19:53:36.437
    data2   NULL    NULL
    data2   NULL    2011-05-24 19:53:36.440
    NULL    323 2011-05-24 19:53:36.440
    NULL    333 2011-05-24 19:53:36.440
    NULL    444 2011-05-24 19:53:36.440
    

    Roux's proposed ROLLUP solution doesn't work at all, at least not in SQL Server. Actually, it makes things more worserer.

    data1   NULL    NULL
    data1   NULL    NULL
    data1   NULL    NULL
    data1   123 2011-05-24 20:16:26.693
    data1   124 2011-05-24 20:16:26.693
    data2   NULL    NULL
    data2   NULL    NULL
    data2   NULL    NULL
    data2   323 2011-05-24 20:16:26.693
    data2   333 2011-05-24 20:16:26.693
    data2   444 2011-05-24 20:16:26.693
    data2   673 2011-05-24 20:16:26.693
    

    Pitiș' ROWNUM solution may work in Oracle (I haven't tried it and there appears to a missing beginning parentheses), but the equivalent SQL Server code using ROW_NUMBER() OVER definitely taint working no good neither --about as well as my grammer and speling.

    SELECT 
        ROW_NUMBER() OVER(ORDER BY [name]) AS [rown]
        , name
        , ''
        , ''
    FROM @Table
    GROUP BY name
    
    UNION ALL
    
    SELECT ROW_NUMBER() OVER(ORDER BY [name]) + 1 AS [rown] , name, size, date 
    FROM @Table 
    

    produces

    data1   NULL    NULL
    data1   NULL    NULL
    data1   NULL    NULL
    data1   123 2011-05-24 20:16:26.693
    data1   124 2011-05-24 20:16:26.693
    data2   NULL    NULL
    data2   NULL    NULL
    data2   NULL    NULL
    data2   323 2011-05-24 20:16:26.693
    data2   333 2011-05-24 20:16:26.693
    data2   444 2011-05-24 20:16:26.693
    data2   673 2011-05-24 20:16:26.693
    

    As a software professional your job is to ensure billions of 1s and 0s line up at the right place, in the right order, at the right time. You know the details, often times down to an individual bit, are important.

    A half vast answer is worse than no answer at all because it wastes everyone's time. So... no offense intended because intentions are good, but please at least test your "solution" before posting it as a "solution".

    I would be perfect if I weren't so darned humble. And even I test.

提交回复
热议问题