Advertisement
ariswb22

CreditRiskContractReadingService

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