2

Hi,

I have this data:

  ---------------------------------
|Masterarticle | childarticle   |
----------------------------------
|     12341       |   12345        |
|     12341       |   12346        |
|     12341       |   12347        |
|     12342       |   44875        |
|     12342       |   44876        |
----------------------------------

 

 

and this is my expected resultset:


|   12341   |    12342   |
-------------------------
|   12345   |    44875   |
-------------------------
|   12346   |    44876   |
-------------------------
|   12347   |    NULL   |
-------------------------
|   12347   |    NULL   |
-------------------------

 

I have this, but it shows me only one row, because of the aggregate max(childarticle). Is there any way, to query a dynamic table whitout aggregate?

            DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Masterarticle) 
                    from  table 
                    group by Masterarticle

            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select childarticle, Masterarticle
                from table 
            ) x
            pivot 
            (
                max(childarticle)
                for Masterarticle in (' + @cols + N')
            ) p '

exec sp_executesql @query;
3
  • 1
    So, what is your expected resultset here? Are those 2 sets of data both in your table? Commented Mar 27, 2018 at 9:53
  • give us the expected result you want Commented Mar 27, 2018 at 9:57
  • sorry, see above now Commented Mar 27, 2018 at 10:14

1 Answer 1

1

All you need to do is add a row_number to the query used for the pivot.

Then the PIVOT will group on that calculated row_number and return more that 1 row.

An example using a temporary table that only lives in the session:

IF OBJECT_ID('tempdb..#tmpTestTable') IS NOT NULL DROP TABLE #tmpTestTable;
create table #tmpTestTable (Masterarticle int, Childarticle int);

insert into #tmpTestTable (Masterarticle, Childarticle) values
(12341, 12345),(12341, 12346),(12341, 12347),
(12342, 44875),(12342, 44876);

DECLARE @cols AS NVARCHAR(MAX) = STUFF((SELECT ',' + QUOTENAME(Masterarticle) from  #tmpTestTable group by Masterarticle FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'');
--select @cols;

DECLARE @query AS NVARCHAR(MAX) = N'SELECT ' + @cols + N'
 from (
    select Masterarticle, Childarticle,
    row_number() over (partition by Masterarticle order by Childarticle) as RN
    from #tmpTestTable
) x
pivot (max(childarticle) for Masterarticle in (' + @cols + N')) pvt';

exec sp_executesql @query;

Returns:

12341  12342
-----  -----
12345  44875
12346  44876
12347  NULL
Sign up to request clarification or add additional context in comments.

2 Comments

ok, i can not modify the sql table. But everything from DECLARE works perfect! Many Thanks!
@Batchechooff Not sure why you would need to modify the sql table. I merely used a temporary table for demonstration purposes. Since that's easier for others to verify that it works. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.