Avatar of kanneswara
kanneswara asked on

SQL view subquery modification

Hi,

I have the following created

CREATE VIEW Finance.TransactionLog AS
SELECT
  'BANK' AS TransactionTargetTypeCode,
  'PAYMENT_RECEIVED' AS TransactionTypeCode,
  PY.PaymentID AS TransactionIdentificationID,
  PY.DepositDate AS TransactionDate,
  PY.Description AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  PY.Amount AS TransactionAmount
FROM
  DATABASE.Payment PY
  INNER JOIN DATABASE.Sponsor S ON
    PY.SponsorID = S.SponsorID
    AND PY.SubType = 2 -- Sponsor
    AND PY.DeletedFlag = 0
    AND UPPER(PY.Description) IN ('BOA','CITIBANK') -- Be sure to modify list below as well
 
UNION ALL
 
SELECT
  'EMPLOYER' AS TransactionTargetTypeCode,
  'INVOICE' AS TransactionTypeCode,
  ST.StatementID AS TransactionIdentificationID,
  ST.InvoiceDate AS TransactionDate,
  S.Name AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  SS.OutstandingBalance AS TransactionAmount
FROM
  DATABASE.Statements ST
  LEFT OUTER JOIN DATABASE.StatementSummary SS ON
    SS.StatementID = ST.StatementID  
    AND SS.DeletedFlag = 0
    AND ST.StatementTypeID = 2 -- Sponsor Invoice
  INNER JOIN DATABASE.Sponsor S ON
    ST.SponsorID = S.SponsorID
 
UNION ALL
 
SELECT
  'EMPLOYER' AS TransactionTargetTypeCode,
  'PAYMENT_RECEIVED' AS TransactionTypeCode,
  PY.PaymentID AS TransactionIdentificationID,
  PY.DepositDate AS TransactionDate,
  S.Name AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  PY.Amount AS TransactionAmount
FROM
  DATABASE.Payment PY
  INNER JOIN DATABASE.Sponsor S ON
    PY.SponsorID = S.SponsorID
    AND PY.SubType = 2 -- Sponsor
    AND PY.DeletedFlag = 0
    AND UPPER(PY.Description) NOT IN ('BOA','CITIBANK') -- Be sure to modify list above as well
 
UNION ALL
 
SELECT
  'EMPLOYER' AS TransactionTargetTypeCode,
  'PAYMENT_POSTED' AS TransactionTypeCode,
  PY.PaymentID AS TransactionIdentificationID,
  PY.DepositDate AS TransactionDate,
  S.Name AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  PAA.Amount AS TransactionAmount
FROM
  DATABASE.Payment PY
  INNER JOIN DATABASE.Sponsor S ON
    PY.SponsorID = S.SponsorID
    AND PY.SubType = 2 -- Sponsor
    AND PY.DeletedFlag = 0
  INNER JOIN DATABASE.PaymentApplyAllocation PAA ON
    PAA.PaymentID = PY.PaymentID
    AND PAA.DeletedFlag = 0
 
UNION ALL
 
SELECT
  'EMPLOYER' AS TransactionTargetTypeCode,
  'ADJUSTMENT' AS TransactionTypeCode,
  ADJ.Adjustment_ID AS TransactionIdentificationID,
  DATE(ADJ.Insert_TS) AS TransactionDate,
  S.Name AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  ADJ.Adjustment_Amount AS TransactionAmount
FROM
  DATABASE.Adjustment ADJ  
  INNER JOIN DATABASE.Sponsor S ON
    ADJ.Entity_Identifier = S.ExternalIdentifier
    --AND ADJ.DeletedFlag = 0
    AND ADJ.Adjustment_Status_Ref_ID = 3
 
UNION ALL
 
SELECT
  'CARRIER' AS TransactionTargetTypeCode,
  'DISBURSEMENT' AS TransactionTypeCode,
  D.ID AS TransactionIdentificationID,
  D.Processing_Date AS TransactionDate,
  S.Name AS TransactionTargetName,
  S.ExternalIdentifier AS TransactionTargetIdentificationID,
  D.Amount AS TransactionAmount
FROM
  DATABASE.Disbursements D
  INNER JOIN DATABASE.Sponsor S ON
    D.Person_External_ID = S.ExternalIdentifier
    --AND D.DeletedFlag = 0
 
UNION ALL
 
SELECT
  'INDIVIDUAL' AS TransactionTargetTypeCode,
  'INVOICE' AS TransactionTypeCode,
  ST.StatementID AS TransactionIdentificationID,
  ST.InvoiceDate AS TransactionDate,
  CONCAT(P.FirstName,CONCAT(' ',P.LastName)) AS TransactionTargetName,
  P.ExternalIdentifier AS TransactionTargetIdentificationID,
  SS.OutstandingBalance AS TransactionAmount
FROM
  DATABASE.Statements ST
  INNER JOIN DATABASE.StatementSummary SS ON
    SS.StatementID = ST.StatementID  
    AND SS.DeletedFlag = 0
    AND ST.StatementTypeID = 1 -- Invididual Invoice
  INNER JOIN DATABASE.Person P ON
    ST.PersonID = P.PersonID
 
UNION ALL
 
SELECT
  'INDIVIDUAL' AS TransactionTargetTypeCode,
  'PAYMENT_RECEIVED' AS TransactionTypeCode,
  PY.PaymentID AS TransactionIdentificationID,
  PY.DepositDate AS TransactionDate,
  CONCAT(P.FirstName,CONCAT(' ',P.LastName)) AS TransactionTargetName,
  P.ExternalIdentifier AS TransactionTargetIdentificationID,
  PY.Amount AS TransactionAmount
FROM
  DATABASE.Payment PY
  INNER JOIN DATABASE.Person P ON
    PY.PersonID = P.PersonID
    AND PY.SubType = 6 -- Person
    AND PY.DeletedFlag = 0
 
UNION ALL
 
SELECT
  'INDIVIDUAL' AS TransactionTargetTypeCode,
  'PAYMENT_POSTED' AS TransactionTypeCode,
  PY.PaymentID AS TransactionIdentificationID,
  PY.DepositDate AS TransactionDate,
  CONCAT(P.FirstName,CONCAT(' ',P.LastName)) AS TransactionTargetName,
  P.ExternalIdentifier AS TransactionTargetIdentificationID,
  PAA.Amount AS TransactionAmount
FROM
  DATABASE.Payment PY
  INNER JOIN DATABASE.Person P ON
    PY.PersonID = P.PersonID
    AND PY.SubType = 6 -- Person
    AND PY.DeletedFlag = 0
  INNER JOIN DATABASE.PaymentApplyAllocation PAA ON
    PAA.PaymentID = PY.PaymentID
    AND PAA.DeletedFlag = 0
 
UNION ALL
 
SELECT
  'INDIVIDUAL' AS TransactionTargetTypeCode,
  'APTC' AS TransactionTypeCode,
  BLC.BLChargeID AS TransactionIdentificationID,
  DATE(BLSG.UpdateTS) AS TransactionDate,
  CONCAT(P.FirstName,CONCAT(' ',P.LastName)) AS TransactionTargetName,
  P.ExternalIdentifier AS TransactionTargetIdentificationID,
  BLC.Amount AS TransactionAmount
FROM DATABASE.BLCharge BLC
  INNER JOIN DATABASE.RateCharge RC ON
    BLC.RateChargeID = RC.RateChargeID
    AND RC.CalcCD = 17
    AND RC.DeletedFlag = 0
    AND BLC.DeletedFlag = 0
    AND BLC.Amount <> 0
  INNER JOIN DATABASE.BLStatementGroup BLSG ON
    BLC.BLStatementGroupID = BLSG.BLStatementGroupID
  INNER JOIN DATABASE.BLOperation BLO ON
    BLSG.BLOperationID = BLO.BLOperationID  
  INNER JOIN DATABASE.BLTransaction BLT ON
    BLO.BLTransactionID = BLT.BLTransactionID  
  INNER JOIN DATABASE.Person P ON
    BLT.PersonID = P.PersonID;  
 
 
CREATE VIEW Reports.FMSRPT03 AS
SELECT
  T.TransactionIdentificationID,
  T.TransactionTargetTypeCode,
  T.TransactionTypeCode,
  T.TransactionDate,
  T.TransactionTargetIdentificationID,
  T.TransactionTargetName,
  T.TransactionAmount,
  (CASE T.TransactionTypeCode WHEN 'INVOICE' THEN T.TransactionDate ELSE NULL END) AS InvoiceDate,
  (CASE T.TransactionTypeCode WHEN 'INVOICE' THEN T.TransactionIdentificationID ELSE NULL END) AS InvoiceReferenceID,
  (CASE T.TransactionTypeCode WHEN 'INVOICE' THEN T.TransactionAmount ELSE NULL END) AS InvoiceAmount,
  (CASE T.TransactionTypeCode WHEN 'APTC' THEN T.TransactionAmount ELSE NULL END) AS APTCAmount,
  (CASE T.TransactionTypeCode WHEN 'ADJUSTMENT' THEN T.TransactionAmount ELSE NULL END) AS AdjustmentAmount,
  (CASE T.TransactionTypeCode WHEN 'ADJUSTMENT' THEN T.TransactionDate ELSE NULL END) AS AdjustmentDate,
  (CASE T.TransactionTypeCode WHEN 'PAYMENT_RECEIVED' THEN T.TransactionAmount ELSE NULL END) AS PaymentReceivedAmount,
  (CASE T.TransactionTypeCode WHEN 'PAYMENT_RECEIVED' THEN T.TransactionDate ELSE NULL END) AS PaymentReceivedDate,
  (CASE T.TransactionTypeCode WHEN 'PAYMENT_POSTED' THEN T.TransactionAmount ELSE NULL END) AS PaymentPostedAmount,
  (CASE T.TransactionTypeCode WHEN 'PAYMENT_POSTED' THEN T.TransactionDate ELSE NULL END) AS PaymentPostedDate,
  (CASE T.TransactionTypeCode WHEN 'DISBURSEMENT' THEN T.TransactionAmount ELSE NULL END) AS DisbursementAmount,
  (CASE T.TransactionTypeCode WHEN 'DISBURSEMENT' THEN T.TransactionDate ELSE NULL END) AS DisbursementDate
FROM Finance.TransactionLog T;

 
My question is for APTC, i do not want to create a separate select, but no other tables in my union have a similiar field, so i have to create a separate view.but i want to include it in Individuals
DB Dev Tools

Avatar of undefined
Last Comment
Sharath S

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sharath S

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck