USE [AAB]
GO
/****** Object: UserDefinedFunction [dbo].[SFN_GET_DATADOWNLOAD_MARKET_PRICE] Script Date: 3/9/2017 9:03:06 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andri Kurniawan
-- Create date: 16 October 2016
-- Description: Get Data for Data Donwload Market Price [Vehicle Market Price Application Cisadane]
-- BRD : 0269 URF 2016 - Vehicle Market Price
-- =============================================
CREATE FUNCTION [dbo].[SFN_GET_DATADOWNLOAD_MARKET_PRICE]
(
@brandID VARCHAR(50)= '' ,
@modelID VARCHAR(50) ='',
@GeoArea VARCHAR(50) =''
)
RETURNS TABLE
AS
RETURN
(
WITH tmpTable
AS ( SELECT a.Vehicle_code AS Code ,
a.Year AS Tahun ,
MAX(a.Effective_date) AS MaxDate
FROM Mst_Vehicle_Price AS A
RIGHT OUTER JOIN Mst_Vehicle AS B ON A.Vehicle_code = B.Vehicle_code
LEFT OUTER JOIN Mst_Vehicle_Model AS E ON B.Model = E.Model_id
LEFT OUTER JOIN Dtl_Ins_Factor AS D ON B.Vehicle_type = D.insurance_code
LEFT OUTER JOIN Mst_Vehicle_Brand AS C ON B.Brand_id = C.Brand_id
WHERE A.GEO_AREA_ID = @GeoArea
AND a.Status = 1
AND b.Status = 1
AND A.Curr_Id = 'IDR'
AND a.Year > ( YEAR(GETDATE()) - 15 )
AND ( @brandID = ''
OR B.Brand_Id = @brandID
)
AND ( @modelID = ''
OR B.Model = @modelID
)
AND LEFT(b.Vehicle_type, 1) = 'T'
AND d.Factor_Code = 'VHCTYP'
AND C.Status = 1
AND D.Status = 1
AND E.Status = 1
AND e.Model_id LIKE 'M%'
GROUP BY a.Vehicle_code ,
a.Year ,
b.Brand_id ,
b.Model
)
SELECT x.Code ,
x.Tahun ,
ISNULL(( SELECT CAST(ISNULL(xx.Price, 0) / 1000000 AS DECIMAL(11,2))
FROM dbo.Mst_Vehicle_Price xx
WHERE xx.Vehicle_code = x.code
AND xx.Year = x.Tahun
AND xx.Effective_date = x.MaxDate
AND xx.Geo_Area_Id = @GeoArea
), 0) AS Harga
FROM tmpTable x
)
GO
GO
/****** Object: UserDefinedFunction [dbo].[SFN_GET_DATADOWNLOAD_MARKET_PRICE] Script Date: 3/9/2017 9:03:06 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andri Kurniawan
-- Create date: 16 October 2016
-- Description: Get Data for Data Donwload Market Price [Vehicle Market Price Application Cisadane]
-- BRD : 0269 URF 2016 - Vehicle Market Price
-- =============================================
CREATE FUNCTION [dbo].[SFN_GET_DATADOWNLOAD_MARKET_PRICE]
(
@brandID VARCHAR(50)= '' ,
@modelID VARCHAR(50) ='',
@GeoArea VARCHAR(50) =''
)
RETURNS TABLE
AS
RETURN
(
WITH tmpTable
AS ( SELECT a.Vehicle_code AS Code ,
a.Year AS Tahun ,
MAX(a.Effective_date) AS MaxDate
FROM Mst_Vehicle_Price AS A
RIGHT OUTER JOIN Mst_Vehicle AS B ON A.Vehicle_code = B.Vehicle_code
LEFT OUTER JOIN Mst_Vehicle_Model AS E ON B.Model = E.Model_id
LEFT OUTER JOIN Dtl_Ins_Factor AS D ON B.Vehicle_type = D.insurance_code
LEFT OUTER JOIN Mst_Vehicle_Brand AS C ON B.Brand_id = C.Brand_id
WHERE A.GEO_AREA_ID = @GeoArea
AND a.Status = 1
AND b.Status = 1
AND A.Curr_Id = 'IDR'
AND a.Year > ( YEAR(GETDATE()) - 15 )
AND ( @brandID = ''
OR B.Brand_Id = @brandID
)
AND ( @modelID = ''
OR B.Model = @modelID
)
AND LEFT(b.Vehicle_type, 1) = 'T'
AND d.Factor_Code = 'VHCTYP'
AND C.Status = 1
AND D.Status = 1
AND E.Status = 1
AND e.Model_id LIKE 'M%'
GROUP BY a.Vehicle_code ,
a.Year ,
b.Brand_id ,
b.Model
)
SELECT x.Code ,
x.Tahun ,
ISNULL(( SELECT CAST(ISNULL(xx.Price, 0) / 1000000 AS DECIMAL(11,2))
FROM dbo.Mst_Vehicle_Price xx
WHERE xx.Vehicle_code = x.code
AND xx.Year = x.Tahun
AND xx.Effective_date = x.MaxDate
AND xx.Geo_Area_Id = @GeoArea
), 0) AS Harga
FROM tmpTable x
)
GO
Comments
Post a Comment