Solved

Defining AS of Date in SQL

Posted on 2014-11-10
2
179 Views
Last Modified: 2014-11-22
I am trying to Define As of Date is use in the code below.  Can someone please look at my code and see if I have it correct.  I also need to show show in the Results this column (END_BAL_A)as BALANCE.  Any other suggestions appreciated.  Using Oracle  11
   
   DEFINE AsOfDate = TO_DATE('07/31/2014','MM/DD/YYYY')


         
SELECT Emp_N, Emp_SHRT_NM,
   SUM(Case when BALANCE>0 THEN 1 Else 0 end) as "# Parts w/ Bal (Total)",
 
   FROM(
      SELECT A.Emp_ID, SUM(END_BAL_A)as BALANCE, A.AS_OF_D
      FROM adbo.V_BAL_MO A      
         
          LEFT JOIN(
                SELECT *--Emp_ID, SSN_ID,
                FROM adbo.V_AT_PART
                 WHERE row_exp_d=TO_DATE('01/01/9999','MM/DD/YYYY')
                 AND Emp_ID IN ('2287')
                ) C ON A.Emp_ID=C.Emp_ID AND A.SSN_ID=C.SSN_ID
         
          LEFT JOIN(
              SELECT *
              FROM adbo.V_FDLLN
              WHERE row_exp_d=TO_DATE('01/01/9999','MM/DD/YYYY')
              AND CUR_LOAN_BAL_A > 0
              ) B ON A.Emp_ID=B.Emp_ID AND A.SSN_ID=B.SSN_ID
       
          LEFT JOIN(
              SELECT Emp_ID, Emp_N, Emp_SHRT_NM
              FROM adbo.V_LA_Emp
              WHERE row_exp_d=TO_DATE('01/01/9999','MM/DD/YYYY')
          ) D ON A.Emp_ID=D.Emp_ID    
         
      AND A.Emp_ID IN ('2287')
   
      GROUP BY A.Emp_ID, A.Emp_ID, A.AS_OF_D
   
)
    WHERE  A.ROW_EXP_D >= AsOfDate
   

GROUP BY Emp_N
0
Comment
Question by:leezac
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40434328
SELECT
      Emp_N
    , Emp_SHRT_NM
    , BALANCE
    , SUM(CASE
            WHEN BALANCE > 0 THEN 1
            ELSE 0
      END) AS "# PARTS W/ BAL (TOTAL)"
   , AsOfDate
FROM (
            SELECT
                  A.Emp_ID
                , SUM(END_BAL_A) AS BALANCE
                , A.AS_OF_D
            FROM adbo.V_BAL_MO A

                  LEFT JOIN (
                        SELECT
                              *--Emp_ID, SSN_ID, 
                        FROM adbo.V_AT_PART
                        WHERE row_exp_d = TO_DATE('01/01/9999', 'MM/DD/YYYY')
                        AND Emp_ID IN ('2287')
                  ) C ON A.Emp_ID = C.Emp_ID
                  AND A.SSN_ID = C.SSN_ID

                  LEFT JOIN (
                        SELECT
                              *
                        FROM adbo.V_FDLLN
                        WHERE row_exp_d = TO_DATE('01/01/9999', 'MM/DD/YYYY')
                        AND CUR_LOAN_BAL_A > 0
                  ) B ON A.Emp_ID = B.Emp_ID
                  AND A.SSN_ID = B.SSN_ID

                  LEFT JOIN (
                        SELECT
                              Emp_ID
                            , Emp_N
                            , Emp_SHRT_NM
                        FROM adbo.V_LA_Emp
                        WHERE row_exp_d = TO_DATE('01/01/9999', 'MM/DD/YYYY')
                  ) D ON A.Emp_ID = D.Emp_ID

                  AND A.Emp_ID IN ('2287')
                  CROSS JOIN (
                              SELECT TO_DATE('07/31/2014','MM/DD/YYYY') AsOfDate FROM dual
                              ) Y
            WHERE A.ROW_EXP_D >= Y.AsOfDate
            GROUP BY A.Emp_ID
                   , A.Emp_ID
                   , A.AS_OF_D
                   , Y.AsOfDate
      ) X
GROUP BY
      Emp_N
    , Emp_SHRT_NM
    , AsOfDate
;

Open in new window

0
 

Author Closing Comment

by:leezac
ID: 40459511
Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now