Please note in Create Data
I have added row number
Create Data Script
drop table #name
go
create table #name (
ID int ,
Name varchar(100)
)
go
insert into #name select 1, 'jayaraj'
insert into #name select 2, 'Bala'
insert into #name select 3, 'sridhar'
go
drop table #marks
go
create table #Marks (
Id int,
Mark int
)
go
insert into #Marks select 1, 100
insert into #Marks select 1, 500
insert into #Marks select 1, 300
insert into #Marks select 2, 100
insert into #Marks select 2, 200
insert into #Marks select 1, 994
insert into #Marks select 1, 444
insert into #Marks select 3, 344
insert into #Marks select 3, 387
go
drop table def
go
select ROW_NUMBER() OVER(partition by id order by id) as rn,a.ID,a.Name,b.Mark into def from #name a join #Marks b
on a.ID = b.Id
go
SQL
SELECT id,
NAME,
[1],
[2],
[3],
[4],
[5]
FROM def
PIVOT ( Max(mark)
FOR [rn] IN ([1],
[2],
[3],
[4],
[5] ) ) AS p
ORDER BY 1,
2
Drop table if exists #t1
Drop table if exists #t2
Create table #t1 (id int, name varchar(20))
insert into #t1 values
(1,'jayaraj'),
(2,'Bala'),
(3,'sridhar')
Create table #t2 (colno int identity(1,1), id int, Mark int)
insert into #t2 values
(1 ,100),
(1 ,500),
(1 ,300),
(2 ,100),
(2 ,200),
(1, 994),
(1 ,444),
(3 ,344),
(3 ,387)
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ', Mark' + cast(RowNum as nvarchar(10))
FROM (
select row_number() over (partition by t2.id order by Colno) RowNum
from #t2 t2
) z
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ID, Name, ' + @cols + ' from
(
select t2.Id, Name, mark, ''Mark'' + cast(row_number() over (partition by t2.id order by Colno) as nvarchar(10)) RowNum
from #t2 t2
join #t1 t1
on t1.id = t2.id
) x
pivot
(
max(Mark)
for RowNum in (' + @cols + ')
) p
order by ID '
execute(@query)
SELECT @cols = STUFF((SELECT
',' + QUOTENAME(CASE
WHEN d.col = 'Montly_payment' THEN col + CAST(
seq AS
varchar(
10))
ELSE col + CAST(seq AS varchar(10))
END)
FROM (SELECT DISTINCT
ROW_NUMBER()
OVER (
PARTITION BY pmt.sesn_id
ORDER BY mpa_calc_mo_pmt_amt)
seq
FROM pmt(nolock) pmt
INNER JOIN [rpmt_schd_rslt] RPT
ON pmt.sesn_id = RPT.sesn_id
AND RPT.[rqst_sys_cd] = @RQST_SYS
AND RPT.[crea_ts] BETWEEN @startdate AND @enddate) t
CROSS APPLY (SELECT
'Montly_payment',
1
UNION ALL
SELECT
'Montly_payment_Due_Date',
2
UNION ALL
SELECT
'Montly_payment_Type',
3
UNION ALL
SELECT
'UN_PAID_ACCURIED_INTREST_AMOUNT',
4
UNION ALL
SELECT
'OUTSTANDING_PRINCIPAL_AMOUNT',
5
UNION ALL
SELECT
'TOTAL_INTREST_AMOUNT',
6
UNION ALL
SELECT
'TOTAL_PAID_AMOUNT',
7) d (col, so)
FOR xml PATH (''), TYPE)
.value('.', 'NVARCHAR(MAX)'), 1, 1,
'')
pmt.SESN_ID,
pmt.MPA_CALC_MO_PMT_AMT,
CAST( [pmt.MPA_CALC_PMT_DT] AS varchar) MPA_CALC_PMT_DT1,
pmt.MPA_CALC_PMT_TYPE_CD,
pmt.UNPD_ACCR_INT_AMT,
pmt.OUTS_PRIN_AMT,
pmt.TOT_INT_AMT,
pmt.TOT_PD_AMT,
row_number() over(partition by pmt.SESN_ID
order by MPA_CALC_MO_PMT_AMT) seq
from PMT(NOlock) pmt inner join [RPMT_SCHD_RSLT] RPT on pmt.SESN_ID = RPT.SESN_ID and RPT.RQST_SYS_CD = ''KBT'' and RPT.[CREA_TS] BETWEEN ''2018-09-07'' and ''2018-09-17''
) t
cross apply
(
select ''Montly_payment'', MPA_CALC_MO_PMT_AMT union all
select ''Montly_payment_Due_Date'', MPA_CALC_PMT_DT1 union all
select ''Montly_payment_Type '', MPA_CALC_PMT_TYPE_CD union all
select ''UN_PAID_ACCURIED_INTREST_AMOUNT'', UNPD_ACCR_INT_AMT union all
select ''OUTSTANDING_PRINCIPAL_AMOUNT'', OUTS_PRIN_AMT union all
select ''TOTAL_INTREST_AMOUNT'', TOT_INT_AMT union all
select ''TOTAL_PAID_AMOUNT'', TOT_PD_AMT
) c (col, value)
) x
pivot
(
max(value)
for col in ('
@cols + ')
) p '
EXECUTE Sp_executesql @query;
Following is the query i generated.
Having some issues in convert the date to varchar. MPA_CALC_PMT_DT is Date.
drop table #temp
create table #temp
(
ID int,
Name varchar(50),
Mark1 int,
Mark2 int,
Mark3 int,
Mark4 int,
Mark5 int
)
insert into #temp
select * from(
select row_number() over(partition by a.Name order by a.ID) as rownumber,a.ID,a.Name,s.mark from names a
inner join marks s
on a.ID = s.id
) m
pivot
(
sum(mark)
for [rownumber] in ([1],[2],[3],[4],[5])
)as pvt