问题
I need to put columns in rows only for the last columns of my query's results.
The query is :
select
dpp.CODE_PORTEFEUILLE,
dpr.SOCIETE_GESTION,
lgp.CODE_GERANT,
lgp.DEVISE_GERANT,
lgp.CODE_INTERVENANT
from
GP3DBA.DESCRIPTIF_PORTEFEUILLE dpp
join GP3DBA.DESCRIPTIF_PORT_REPORTING dpr on dpp.CODE_PORTEFEUILLE = dpr.CODE_PORTEFEUILLE
join GP3DBA.LIEN_GERANT_PORTEFEUILLE lgp on dpp.CODE_PORTEFEUILLE = lgp.CODE_PORTEFEUILLE
RESULT:
DESIRED RESULT:
I have tried to do it myself, but without any satisfying result.
回答1:
EDIT
I don't see an easy way to do this. You actual result is in #tmpResult in this example.
select CODE_PORTEFEUILLE, SOCIETE_GESTION, count(1) as 'number'
into #columnNumber
from #tmpResult
group by CODE_PORTEFEUILLE, SOCIETE_GESTION
declare @columnMaxNumber int;
SELECT @columnMaxNumber = MAX(number)
FROM #columnNumber
declare @tableSql = 'CREATE TABLE #finalResult(
CODE_PORTEFEUILLE int not null,
SOCIETE_GESTION varchar(10) not null'
declare @cnt int = 0;
WHILE @cnt < @columnMaxNumber
BEGIN
set @tableSql = @tableSql + ', CODE_GERANT' + @cnt + ' varchar(2)'
set @tableSql = @tableSql + ', DEVISE_GERANT' + @cnt + ' varchar(3)'
set @tableSql = @tableSql + ', CODE_INTERVENANT' + @cnt + ' varchar(3)'
SET @cnt = @cnt + 1;
END
set @tableSql = @tableSql + ')'
exec @tableSql
then you just have to inject data in this table
declare cursor_portefeuille CURSOR FOR
select * from #tmpResult /*order by fields you want to order by*/
declare @code_portefeuille int, @societe_gestion varchar(3), @code_gerant varchar(2), @device_gerant varchar(3), @code_intervenant varchar(3), @gerant_index int = 0, @last_portefeuille int = 0, @last_societe varchar(3) = ''
OPEN cursor_portefeuille
FETCH NEXT FROM cursor_portefeuille
INTO @code_portefeuille, @societe_gestion, @code_gerant, @device_gerant, @code_intervenant
WHILE @@FETCH_STATUS = 0
BEGIN
if(@last_portefeuille = @code_portefeuille and @last_societe = @societe_gestion)
begin
set @gerant_index = @gerant_index + 1
end
else
begin
set @last_portefeuille = @code_portefeuille
set @last_societe = @societe_gestion
set @gerant_index = 0
end
if exists(select 1 from #finalResult where CODE_PORTEFEUILLE = @code_portefeuille and SOCIETE_GESTION = @societe_gestion)
begin
declare @sqlQuery varchar(512) = 'update #finalResult
set CODE_GERANT' + @gerant_index + ' = @code_gerant,
DEVISE_GERANT' + @gerant_index + ' = @device_gerant,
CODE_INTERVENANT' + @gerant_index + ' = @code_intervenant
WHERE CODE_PORTEFEUILLE = @code_portefeuille and SOCIETE_GESTION = @societe_gestion'
exec @sqlQuery;
end
else
begin
insert into #finalResult(CODE_PORTEFEUILLE, SOCIETE_GESTION, CODE_GERANT0, DEVISE_GERANT0, CODE_INTERVENANT0)
values(@code_portefeuille, @societe_gestion, @code_gerant, @device_gerant, @code_intervenant)
end
FETCH NEXT FROM cursor_portefeuille
INTO @code_portefeuille, @societe_gestion, @code_gerant, @device_gerant, @code_intervenant
END
CLOSE cursor_portefeuille ;
DEALLOCATE cursor_portefeuille ;
SELECT * from #finalResult
/*Don't forget to drop temp tables */
Didn't try it, I have no actual case at reach but I know the logic works. But you should avoid doing this... It runs fairly slowly
来源:https://stackoverflow.com/questions/39390972/sql-server-pivot-only-on-a-part-of-the-result