[Database][Oracle] Operator Query


Deskripsi
      Untuk mengetahui selak beluk query pada database oracle. Content Summarize from Oracle.com


* Level Priority (semakin kecil semakin didahulukan)
level
1
Deskripsi
Unary + - arithmetic operators, PRIOR operator
2* / arithmetic operators
3Binary + - arithmetic operators, || character operators
4All comparison operators
5NOT logical operator
6AND logical operator
7OR logical operator



** Arithmetic Operators
OperatorDescriptionExample
+ (unary)Makes operand positiveSELECT +3 FROM DUAL;
- (unary)Negates operandSELECT -4 FROM DUAL;
/  Division (numbers and dates)SELECT SAL / 10 FROM EMP;
*  MultiplicationSELECT SAL * 5 FROM EMP;
+  Addition (numbers and dates)SELECT SAL + 200 FROM EMP;
-Subtraction (numbers and dates)SELECT SAL - 100 FROM EMP;



** Character Operator
OperatorDescriptionExample
||  Concatenates character stringsSELECT 'Your name: ' || ENAME FROM EMP;




** Comparison Operator
Comparison operators are used in conditions that compare one expression with another. The result of a comparison can be TRUE, FALSE, or UNKNOWN.
OperatorDescriptionExample
=  Equality test.SELECT ENAME "Employee" FROM EMP WHERE SAL = 1500;
!=, ^=, <>Inequality test.SELECT ENAME FROM EMP WHERE SAL ^= 5000;
>  Greater than test.SELECT ENAME "Employee", JOB "Title" FROM EMP WHERE SAL > 3000;
<  Less than test.SELECT * FROM PRICE WHERE MINPRICE < 30;
>=  Greater than or equal to test.SELECT * FROM PRICE WHERE MINPRICE >= 20;
<=  Less than or equal to test.SELECT ENAME FROM EMP WHERE SAL <= 1500;
IN"Equivalent to any member of" test. Equivalent to "= ANY".SELECT * FROM EMP WHERE ENAME IN ('SMITH', 'WARD');
ANY/ SOMECompares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, or >=. Evaluates to FALSE if the query returns no rows.SELECT * FROM DEPT WHERE LOC = SOME ('NEW YORK','DALLAS');
NOT INEquivalent to "!= ANY". Evaluates to FALSE if any member of the set is NULL.SELECT * FROM DEPT WHERE LOC NOT IN ('NEW YORK', 'DALLAS');
ALLCompares a value with every value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, or >=. Evaluates to TRUE if the query returns no rows.SELECT * FROM emp WHERE sal >= ALL (1400, 3000);
[NOT] BETWEEN and y[Not] greater than or equal to x and less than or equal to y.SELECT ENAME, JOB FROM EMP WHERE SAL BETWEEN 3000 AND 5000;
EXISTSTRUE if a sub-query returns at least one row.SELECT * FROM EMP WHERE EXISTS (SELECT ENAME FROM EMP WHERE MGR IS NULL);
x [NOT] LIKE y[ESCAPE z]TRUE if x does [not] match the pattern y. Within y, the character "%" matches any string of zero or more characters except null. The character "_" matches any single character. Any character following ESCAPE is interpretted litteraly, useful when contains a percent (%) or underscore (_).SELECT * FROM EMP WHERE ENAME LIKE '%E%';
IS [NOT] NULLTests for nulls. This is the only operator that should be used to test for nulls.SELECT * FROM EMP WHERE COMM IS NOT NULL AND SAL > 1500;

   


** Logical Operator
OperatorDescriptionExample
NOTReturns TRUE if the following condition is FALSE. Returns FALSE if it is TRUE. If it is UNKNOWN, it remains UNKNOWN.SELECT * FROM EMP WHERE NOT (job IS NULL)SELECT * FROM EMP WHERE NOT (sal BETWEEN 1000 AND 2000)
ANDReturns TRUE if both component conditions are TRUE. Returns FALSE if either is FALSE; otherwise returns UNKNOWN.SELECT * FROM EMP WHERE job='CLERK' AND deptno=10
ORReturns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE. Otherwise, returns UNKNOWN.SELECT * FROM emp WHERE job='CLERK' OR deptno=10



