Advertisement
ariswb22

[BMRI] CreditRisk query loader

Sep 7th, 2021
3,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 8.49 KB | None | 0 0
  1.                 SELECT
  2.                     c.[ContractId],
  3.                     c.[ReportingDate],
  4.                     c.[Entity],
  5.                     cm.[ContractMasterId],
  6.                     cm.[OriginContractId],
  7.                     cm.[OriginSystemId],
  8.                     c.[ProductType],
  9.                     c.[ProductSubtype],
  10.                     c.[BusinessUnit],
  11.                     c.[CounterpartyId],
  12.                     c.[BaseLgd],
  13.                     c.[BasePd],
  14.                     c.[CreditRating],
  15.                     c.[CreditRatingAgency],
  16.                     c.[Collectibility],
  17.                     c.[IsRestructured],
  18.                     c.[Currency],
  19.                     c.[CurrentPd],
  20.                     c.[DaysPastDue],
  21.                     c.[DiscountRate],
  22.                     c.[EffectiveInterestRate],
  23.                     c.[EffectiveInterestRateIncludingFeesAndCosts],
  24.                     c.[Segment],
  25.                     c.[IsSecured],
  26.                     c.[InitialPd],
  27.                     c.[InterestRate],
  28.                     c.[InterestSpread],
  29.                     c.[InterestCurve],
  30.                     c.[StartDate],
  31.                     c.[MaturityDate],
  32.                     c.[PrincipalAmount],
  33.                     c.[OutstandingAmount],
  34.                     c.[LimitAmount],
  35.                     c.[AccruedInterest],
  36.                     c.[PresentValue],
  37.                     c.[PresentValueIncludingFeesAndCosts],
  38.                     cn.[CounterpartyType],
  39.                     cnm.[OriginCounterpartyId],
  40.                     JSON_QUERY((
  41.                         SELECT
  42.                             ra.[Code] AS [ratingAgency],
  43.                             rs.[Code] AS [ratingScale],
  44.                             r.[Code] AS [rating]
  45.                         FROM [ContractRatings] cr
  46.                         JOIN [Ratings] r
  47.                             ON cr.[RatingId] = r.[RatingId]
  48.                         JOIN [RatingScales] rs
  49.                             ON r.[RatingScaleId] = rs.[RatingScaleId]
  50.                         JOIN [RatingAgencies] ra
  51.                             ON rs.[RatingAgencyId] = ra.[RatingAgencyId]
  52.                         WHERE cr.[ContractId] = c.[ContractId]
  53.                             FOR JSON PATH
  54.                     )) AS [ContractRatings],
  55.                     JSON_QUERY((
  56.                         SELECT
  57.                             ra.[Code] AS [ratingAgency],
  58.                             rs.[Code] AS [ratingScale],
  59.                             r.[Code] AS [rating]
  60.                         FROM [CounterpartyRatings] cr
  61.                         JOIN [Ratings] r
  62.                             ON cr.[RatingId] = r.[RatingId]
  63.                         JOIN [RatingScales] rs
  64.                             ON r.[RatingScaleId] = rs.[RatingScaleId]
  65.                         JOIN [RatingAgencies] ra
  66.                             ON rs.[RatingAgencyId] = ra.[RatingAgencyId]
  67.                         WHERE cr.[CounterpartyId] = c.[CounterpartyId]
  68.                             FOR JSON PATH
  69.                     )) AS [CounterpartyRatings],
  70.                     JSON_QUERY((
  71.                         SELECT
  72.                             cl.[CollateralId] AS [collateralId],
  73.                             clm.[OriginSystemId] AS [originSystemId],
  74.                             clm.[OriginCollateralId] AS [originCollateralId],
  75.                             cl.[CollateralType] AS [collateralType],
  76.                             JSON_VALUE(cl.DATA, '$.additionalProperties.collateralTypeProperty') AS [collateralTypeProperty],
  77.                             JSON_VALUE(cl.DATA, '$.additionalProperties.collateralMaturityDate') AS [collateralMaturityDate],
  78.                             cl.[CollateralAmount] AS [collateralAmount],
  79.                             cl.[Currency] AS [currency],
  80.                             @ReportingDate AS [reportingDate]
  81.                         FROM [CollateralContracts] cc
  82.                         JOIN [Collaterals] cl
  83.                           ON cc.[CollateralId] = cl.[CollateralId]
  84.                         JOIN [CollateralMaster] clm
  85.                           ON clm.[CollateralMasterId] = cl.[CollateralMasterId]
  86.                        WHERE cc.[ContractId] = c.[ContractId]
  87.                          AND cl.[CollateralType] IN ('101', '102', '282', '288', '289', '290', '291', '292','CDI')
  88.                          FOR JSON PATH
  89.                     )) AS [Collaterals],
  90.  
  91.                     ce.[GolonganDebitur],
  92.                     ce.[LBUForm],
  93.                     ce.[GolonganDebiturRWA],
  94.                     ce.[LBUFormRWA],
  95.                     ce.[SpecificProvisionAmount],
  96.                     ce.[Stage],
  97.                     ce.[Level],
  98.                     ce.[OutstandingAmountRWA],
  99.                     ce.[AccruedInterestRWA],
  100.                     ce.[LtvRatio],
  101.                     ce.[LtvRatioRWA],
  102.                     ce.[LtvRatioRWAB4],
  103.                     ce.[SecId],
  104.                     ce.[MarketRate],
  105.                     ce.[LendingType],
  106.                     ce.[LandType],
  107.                     ce.[GASReporting],
  108.                     ce.[CommittedUndrawnAmount],
  109.                     ce.[UncommittedUndrawnAmount],
  110.                     ce.[GlNumber],
  111.                     ce.[GlNumberRWA],
  112.                     ce.[Financing],
  113.                     ce.[FinancingB4],
  114.                     ce.[LimitDebtor],
  115.                     ce.[LimitDebtorB4],
  116.                     ce.[BaselCustomerType],
  117.                     ce.[BaselCustomerTypeB4],
  118.                     ce.[BaselProductSubtype],
  119.                     ce.[BaselProductSubtypeB4],
  120.                     ce.[BaselEconomicSector],
  121.                     ce.[BranchCode],
  122.  
  123.                     cpsd.DATA AS [ContractPropsSpotDerivativeData],
  124.                     cpsz.DATA AS [ContractPropsSecuritizationData],
  125.  
  126.                     JSON_VALUE(cnp.DATA, '$.netSalesCust') AS [NetSalesAmount],
  127.                     mbca.[TransactorFlag],
  128.                     mbca.[CollateralTypeProperty],
  129.  
  130.                     'Fix' AS [PayLegInterestType],
  131.                     'Floating' AS [ReceiveLegInterestType]
  132.  
  133.                 FROM [Contracts] c
  134.                 JOIN [ContractMaster] cm
  135.                   ON cm.[ContractMasterId] = c.[ContractMasterId]
  136.                 JOIN [Counterparties] cn
  137.                   ON cn.[CounterpartyId] = c.[CounterpartyId]
  138.                 JOIN [CounterpartyMaster] cnm
  139.                   ON cn.[CounterpartyMasterId] = cnm.[CounterpartyMasterId]
  140.                 JOIN [ContractsExtension] ce
  141.                   ON c.[ContractId] = ce.[ContractId]
  142.            LEFT JOIN [ContractPropsSpotDerivative] cpsd  
  143.                   ON c.[ContractId] = cpsd.[ContractId]
  144.            LEFT JOIN [ContractPropsSecuritization] cpsz  
  145.                   ON c.[ContractId] = cpsz.[ContractId]
  146.            LEFT JOIN [CounterpartyProps] cnp
  147.                   ON c.[CounterpartyId] = cnp.[CounterpartyId]
  148.            LEFT JOIN [MandiriBaselContractAggregate] mbca
  149.                   ON c.[ContractId] = mbca.[ContractId]
  150.             "
  151.            +
  152.            // This section is for querying standalone contracts
  153.            (SupportedContractType.Single.Equals(contractType) ?
  154.            @"
  155.            LEFT JOIN (SELECT DISTINCT cc.[ContractId]
  156.                         FROM [CollateralContracts] cc
  157.                         JOIN [CollateralClusters] clc
  158.                           ON clc.[ClusterId] = cc.[ClusterId]
  159.                        WHERE clc.[NumContracts] > 1) AS nn
  160.                   ON c.[ContractId] = nn.[ContractId]
  161.             "
  162.            :
  163.            // This section is for querying contracts in a specified cluster
  164.            @"
  165.                 JOIN (SELECT DISTINCT cc.[ContractId]
  166.                         FROM [CollateralContracts] cc
  167.                        WHERE cc.[ClusterId] = @ClusterId) AS clct
  168.                   ON c.[ContractId] = clct.[ContractId]
  169.             ")
  170.            +
  171.            @"
  172.                 WHERE 1=1
  173.                 AND c.[ReportingDate] = @ReportingDate
  174.                 AND c.[Entity] = @Entity
  175.                 AND cm.[OriginSystemId] <> 'ICS-IRB'
  176.                 AND (ce.[PsakClassification] <> 'T' OR ce.[PsakClassification] IS NULL)
  177.             "
  178.            +
  179.            // This section is for querying standalone contracts
  180.            (SupportedContractType.Single.Equals(contractType) ?
  181.            @"
  182.                 AND nn.[ContractId] IS NULL
  183.             "
  184.            :
  185.            // This section is for querying contracts in a specified cluster
  186.            @"")
  187.            ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement