Solved

Defining AS of Date in SQL

Posted on 2014-11-10
2
174 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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

772 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

11 Experts available now in Live!

Get 1:1 Help Now