** Set Operator
OperatorDescriptionExample
UNIONReturns all distinct rows selected by either query.SELECT * FROM(SELECT ENAME FROM EMP WHERE JOB = 'CLERK'
UNION
SELECT ENAME FROM EMP WHERE JOB = 'ANALYST');
UNION ALLReturns all rows selected by either query, including all duplicates.SELECT * FROM(SELECT SAL FROM EMP WHERE JOB = 'CLERK'
UNION
SELECT SAL FROM EMP WHERE JOB = 'ANALYST');
INTERSECT and INTERSECT ALLReturns all distinct rows selected by both queries.SELECT * FROM orders_list1INTERSECT
SELECT * FROM orders_list2
MINUSReturns all distinct rows selected by the first query but not the second.SELECT * FROM (SELECT SAL FROM EMP WHERE JOB = 'PRESIDENT'MINUS
SELECT SAL FROM EMP WHERE JOB = 'MANAGER');




** Other Operator
OperatorDescriptionExample
(+)Indicates that the preceding column is the outer join column in a join.SELECT ENAME, DNAME FROM EMP, DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO (+);
PRIOREvaluates the following expression for the parent row of the current row in a hierarchical, or tree-structured query. In such a query, you must use this operator in the CONNECT BY clause to define the relationship between the parent and child rows.SELECT EMPNO, ENAME, MGR FROM EMP CONNECT BY PRIOR EMPNO = MGR;

[E-Bussiness Suite] Query Invoice with Accounting by Date


Deskripsi
     Query Invoice with Accounting (Debit,Credit,COA,Liabilities) plus additional display related PR , PO , RR , Invoice , Voucher Payment (STATUS : Negotiable and Reconciled Unaccounted) search by Invoice GL_DATE.

Howto
1. Execute Show View Hidden Here , because we need some oracle view.
2. Run Query
select *
from
(
select distinct PRHA.segment1 as PR
                    , PHA.segment1 as PO
                    , pv.VENDOR_NAME as Supplier
                    ,RVHV.RECEIPT_NUM as RECEIPT_NUMBER
                   ,AI.INVOICE_NUM as INVOICE
                   ,AC.DOC_SEQUENCE_VALUE as VOUCHER_PAYMENT
                   --,AID.PA_ADDITION_FLAG
                   --,AID.PROJECT_ACCOUNTING_CONTEXT
                  ,AID.DISTRIBUTION_LINE_NUMBER as DIST_LINE_NUM
                  ,AID.DESCRIPTION
                  ,AID.AMOUNT
                  ,AELA.AE_LINE_TYPE_CODE
                  ,GCC.SEGMENT1 AS ACCOUNT
                  ,AELA.ACCOUNTED_DR
                  ,AELA.ACCOUNTED_CR
     
from ap_invoices_all ai
    ,AP_INVOICE_PAYMENTS_V AIP
    ,AP_CHECKS_all AC
    ,ap_invoice_distributions AID
    ,po_distributions PD
    ,po_headers_all PHA
    ,po_line_locations_all PLLA
    ,po_lines_all PLA
    ,po_requisition_headers_all PRHA
    ,po_requisition_lines_all PRLA
    ,RCV_VRC_HDS_V RVHV
    ,RCV_SHIPMENT_LINES RSL
    ,ap_ae_lines_all aela
    ,gl_code_combinations GCC
    ,po_vendors PV
 
where ai.gl_date >= to_date('01/05/2013','dd/mm/yyyy')
and ai.gl_date <= to_date('02/05/2013','dd/mm/yyyy')
and ai.org_id = '0'
AND AI.INVOICE_ID = AIP.INVOICE_ID(+)
AND AIP.CHECK_ID = AC.CHECK_ID(+)
AND AI.INVOICE_ID = AID.INVOICE_ID(+)
AND AID.po_distribution_id = PD.po_distribution_id(+)
AND PD.po_header_id = PHA.po_header_id(+)
and PHA.po_header_id = PLA.po_header_id(+)
and PLA.po_line_id = plla.po_line_id(+)
and prha.requisition_header_id(+) = PRLA.requisition_header_id
and PRLA.line_location_id(+) = PLLA.line_location_id
AND PHA.po_header_id = RSL.po_header_id (+)
AND RSL.shipment_header_id = RVHV.shipment_header_id(+)
AND AELA.SOURCE_ID = AID.INVOICE_DISTRIBUTION_ID
AND GCC.CODE_COMBINATION_ID = AELA.CODE_COMBINATION_ID
AND PHA.VENDOR_ID = PV.VENDOR_ID(+)
AND (AC.STATUS_LOOKUP_CODE = 'RECONCILED UNACCOUNTED' OR AC.STATUS_LOOKUP_CODE = 'NEGOTIABLE')
--AND AID.TAX_RECOVERABLE_FLAG = 'N'
--AND AID.LINE_TYPE_LOOKUP_CODE <> 'TAX'
--AND AID.FINAL_MATCH_FLAG is null
--AND AID.DISTRIBUTION_LINE_NUMBER = '2'
UNION ALL
select distinct NULL as PR
               ,NULL as PO
               ,NULL as Supplier
               ,NULL as RECEIPT_NUMBER
               ,ai.INVOICE_NUM as INVOICE
               ,NULL as VOUCHER_PAYMENT
               ,AELA.AE_LINE_NUMBER as DIST_LINE_NUM
               ,NULL
               ,NULL
               ,AELA.AE_LINE_TYPE_CODE
               ,GCC.SEGMENT1 AS ACCOUNT
               ,AELA.ACCOUNTED_DR
               ,AELA.ACCOUNTED_CR
             
from AP_INVOICES_ALL AI
    ,AP_AE_LINES_ALL AELA
    ,gl_code_combinations GCC
 
where ai.gl_date >= to_date('01/05/2013','dd/mm/yyyy')
and ai.gl_date <= to_date('02/05/2013','dd/mm/yyyy')
and aela.SOURCE_ID = ai.INVOICE_ID(+)
and ae_line_type_code = 'LIABILITY'
and aela.ACCOUNTED_CR is not null
AND GCC.CODE_COMBINATION_ID = AELA.CODE_COMBINATION_ID
) data

