Pivot-table over multiple tables

Hello,

We would like to have an pivot-ouput, but it's coming from 3 tables

We have the following tablestructure (by design, we cannot change it)

UP (with 2 columns ID and Description)
1   FirstUp
2   SecondUp
...

SampleType (ID, Descr, ...)
10   SampleA
20   SampleB      
30   SampleC
...

SampleUp (ID_UP, ID_SAMPLE, TYPE)
1   10    W
2   10    W
1   20    W
2   30    W
....

We would like to have the following outcome with above example
                       FirstUp          SecondUp  ....
SampleA          1 (or yes)        1
SampleB          1                      0
SampleC          0                      1
SampleD          0                     0
...
LVL 53
DhaestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
It's a simple PIVOT:

DECLARE @UP TABLE
    (
        ID INT ,
        [Description] VARCHAR(255)
    );
INSERT INTO @UP ( ID ,
                  Description )
VALUES ( 1, 'FirstUp' ) ,
       ( 2, 'SecondUp' );

DECLARE @SampleType TABLE
    (
        ID INT ,
        Descr VARCHAR(255)
    );
INSERT INTO @SampleType ( ID ,
                          Descr )
VALUES ( 10, 'SampleA' ) ,
       ( 20, 'SampleB' ) ,
       ( 30, 'SampleC' );


DECLARE @SampleUp TABLE
    (
        ID_UP INT ,
        ID_SAMPLE INT ,
        [TYPE] CHAR(1)
    );
INSERT INTO @SampleUp ( ID_UP ,
                        ID_SAMPLE ,
                        TYPE )
VALUES ( 1, 10, 'W' ) ,
       ( 2, 10, 'W' ) ,
       ( 1, 20, 'W' ) ,
       ( 2, 30, 'W' );

WITH [Data]
AS ( SELECT U.ID ,
            U.Description ,
            ST.Descr
     FROM   @UP U
            INNER JOIN @SampleUp SU ON U.ID = SU.ID_UP
            INNER JOIN @SampleType ST ON SU.ID_SAMPLE = ST.ID )
SELECT *
FROM   [Data] D
    PIVOT (   COUNT(ID)
              FOR Description IN ( FirstUp, SecondUp )) P;

Open in new window

0
DhaestAuthor Commented:
Hello,

Thanks for that answer.
I changed a little bit to get to my results

WITH [Data]
AS ( SELECT U.UP ,
            U.DESCRIPTION ,
            ST.DESCRIPTION as descr
     FROM   RndSuite.RndtUp U
            INNER JOIN RndSuite.RndtStAc SU ON U.UP = SU.UP
            INNER JOIN RndSuite.RndtSt ST ON SU.ST = ST.ST and SU.VERSION = ST.VERSION
                  WHERE ST.ST = 3 )
SELECT *
FROM   [Data] D
    PIVOT (   COUNT(DESCRIPTION)
              FOR UP IN ( [1], [4] )) P;


Still have one question. Now I probably have to add each column in the FOR UP IN ...

Is there a way to get here a "dynamic" list, namely all the values from another table, namely all the UP-id ' s .. (the @up U table)

So instead of
              FOR UP IN ( [1], [2], [3], [4], [5],... )) P;

FOR UP IN (select id's)
0
ste5anSenior DeveloperCommented:
Is it really dynamic? Thus is RndtUp variant?

Using dynamic SQL require persistent tables. E.g.

USE tempdb;
GO

CREATE TABLE UP
    (
        ID INT ,
        [Description] VARCHAR(255)
    );
INSERT INTO dbo.UP ( ID ,
                     Description )
VALUES ( 1, 'FirstUp' ) ,
       ( 2, 'SecondUp' );

CREATE TABLE SampleType
    (
        ID INT ,
        Descr VARCHAR(255)
    );
INSERT INTO dbo.SampleType ( ID ,
                             Descr )
VALUES ( 10, 'SampleA' ) ,
       ( 20, 'SampleB' ) ,
       ( 30, 'SampleC' );


CREATE TABLE SampleUp
    (
        ID_UP INT ,
        ID_SAMPLE INT ,
        [TYPE] CHAR(1)
    );
INSERT INTO dbo.SampleUp ( ID_UP ,
                           ID_SAMPLE ,
                           TYPE )
VALUES ( 1, 10, 'W' ) ,
       ( 2, 10, 'W' ) ,
       ( 1, 20, 'W' ) ,
       ( 2, 30, 'W' );
GO

DECLARE @Columns NVARCHAR(MAX);
DECLARE @Sql NVARCHAR(MAX) = N'
WITH [Data]
AS ( SELECT U.ID ,
            ST.Descr
     FROM   Up U
        INNER JOIN SampleUp SU ON U.ID = SU.ID_UP
        INNER JOIN SampleType ST ON SU.ID_SAMPLE = ST.ID )
SELECT P.*
FROM   [Data] D
    PIVOT ( COUNT(D.ID) FOR D.ID IN ( @Columns )) P;
';

SET @Columns = STUFF((   SELECT ', ' + QUOTENAME(CAST(ID AS NVARCHAR(MAX)))
                         FROM   dbo.UP
                         FOR XML PATH('')) ,
                     1 ,
                     2 ,
                     '');

SET @Sql = REPLACE(@Sql, '@Columns', @Columns);
PRINT ( @Sql );
EXECUTE ( @Sql );
GO

DROP TABLE dbo.SampleType;
DROP TABLE dbo.SampleUp;
DROP TABLE dbo.UP;
GO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.