SQL Server 2008 PIVOT and JOIN












1















I have 3 tables with data:



TABLE 1 : Type



IdType  LibelleType
-------------------
1 Type1
2 Type2


TABLE 2 : Procedure



IdProcedur    LibelleProcedure
-------------------------------
1 Procedure1
2 Procedure2


TABLE 3 : INFORMATION



IdInfo    IdType    IdProcedure    InfoValue
------------------------------------------------
1 1 1 11
2 1 2 12
3 2 1 21
4 2 2 22


I need a query which will produce this output:



         Procedure1   Procedure2
Type1 11 12
Type2 21 22


Thank you !










share|improve this question

























  • Can you please do some formatting. Its difficult to understand otherwise.

    – WhoamI
    Nov 25 '18 at 0:35











  • I edited the post .. hope can help

    – Abdou303
    Nov 25 '18 at 0:58











  • See the answer below. Let me know if this helps.

    – WhoamI
    Nov 25 '18 at 1:13
















1















I have 3 tables with data:



TABLE 1 : Type



IdType  LibelleType
-------------------
1 Type1
2 Type2


TABLE 2 : Procedure



IdProcedur    LibelleProcedure
-------------------------------
1 Procedure1
2 Procedure2


TABLE 3 : INFORMATION



IdInfo    IdType    IdProcedure    InfoValue
------------------------------------------------
1 1 1 11
2 1 2 12
3 2 1 21
4 2 2 22


I need a query which will produce this output:



         Procedure1   Procedure2
Type1 11 12
Type2 21 22


Thank you !










share|improve this question

























  • Can you please do some formatting. Its difficult to understand otherwise.

    – WhoamI
    Nov 25 '18 at 0:35











  • I edited the post .. hope can help

    – Abdou303
    Nov 25 '18 at 0:58











  • See the answer below. Let me know if this helps.

    – WhoamI
    Nov 25 '18 at 1:13














1












1








1








I have 3 tables with data:



TABLE 1 : Type



IdType  LibelleType
-------------------
1 Type1
2 Type2


TABLE 2 : Procedure



IdProcedur    LibelleProcedure
-------------------------------
1 Procedure1
2 Procedure2


TABLE 3 : INFORMATION



IdInfo    IdType    IdProcedure    InfoValue
------------------------------------------------
1 1 1 11
2 1 2 12
3 2 1 21
4 2 2 22


I need a query which will produce this output:



         Procedure1   Procedure2
Type1 11 12
Type2 21 22


Thank you !










share|improve this question
















I have 3 tables with data:



TABLE 1 : Type



IdType  LibelleType
-------------------
1 Type1
2 Type2


TABLE 2 : Procedure



IdProcedur    LibelleProcedure
-------------------------------
1 Procedure1
2 Procedure2


TABLE 3 : INFORMATION



IdInfo    IdType    IdProcedure    InfoValue
------------------------------------------------
1 1 1 11
2 1 2 12
3 2 1 21
4 2 2 22


I need a query which will produce this output:



         Procedure1   Procedure2
Type1 11 12
Type2 21 22


Thank you !







sql-server join pivot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:23









marc_s

578k12911171263




578k12911171263










asked Nov 25 '18 at 0:30









Abdou303Abdou303

84




84













  • Can you please do some formatting. Its difficult to understand otherwise.

    – WhoamI
    Nov 25 '18 at 0:35











  • I edited the post .. hope can help

    – Abdou303
    Nov 25 '18 at 0:58











  • See the answer below. Let me know if this helps.

    – WhoamI
    Nov 25 '18 at 1:13



















  • Can you please do some formatting. Its difficult to understand otherwise.

    – WhoamI
    Nov 25 '18 at 0:35











  • I edited the post .. hope can help

    – Abdou303
    Nov 25 '18 at 0:58











  • See the answer below. Let me know if this helps.

    – WhoamI
    Nov 25 '18 at 1:13

















Can you please do some formatting. Its difficult to understand otherwise.

– WhoamI
Nov 25 '18 at 0:35





Can you please do some formatting. Its difficult to understand otherwise.

– WhoamI
Nov 25 '18 at 0:35













I edited the post .. hope can help

– Abdou303
Nov 25 '18 at 0:58





I edited the post .. hope can help

– Abdou303
Nov 25 '18 at 0:58













See the answer below. Let me know if this helps.

– WhoamI
Nov 25 '18 at 1:13





See the answer below. Let me know if this helps.

– WhoamI
Nov 25 '18 at 1:13












1 Answer
1






active

oldest

votes


















0