order by 5,7

[Database][Oracle] Union with Order By


Deskripsi
      sometimes we get confused at the time of the union query with sort order and always ended with "invalid identifier" , where's the problem ?? the problem oracle doesn't know about the field_name. what the solution ?? to identify the field union when using order by just use column index. 

Howto

case :
1. i have the table with different column format, but there's 2 same field and i want to union
2. i want order by y and z column

Column table A : x,y,z
Column table B : s,y,z

the solution
select *
from
(
select x,y,z
from A
union all -- or union
selecy null,y,z -- null means fake column
from B
) data --aliases
order by 2,3 ;
-- 2 and 3 is index column from left : x(1) , y(2) , z(3) .


if you want still use field_name , add it with alias (recommended)
select *
from
(
select x as x_one,
         y as y_two,
         z as z_three
from A
union all -- or union
selecy null,y,z -- null means fake column
from B
) data --aliases
order by data.y_two,data.z_three ;


[E-Bussiness Suite] Query Get Accounting 'Liabilities' invoice


Deskripsi
     Untuk mendapatkan data 'Liabilities' pada View Accounting (Invoices)

Howto
select distinct ai.INVOICE_NUM
               ,AELA.AE_LINE_TYPE_CODE
               ,AELA.AE_LINE_NUMBER
               ,GCC.SEGMENT1 AS ACCOUNT
               ,AELA.ACCOUNTED_DR as debit
               ,AELA.ACCOUNTED_CR as credit

             
from AP_INVOICES_ALL AI
    ,AP_AE_LINES_ALL AELA
    ,gl_code_combinations GCC
 
where ai.gl_date >= to_date('01/05/2013','dd/mm/yyyy')
and ai.gl_date <= to_date('02/05/2013','dd/mm/yyyy')
and aela.SOURCE_ID = ai.INVOICE_ID(+)
and ae_line_type_code = 'LIABILITY'
and aela.ACCOUNTED_CR is not null
AND GCC.CODE_COMBINATION_ID = AELA.CODE_COMBINATION_ID
order by AI.INVOICE_NUM
;

[E-Bussiness Suite] Query Invoice - Line Distribution with Accounting


Deskripsi
     Untuk menampilkan data PR - PO - RR - Invoice - Payment dan difilter bedasarkan GL Invoice serta ditambah data tambahan dari invoice seperti line_distribution , amount , debit , credit ,  Chart of Account tapi tidak menampilkan 'Liabilities'

