USE [AAB]
GO
/****** Object: StoredProcedure [dbo].[SP_GET_DOWNLOAD_MARKET_PRICE] Script Date: 3/9/2017 9:02:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andri Kurniawan
-- Create date: 9 Desember 2016
-- Description: Get Data for Data Donwload Market Price [Vehicle Market Price Application Cisadane]
-- BRD : 0269 URF 2016 - Vehicle Market Price
-- =============================================
CREATE PROCEDURE [dbo].[SP_GET_DOWNLOAD_MARKET_PRICE]
@brandID AS VARCHAR(50) = '' ,
@modelID AS VARCHAR(50) = ''
AS
BEGIN
DECLARE @tsqlUMP VARCHAR(MAX) = '';
DECLARE @tsqlYearHeader VARCHAR(MAX) = '';
DECLARE @tsqlYearPivot VARCHAR(MAX) = '';
DECLARE @maxyear INT = 0;
DECLARE @minyear INT = 0;
SET @maxyear = YEAR(GETDATE());
SET @minyear = YEAR(GETDATE()) - 14;
DECLARE @mxP INT = @maxyear; -- mxP = Maximum Year For Dynamic Year in Pivot
DECLARE @mxH INT = @maxyear; -- mnH = Minimum Year For Dynamic Year in Header
DECLARE @mnP INT = @minyear; -- mnP = Maximum Year For Dynamic Year in Pivot
DECLARE @mnH INT = @minyear; -- mnH = Maximum Year For Dynamic Year in Header
DECLARE @Id INT= 0;
if @brandID='All'
begin
set @brandID =''
end
IF @modelID = 'All'
BEGIN
set @modelID=''
END
CREATE TABLE #andriTemp
(
Code NVARCHAR(50) ,
Tahun INT ,
Harga DECIMAL(11,2)
)
INSERT INTO #andriTemp
( Code ,
Tahun ,
Harga
)
SELECT Code ,
Tahun ,
Harga
FROM [SFN_GET_DATADOWNLOAD_MARKET_PRICE](@brandID, @modelID, '000001')
WHILE @mxH >= @mnH
BEGIN
SET @id = @Id + 1
SET @tsqlYearHeader = @tsqlYearHeader + 'coalesce(['
+ CAST(@mxH AS VARCHAR) + '], 0) AS y'
+ CONVERT(VARCHAR(10), @id) + ',';
SET @mxH = @mxH - 1;
END
SELECT @tsqlYearHeader = SUBSTRING(@tsqlYearHeader, 0,
LEN(@tsqlYearHeader))
SET @tsqlUMP = @tsqlUMP + 'Select Code, ' + @tsqlYearHeader
+ ' from ('
SET @tsqlUMP = @tsqlUMP
+ 'select Code, tahun, harga from #andriTemp'
SET @tsqlUMP = @tsqlUMP + ') src pivot (sum(harga) for tahun in ('
WHILE @mxP >= @mnP
BEGIN
IF ( @mxP = @mnP )
BEGIN
SET @tsqlYearPivot = @tsqlYearPivot + '['
+ CAST(@mxP AS VARCHAR) + '] ';
END
ELSE
BEGIN
SET @tsqlYearPivot = @tsqlYearPivot + '['
+ CAST(@mxP AS VARCHAR) + '], ';
END
SET @mxP = @mxP - 1;
END
SET @tsqlUMP = @tsqlUMP + @tsqlYearPivot;
SET @tsqlUMP = @tsqlUMP + ')) piv'
-- PRINT ( @tsqlUMP )
SET @tsqlUMP = 'SELECT aa.*, replace(COALESCE(NULLIF(s.Series,''''),''N/A''),''<None>'',''N/A'') as Series
From Mst_Vehicle AS S
RIGHT OUTER JOIN (' + @tsqlUMP + ') AS aa ON S.Vehicle_code = aa.Code'
PRINT ( @tsqlUMP )
CREATE TABLE #TEMP
(
code VARCHAR(100) ,
y1 VARCHAR(100) ,
y2 VARCHAR(100) ,
y3 VARCHAR(100) ,
y4 VARCHAR(100) ,
y5 VARCHAR(100) ,
y6 VARCHAR(100) ,
y7 VARCHAR(100) ,
y8 VARCHAR(100) ,
y9 VARCHAR(100) ,
y10 VARCHAR(100) ,
y11 VARCHAR(100) ,
y12 VARCHAR(100) ,
y13 VARCHAR(100) ,
y14 VARCHAR(100) ,
y15 VARCHAR(100) ,
Series VARCHAR(100) ,
y1x VARCHAR(10) ,
y2x VARCHAR(10) ,
y3x VARCHAR(10) ,
y4x VARCHAR(10) ,
y5x VARCHAR(10) ,
y6x VARCHAR(10) ,
y7x VARCHAR(10) ,
y8x VARCHAR(10) ,
y9x VARCHAR(10) ,
y10x VARCHAR(10) ,
y11x VARCHAR(10) ,
y12x VARCHAR(10) ,
y13x VARCHAR(10) ,
y14x VARCHAR(10) ,
y15x VARCHAR(10) ,
)
INSERT INTO #TEMP
( code ,
y1 ,
y2 ,
y3 ,
y4 ,
y5 ,
y6 ,
y7 ,
y8 ,
y9 ,
y10 ,
y11 ,
y12 ,
y13 ,
y14 ,
Y15 ,
Series
)
EXECUTE ( @tsqlUMP
)
UPDATE #TEMP
SET y1x = CAST(YEAR(GETDATE()) AS VARCHAR(10)) ,
y2x = CAST(YEAR(GETDATE()) - 1 AS VARCHAR(10)) ,
y3x = CAST(YEAR(GETDATE()) - 2 AS VARCHAR(10)) ,
y4x = CAST(YEAR(GETDATE()) - 3 AS VARCHAR(10)) ,
y5x = CAST(YEAR(GETDATE()) - 4 AS VARCHAR(10)) ,
y6x = CAST(YEAR(GETDATE()) - 5 AS VARCHAR(10)) ,
y7x = CAST(YEAR(GETDATE()) - 6 AS VARCHAR(10)) ,
y8x = CAST(YEAR(GETDATE()) - 7 AS VARCHAR(10)) ,
y9x = CAST(YEAR(GETDATE()) - 8 AS VARCHAR(10)) ,
y10x = CAST(YEAR(GETDATE()) - 9 AS VARCHAR(10)) ,
y11x = CAST(YEAR(GETDATE()) - 10 AS VARCHAR(10)) ,
y12x = CAST(YEAR(GETDATE()) - 11 AS VARCHAR(10)) ,
y13x = CAST(YEAR(GETDATE()) - 12 AS VARCHAR(10)) ,
y14x = CAST(YEAR(GETDATE()) - 13 AS VARCHAR(10)) ,
y15x = CAST(YEAR(GETDATE()) - 14 AS VARCHAR(10))
SELECT *
FROM #TEMP
DROP TABLE #TEMP
DROP TABLE #andriTemp
END
GO
GO
/****** Object: StoredProcedure [dbo].[SP_GET_DOWNLOAD_MARKET_PRICE] Script Date: 3/9/2017 9:02:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andri Kurniawan
-- Create date: 9 Desember 2016
-- Description: Get Data for Data Donwload Market Price [Vehicle Market Price Application Cisadane]
-- BRD : 0269 URF 2016 - Vehicle Market Price
-- =============================================
CREATE PROCEDURE [dbo].[SP_GET_DOWNLOAD_MARKET_PRICE]
@brandID AS VARCHAR(50) = '' ,
@modelID AS VARCHAR(50) = ''
AS
BEGIN
DECLARE @tsqlUMP VARCHAR(MAX) = '';
DECLARE @tsqlYearHeader VARCHAR(MAX) = '';
DECLARE @tsqlYearPivot VARCHAR(MAX) = '';
DECLARE @maxyear INT = 0;
DECLARE @minyear INT = 0;
SET @maxyear = YEAR(GETDATE());
SET @minyear = YEAR(GETDATE()) - 14;
DECLARE @mxP INT = @maxyear; -- mxP = Maximum Year For Dynamic Year in Pivot
DECLARE @mxH INT = @maxyear; -- mnH = Minimum Year For Dynamic Year in Header
DECLARE @mnP INT = @minyear; -- mnP = Maximum Year For Dynamic Year in Pivot
DECLARE @mnH INT = @minyear; -- mnH = Maximum Year For Dynamic Year in Header
DECLARE @Id INT= 0;
if @brandID='All'
begin
set @brandID =''
end
IF @modelID = 'All'
BEGIN
set @modelID=''
END
CREATE TABLE #andriTemp
(
Code NVARCHAR(50) ,
Tahun INT ,
Harga DECIMAL(11,2)
)
INSERT INTO #andriTemp
( Code ,
Tahun ,
Harga
)
SELECT Code ,
Tahun ,
Harga
FROM [SFN_GET_DATADOWNLOAD_MARKET_PRICE](@brandID, @modelID, '000001')
WHILE @mxH >= @mnH
BEGIN
SET @id = @Id + 1
SET @tsqlYearHeader = @tsqlYearHeader + 'coalesce(['
+ CAST(@mxH AS VARCHAR) + '], 0) AS y'
+ CONVERT(VARCHAR(10), @id) + ',';
SET @mxH = @mxH - 1;
END
SELECT @tsqlYearHeader = SUBSTRING(@tsqlYearHeader, 0,
LEN(@tsqlYearHeader))
SET @tsqlUMP = @tsqlUMP + 'Select Code, ' + @tsqlYearHeader
+ ' from ('
SET @tsqlUMP = @tsqlUMP
+ 'select Code, tahun, harga from #andriTemp'
SET @tsqlUMP = @tsqlUMP + ') src pivot (sum(harga) for tahun in ('
WHILE @mxP >= @mnP
BEGIN
IF ( @mxP = @mnP )
BEGIN
SET @tsqlYearPivot = @tsqlYearPivot + '['
+ CAST(@mxP AS VARCHAR) + '] ';
END
ELSE
BEGIN
SET @tsqlYearPivot = @tsqlYearPivot + '['
+ CAST(@mxP AS VARCHAR) + '], ';
END
SET @mxP = @mxP - 1;
END
SET @tsqlUMP = @tsqlUMP + @tsqlYearPivot;
SET @tsqlUMP = @tsqlUMP + ')) piv'
-- PRINT ( @tsqlUMP )
SET @tsqlUMP = 'SELECT aa.*, replace(COALESCE(NULLIF(s.Series,''''),''N/A''),''<None>'',''N/A'') as Series
From Mst_Vehicle AS S
RIGHT OUTER JOIN (' + @tsqlUMP + ') AS aa ON S.Vehicle_code = aa.Code'
PRINT ( @tsqlUMP )
CREATE TABLE #TEMP
(
code VARCHAR(100) ,
y1 VARCHAR(100) ,
y2 VARCHAR(100) ,
y3 VARCHAR(100) ,
y4 VARCHAR(100) ,
y5 VARCHAR(100) ,
y6 VARCHAR(100) ,
y7 VARCHAR(100) ,
y8 VARCHAR(100) ,
y9 VARCHAR(100) ,
y10 VARCHAR(100) ,
y11 VARCHAR(100) ,
y12 VARCHAR(100) ,
y13 VARCHAR(100) ,
y14 VARCHAR(100) ,
y15 VARCHAR(100) ,
Series VARCHAR(100) ,
y1x VARCHAR(10) ,
y2x VARCHAR(10) ,
y3x VARCHAR(10) ,
y4x VARCHAR(10) ,
y5x VARCHAR(10) ,
y6x VARCHAR(10) ,
y7x VARCHAR(10) ,
y8x VARCHAR(10) ,
y9x VARCHAR(10) ,
y10x VARCHAR(10) ,
y11x VARCHAR(10) ,
y12x VARCHAR(10) ,
y13x VARCHAR(10) ,
y14x VARCHAR(10) ,
y15x VARCHAR(10) ,
)
INSERT INTO #TEMP
( code ,
y1 ,
y2 ,
y3 ,
y4 ,
y5 ,
y6 ,
y7 ,
y8 ,
y9 ,
y10 ,
y11 ,
y12 ,
y13 ,
y14 ,
Y15 ,
Series
)
EXECUTE ( @tsqlUMP
)
UPDATE #TEMP
SET y1x = CAST(YEAR(GETDATE()) AS VARCHAR(10)) ,
y2x = CAST(YEAR(GETDATE()) - 1 AS VARCHAR(10)) ,
y3x = CAST(YEAR(GETDATE()) - 2 AS VARCHAR(10)) ,
y4x = CAST(YEAR(GETDATE()) - 3 AS VARCHAR(10)) ,
y5x = CAST(YEAR(GETDATE()) - 4 AS VARCHAR(10)) ,
y6x = CAST(YEAR(GETDATE()) - 5 AS VARCHAR(10)) ,
y7x = CAST(YEAR(GETDATE()) - 6 AS VARCHAR(10)) ,
y8x = CAST(YEAR(GETDATE()) - 7 AS VARCHAR(10)) ,
y9x = CAST(YEAR(GETDATE()) - 8 AS VARCHAR(10)) ,
y10x = CAST(YEAR(GETDATE()) - 9 AS VARCHAR(10)) ,
y11x = CAST(YEAR(GETDATE()) - 10 AS VARCHAR(10)) ,
y12x = CAST(YEAR(GETDATE()) - 11 AS VARCHAR(10)) ,
y13x = CAST(YEAR(GETDATE()) - 12 AS VARCHAR(10)) ,
y14x = CAST(YEAR(GETDATE()) - 13 AS VARCHAR(10)) ,
y15x = CAST(YEAR(GETDATE()) - 14 AS VARCHAR(10))
SELECT *
FROM #TEMP
DROP TABLE #TEMP
DROP TABLE #andriTemp
END
GO
Comments
Post a Comment