Please check below. The last select does the pivot.(My earlier queries are just to prepare the dataset)



        create table Type
(IDType INT,Label VARCHAR(100)
)
create table [PROC]
(IDProc INT, LabelProc varchar(100))
Create table INFO
(IDInfo INT, IDType INT, IDProc INT, INFOValue INT)

insert into Type
values(1,'Type1'),(2,'Type2')

insert into [PROC]
values(1,'Proc1'),(2,'Proc2')

insert into InFO
values(1,1,1,11),(2,1,2,12),(3,2,1,21),(4,2,2,22)

select * from Type
select * from [PROC]
select * from info

declare @labelforprocs varchar(max) = ''
,@sql NVARCHAR(MAX)
select @labelforprocs = CONCAT(@labelforprocs,QUOTENAME(LabelProc),',') from [PROC]
select @labelforprocs = LEFT(@labelforprocs,LEN(@labelforprocs)-1)
SET @sql =
'select * from
(
select T.Label,P.LabelProc,I.INFOValue from INFO I
INNER JOIN [PROC] P
ON I.IDPROC = P.IDProc
INNER JOIN TYPE T
on T.IDType = I.IDType
)SRC
PIVOT
(MAX(INFOValue)
FOR LabelProc in (' + @labelforprocs +
'))piv'
EXEC sp_executesql @sql


enter image description here






share|improve this answer


























  • Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

    – Abdou303
    Nov 25 '18 at 1:17











  • Edited the answer to do it dynamically using dynamic sql. Please check.

    – WhoamI
    Nov 25 '18 at 1:36











  • I check it .. thank you so much

    – Abdou303
    Nov 25 '18 at 15:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463659%2fsql-server-2008-pivot-and-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Please check below. The last select does the pivot.(My earlier queries are just to prepare the dataset)



        create table Type
(IDType INT,Label VARCHAR(100)
)
create table [PROC]
(IDProc INT, LabelProc varchar(100))
Create table INFO
(IDInfo INT, IDType INT, IDProc INT, INFOValue INT)

insert into Type
values(1,'Type1'),(2,'Type2')

insert into [PROC]
values(1,'Proc1'),(2,'Proc2')

insert into InFO
values(1,1,1,11),(2,1,2,12),(3,2,1,21),(4,2,2,22)

select * from Type
select * from [PROC]
select * from info

declare @labelforprocs varchar(max) = ''
,@sql NVARCHAR(MAX)
select @labelforprocs = CONCAT(@labelforprocs,QUOTENAME(LabelProc),',') from [PROC]
select @labelforprocs = LEFT(@labelforprocs,LEN(@labelforprocs)-1)
SET @sql =
'select * from
(
select T.Label,P.LabelProc,I.INFOValue from INFO I
INNER JOIN [PROC] P
ON I.IDPROC = P.IDProc
INNER JOIN TYPE T
on T.IDType = I.IDType
)SRC
PIVOT
(MAX(INFOValue)
FOR LabelProc in (' + @labelforprocs +
'))piv'
EXEC sp_executesql @sql


enter image description here






share|improve this answer


























  • Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

    – Abdou303
    Nov 25 '18 at 1:17











  • Edited the answer to do it dynamically using dynamic sql. Please check.

    – WhoamI
    Nov 25 '18 at 1:36











  • I check it .. thank you so much

    – Abdou303
    Nov 25 '18 at 15:57
















0














Please check below. The last select does the pivot.(My earlier queries are just to prepare the dataset)



        create table Type
(IDType INT,Label VARCHAR(100)
)
create table [PROC]
(IDProc INT, LabelProc varchar(100))
Create table INFO
(IDInfo INT, IDType INT, IDProc INT, INFOValue INT)

insert into Type
values(1,'Type1'),(2,'Type2')

insert into [PROC]
values(1,'Proc1'),(2,'Proc2')

insert into InFO
values(1,1,1,11),(2,1,2,12),(3,2,1,21),(4,2,2,22)

select * from Type
select * from [PROC]
select * from info

declare @labelforprocs varchar(max) = ''
,@sql NVARCHAR(MAX)
select @labelforprocs = CONCAT(@labelforprocs,QUOTENAME(LabelProc),',') from [PROC]
select @labelforprocs = LEFT(@labelforprocs,LEN(@labelforprocs)-1)
SET @sql =
'select * from
(
select T.Label,P.LabelProc,I.INFOValue from INFO I
INNER JOIN [PROC] P
ON I.IDPROC = P.IDProc
INNER JOIN TYPE T
on T.IDType = I.IDType
)SRC
PIVOT
(MAX(INFOValue)
FOR LabelProc in (' + @labelforprocs +
'))piv'
EXEC sp_executesql @sql


enter image description here






share|improve this answer


























  • Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

    – Abdou303
    Nov 25 '18 at 1:17











  • Edited the answer to do it dynamically using dynamic sql. Please check.

    – WhoamI
    Nov 25 '18 at 1:36











  • I check it .. thank you so much

    – Abdou303
    Nov 25 '18 at 15:57














0












0








0







Please check below. The last select does the pivot.(My earlier queries are just to prepare the dataset)



        create table Type
(IDType INT,Label VARCHAR(100)
)
create table [PROC]
(IDProc INT, LabelProc varchar(100))
Create table INFO
(IDInfo INT, IDType INT, IDProc INT, INFOValue INT)

insert into Type
values(1,'Type1'),(2,'Type2')

insert into [PROC]
values(1,'Proc1'),(2,'Proc2')

insert into InFO
values(1,1,1,11),(2,1,2,12),(3,2,1,21),(4,2,2,22)

select * from Type
select * from [PROC]
select * from info

declare @labelforprocs varchar(max) = ''
,@sql NVARCHAR(MAX)
select @labelforprocs = CONCAT(@labelforprocs,QUOTENAME(LabelProc),',') from [PROC]
select @labelforprocs = LEFT(@labelforprocs,LEN(@labelforprocs)-1)
SET @sql =
'select * from
(
select T.Label,P.LabelProc,I.INFOValue from INFO I
INNER JOIN [PROC] P
ON I.IDPROC = P.IDProc
INNER JOIN TYPE T
on T.IDType = I.IDType
)SRC
PIVOT
(MAX(INFOValue)
FOR LabelProc in (' + @labelforprocs +
'))piv'
EXEC sp_executesql @sql


enter image description here






share|improve this answer















Please check below. The last select does the pivot.(My earlier queries are just to prepare the dataset)



        create table Type
(IDType INT,Label VARCHAR(100)
)
create table [PROC]
(IDProc INT, LabelProc varchar(100))
Create table INFO
(IDInfo INT, IDType INT, IDProc INT, INFOValue INT)

insert into Type
values(1,'Type1'),(2,'Type2')

insert into [PROC]
values(1,'Proc1'),(2,'Proc2')

insert into InFO
values(1,1,1,11),(2,1,2,12),(3,2,1,21),(4,2,2,22)

select * from Type
select * from [PROC]
select * from info

declare @labelforprocs varchar(max) = ''
,@sql NVARCHAR(MAX)
select @labelforprocs = CONCAT(@labelforprocs,QUOTENAME(LabelProc),',') from [PROC]
select @labelforprocs = LEFT(@labelforprocs,LEN(@labelforprocs)-1)
SET @sql =
'select * from
(
select T.Label,P.LabelProc,I.INFOValue from INFO I
INNER JOIN [PROC] P
ON I.IDPROC = P.IDProc
INNER JOIN TYPE T
on T.IDType = I.IDType
)SRC
PIVOT
(MAX(INFOValue)
FOR LabelProc in (' + @labelforprocs +
'))piv'
EXEC sp_executesql @sql


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 1:35

























answered Nov 25 '18 at 1:03









WhoamIWhoamI

162129




162129













  • Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

    – Abdou303
    Nov 25 '18 at 1:17











  • Edited the answer to do it dynamically using dynamic sql. Please check.

    – WhoamI
    Nov 25 '18 at 1:36











  • I check it .. thank you so much

    – Abdou303
    Nov 25 '18 at 15:57



















  • Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

    – Abdou303
    Nov 25 '18 at 1:17











  • Edited the answer to do it dynamically using dynamic sql. Please check.

    – WhoamI
    Nov 25 '18 at 1:36











  • I check it .. thank you so much

    – Abdou303
    Nov 25 '18 at 15:57

















Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

– Abdou303
Nov 25 '18 at 1:17





Thank you so much for your answer .. please can you help me to select TYPE and PROCEDURE tables values dynamically ?

– Abdou303
Nov 25 '18 at 1:17













Edited the answer to do it dynamically using dynamic sql. Please check.

– WhoamI
Nov 25 '18 at 1:36





Edited the answer to do it dynamically using dynamic sql. Please check.

– WhoamI
Nov 25 '18 at 1:36













I check it .. thank you so much

– Abdou303
Nov 25 '18 at 15:57





I check it .. thank you so much

– Abdou303
Nov 25 '18 at 15:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463659%2fsql-server-2008-pivot-and-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'