Howto
1. Activate Show View Hidden
2. Query this sql below

select distinct PRHA.segment1 as PR

              , PHA.segment1 as PO
              ,RVHV.RECEIPT_NUM as RECEIPT_NUMBER
              ,AI.INVOICE_NUM as INVOICE
              ,AC.DOC_SEQUENCE_VALUE as VOUCHER_PAYMENT  
              --,AID.PA_ADDITION_FLAG
              --,AID.PROJECT_ACCOUNTING_CONTEXT 
              ,AID.DISTRIBUTION_LINE_NUMBER
              ,AID.DESCRIPTION
              ,AID.AMOUNT
              ,AELA.AE_LINE_TYPE_CODE
              ,GCC.SEGMENT1 AS ACCOUNT
              ,AELA.ACCOUNTED_DR
              ,AELA.ACCOUNTED_CR  
       
       
from ap_invoices_all ai
    ,AP_INVOICE_PAYMENTS_V AIP
    ,AP_CHECKS_all AC
    ,ap_invoice_distributions AID
    ,po_distributions PD
    ,po_headers_all PHA
    ,po_line_locations_all PLLA
    ,po_lines_all PLA
    ,po_requisition_headers_all PRHA
    ,po_requisition_lines_all PRLA
    ,RCV_VRC_HDS_V RVHV
    ,RCV_SHIPMENT_LINES RSL
    ,ap_ae_lines_all aela
    ,gl_code_combinations GCC
    
-- change your date here , or you can create input     
where ai.gl_date >= to_date('01/05/2013','dd/mm/yyyy')
and ai.gl_date <= to_date('02/05/2013','dd/mm/yyyy')
and ai.org_id = '0'
AND AI.INVOICE_ID = AIP.INVOICE_ID(+)
AND AIP.CHECK_ID = AC.CHECK_ID(+)
AND AI.INVOICE_ID = AID.INVOICE_ID(+)
AND AID.po_distribution_id = PD.po_distribution_id(+)
AND PD.po_header_id = PHA.po_header_id(+)
and PHA.po_header_id = PLA.po_header_id(+)
and PLA.po_line_id = plla.po_line_id(+)
and prha.requisition_header_id(+) = PRLA.requisition_header_id
and PRLA.line_location_id(+) = PLLA.line_location_id
AND PHA.po_header_id = RSL.po_header_id (+)
AND RSL.shipment_header_id = RVHV.shipment_header_id(+)
AND AELA.SOURCE_ID = AID.INVOICE_DISTRIBUTION_ID 
AND GCC.CODE_COMBINATION_ID = AELA.CODE_COMBINATION_ID
--AND AID.TAX_RECOVERABLE_FLAG = 'N'
--AND AID.LINE_TYPE_LOOKUP_CODE <> 'TAX' 
--AND AID.FINAL_MATCH_FLAG is null
--AND AID.DISTRIBUTION_LINE_NUMBER = '2'
order by AI.INVOICE_NUM,PHA.segment1,RVHV.RECEIPT_NUM,AID.DISTRIBUTION_LINE_NUMBER;

[E-Bussiness Suite] Query All Resource PSI - Project Costing


Deskripsi 
     Untuk mendapatkan semua item resource per-project dari PSI (Project Costing) beserta nilai dari commitment , actual , cost budget , Actual + Commitment berdasarkan task_number level 3.

Howto
SELECT distinct PPAH.project_id
      ,PPAH.task_id
      ,PT.WBS_LEVEL
      ,PT.top_task_id
      --,PRLM.parent_member_id
      ,PTK.parent_member_id
      ,PT.task_name
      ,PT.TASK_NUMBER
      ,PRLM.alias
      ,PRLM.expenditure_type as Resource_Name
      ,NVL(PPAB.BASE_RAW_COST_ITD,0) as ITD_Cost_Budget
      ,NVL(PPAA.RAW_COST_ITD,0) as ITD_Act_Cost
      ,NVL(PPAC.CMT_RAW_COST_ITD,0) as Commitment_ITD 
      ,(NVL(PPAA.RAW_COST_ITD,0) + NVL(PPAC.CMT_RAW_COST_ITD,0)) as Total_Act_Com
      ,PPAB.BASE_RAW_COST_TOT 
      ,PPAB.ORIG_BURDENED_COST_TOT 
      ,(NVL(PPAB.BASE_RAW_COST_TOT,0) - NVL(PPAA.RAW_COST_ITD,0) + NVL(PPAC.CMT_RAW_COST_ITD,0) ) as estimate_to_complete
      
