Небольшая настройка того, что у вас уже есть, приведет вас туда.
Измените Max (GroupingID) на Max (Value), так как это то, что вы хотите развернуть.
Затем либо выполнитеMAX для всех поворотных полей и групп по GroupingId или используйте подзапрос, чтобы вернуть только GroupID, ColumnName, Value.
Вот рабочий пример:
DECLARE @CustomConfigItems TABLE
(
[CustomConfigname] NVARCHAR(100)
, [ColumnNumber] INT
, [ColumnName] NVARCHAR(100)
, [GroupingID] INT
, [Value] NVARCHAR(100)
);
INSERT INTO @CustomConfigItems (
[CustomConfigname]
, [ColumnNumber]
, [ColumnName]
, [GroupingID]
, [Value]
)
VALUES ( 'SAPTotals', '1', 'Batch_Reference', '35', 'BATCH001' )
, ( 'SAPTotals', '2', 'Bank_Account', '35', '72368837' )
, ( 'SAPTotals', '3', 'Sequence_No', '35', '123' )
, ( 'SAPTotals', '4', 'Entry_Date', '35', '29/01/2019' )
, ( 'SAPTotals', '5', 'Opening_Balance', '35', '2458.65' )
, ( 'SAPTotals', '6', 'Closing_Balance', '35', '2587.99' )
, ( 'SAPTotals', '7', 'Total_Credits', '35', '76' )
, ( 'SAPTotals', '8', 'Total_Debits', '35', '34' )
, ( 'SAPTotals', '9', 'Impex_No', '35', 'I907' )
, ( 'SAPTotals', '10', 'Export_Date', '35', '1/2/2019' )
, ( 'SAPTotals', '1', 'Batch_Reference', '36', 'BATCH002' )
, ( 'SAPTotals', '2', 'Bank_Account', '36', '72368837' )
, ( 'SAPTotals', '3', 'Sequence_No', '36', '124' )
, ( 'SAPTotals', '4', 'Entry_Date', '36', '30/01/2019' )
, ( 'SAPTotals', '5', 'Opening_Balance', '36', '2587.99' )
, ( 'SAPTotals', '6', 'Closing_Balance', '36', '2308.32' )
, ( 'SAPTotals', '7', 'Total_Credits', '36', '154' )
, ( 'SAPTotals', '8', 'Total_Debits', '36', '776' )
, ( 'SAPTotals', '9', 'Impex_No', '36', 'I907' )
, ( 'SAPTotals', '10', 'Export_Date', '36', '2/2/2019' )
, ( 'SAPTotals', '1', 'Batch_Reference', '37', 'BATCH003' )
, ( 'SAPTotals', '2', 'Bank_Account', '37', '72368837' )
, ( 'SAPTotals', '3', 'Sequence_No', '37', '125' )
, ( 'SAPTotals', '4', 'Entry_Date', '37', '31/01/2019' )
, ( 'SAPTotals', '5', 'Opening_Balance', '37', '2308.32' )
, ( 'SAPTotals', '6', 'Closing_Balance', '37', '4219.37' )
, ( 'SAPTotals', '7', 'Total_Credits', '37', '345' )
, ( 'SAPTotals', '8', 'Total_Debits', '37', '87' )
, ( 'SAPTotals', '9', 'Impex_No', '37', 'I907' )
, ( 'SAPTotals', '10', 'Export_Date', '37', '' )
, ( 'SAPTotals', '1', 'Batch_Reference', '38', 'BATCH566' )
, ( 'SAPTotals', '2', 'Bank_Account', '38', '73297699' )
, ( 'SAPTotals', '3', 'Sequence_No', '38', '344' )
, ( 'SAPTotals', '4', 'Entry_Date', '38', '30/01/2019' )
, ( 'SAPTotals', '5', 'Opening_Balance', '38', '1987.66' )
, ( 'SAPTotals', '6', 'Closing_Balance', '38', '4588.22' )
, ( 'SAPTotals', '7', 'Total_Credits', '38', '67' )
, ( 'SAPTotals', '8', 'Total_Debits', '38', '98' )
, ( 'SAPTotals', '9', 'Impex_No', '38', 'I907' )
, ( 'SAPTotals', '10', 'Export_Date', '38', '4/2/2019' )
, ( 'SAPTotals', '1', 'Batch_Reference', '39', 'BATCH_ABC' )
, ( 'SAPTotals', '2', 'Bank_Account', '39', '73297699' )
, ( 'SAPTotals', '3', 'Sequence_No', '39', '345' )
, ( 'SAPTotals', '4', 'Entry_Date', '39', '31/01/2019' )
, ( 'SAPTotals', '5', 'Opening_Balance', '39', '4588.22' )
, ( 'SAPTotals', '6', 'Closing_Balance', '39', '7777.25' )
, ( 'SAPTotals', '7', 'Total_Credits', '39', '999' )
, ( 'SAPTotals', '8', 'Total_Debits', '39', '2012' )
, ( 'SAPTotals', '9', 'Impex_No', '39', 'I907' )
, ( 'SAPTotals', '10', 'Export_Date', '39', '' );
SELECT [GroupingID]
, MAX([pvt].[Batch_Reference]) AS [Batch_Reference]
, MAX([pvt].[Bank_Account]) AS [Bank_Account]
, MAX([pvt].[Sequence_No]) AS [Sequence_No]
, MAX([pvt].[Entry_Date]) AS [Entry_Date]
, MAX([pvt].[Opening_Balance]) AS [OpeningBalane]
, MAX([pvt].[Closing_Balance]) AS [ClosingBalance]
, MAX([pvt].[Total_Credits]) AS [Total_Credits]
, MAX([pvt].[Total_Debits]) AS [Total_Debits]
, MAX([pvt].[Impex_No]) AS [Impex_No]
, MAX([pvt].[Export_Date]) AS [Export_Date]
FROM @CustomConfigItems
PIVOT (
MAX([Value]) --Pivot on this column
FOR [ColumnName] ----Make column where [Value] is in one of these
IN ( [Batch_Reference]
, [Bank_Account]
, [Sequence_No]
, [Entry_Date]
, [Opening_Balance]
, [Closing_Balance]
, [Total_Credits]
, [Total_Debits]
, [Impex_No]
, [Export_Date]
)
) [pvt]
GROUP BY [pvt].[GroupingID]
--or to simplify and not use all the aggregate functions
--use a sub query to only get GroupingID, Column and Value
SELECT *
FROM (
SELECT [GroupingID]
, [ColumnName]
, [Value]
FROM @CustomConfigItems
) AS [t]
PIVOT (
MAX([Value]) --Pivot on this column
FOR [ColumnName] ----Make column where [Value] is in one of these
IN ( [Batch_Reference], [Bank_Account], [Sequence_No], [Entry_Date], [Opening_Balance], [Closing_Balance], [Total_Credits], [Total_Debits], [Impex_No], [Export_Date] )
) [pvt];