FROM pa_project_accum_headers PPAH
    ,pa_resource_list_members PRLM
    ,pa_project_accum_actuals PPAA
    ,PA_PROJECT_ACCUM_BUDGETS PPAB
    ,PA_PROJECT_ACCUM_COMMITMENTS PPAC
    ,pa_tasks PT
    ,(select distinct prlm.parent_member_id, pt.task_name
      from pa_resource_list_members PRLM
          ,pa_project_accum_headers PPAH
          ,pa_tasks PT
    
     where PT.project_id = :Project_ID
     and wbs_level = '2'
     and PT.PROJECT_ID = PPAH.PROJECT_ID
     AND PT.TASK_ID = PPAH.TASK_ID  
     and PPAH.resource_id=PRLM.resource_id
     order by task_name) PTK
    
where PT.project_id = :Project_ID
and wbs_level = '3'
and PT.PROJECT_ID = PPAH.PROJECT_ID
AND PT.TASK_ID = PPAH.TASK_ID  
and PPAH.resource_id=PRLM.resource_id
and PPAH.PROJECT_ACCUM_ID=PPAA.PROJECT_ACCUM_ID(+)
and PPAH.PROJECT_ACCUM_ID=PPAB.PROJECT_ACCUM_ID(+)
and PPAH.PROJECT_ACCUM_ID=PPAC.PROJECT_ACCUM_ID(+)
and PRLM.parent_member_id = ptk.parent_member_id(+)
--to see minus budget you add this line below .....
--and (NVL(PPAB.BASE_RAW_COST_TOT,0) - NVL(PPAA.RAW_COST_ITD,0) + NVL(PPAC.CMT_RAW_COST_ITD,0) ) < 0
order by pt.task_number 
;

[E-Bussiness Suite] Query Resources PSI - Project Costing


Deskripsi
     Untuk mendapatkan item resource dari PSI (Project Costing) beserta nilai dari commitment , actual , cost budget, Actual + Commitment berdasarkan task_number level 3.

Howto
SELECT PPAH.project_id
      ,PPAH.task_id
      ,PT.WBS_LEVEL
      ,PT.top_task_id
      --,PRLM.parent_member_id
      ,PT.task_name
      ,PT.TASK_NUMBER
      ,PRLM.alias
      ,PRLM.expenditure_type as Resource_Name
      ,NVL(PPAB.BASE_RAW_COST_ITD,0) as ITD_Cost_Budget
      ,NVL(PPAA.RAW_COST_ITD,0) as ITD_Act_Cost
      ,NVL(PPAC.CMT_RAW_COST_ITD,0) as Commitment_ITD
      ,(NVL(PPAA.RAW_COST_ITD,0) + NVL(PPAC.CMT_RAW_COST_ITD,0)) as Total_Act_Com
      ,PPAB.BASE_RAW_COST_TOT
      ,PPAB.ORIG_BURDENED_COST_TOT
   
FROM pa_project_accum_headers PPAH
    ,pa_resource_list_members PRLM
    ,pa_project_accum_actuals PPAA
    ,PA_PROJECT_ACCUM_BUDGETS PPAB
    ,PA_PROJECT_ACCUM_COMMITMENTS PPAC
    ,pa_tasks PT
 
where PT.project_id = :Project_ID_NUMBER
and PT.PROJECT_ID = PPAH.PROJECT_ID
AND PT.TASK_ID = PPAH.TASK_ID
and PPAH.resource_id=PRLM.resource_id
and PPAH.PROJECT_ACCUM_ID=PPAA.PROJECT_ACCUM_ID(+)
and PPAH.PROJECT_ACCUM_ID=PPAB.PROJECT_ACCUM_ID(+)
and PPAH.PROJECT_ACCUM_ID=PPAC.PROJECT_ACCUM_ID(+)
and PT.task_number = :TASK_NUMBER_LEVEL3 'for example 24.0001.01
order by pt.task_number
;