Skip to main content

SQL - With Clause - Explained with examples

In this blog, we are going to see SQL With Clause

Syntax of the SQL WITH clause using a single sub-query alias

WITH <alias_name> AS (sql_subquery_statement)
SELECT column_list FROM <alias_name>[,table_name]
[WHERE <join_condition>]

Example:
With

Empl_Name
--alias_name
as

(

select

'1234' Emplid
--Column 1
,

'Test1' Name
--Column2
from

Dual
--Table Name
)

Select

*
-- all columns
from

Empl_Name
-- alias_name

Result:
Emplid
Name
1234
Test1

Syntax of the SQL WITH clause using a multiple sub-query alias

WITH <alias_name_A> AS (sql_subquery_statement),
<alias_name_B> AS(sql_subquery_statement_from_alias_name_A
or sql_subquery_statement )
SELECT <column_list>
FROM <alias_name_A>, <alias_name_B> [,table_names]
[WHERE <join_condition>]

Example: 
With

Empl_Name
--alias_name_A
as

(

select

'1234' Emplid
--Column1
,
'Test1' Name
--Column2
from

Dual
--Table Name
)

,

Empl_Position
--alias_name_B
as

(

select

'1234' Emplid
--Column1
,
'Position1' Position
--Column2
from

Dual
--Table Name
)

Select

Empl_Name.Emplid
--alias_name_A.Column1
,

Empl_Name.Name
--alias_name_A.Column2
,

Empl_Position.Position
--alias_name_B.Column2
from

Empl_Name
-- alias_name_A
,

Empl_Position
-- alias_name_B
where

Empl_Name.Emplid
--alias_name_A.Column1
=

Empl_Position.Emplid
--alias_name_B.Column1

Result:  
Emplid
Name
Position
1234
Test1
Position1

I hope this blog explained SQL With Clause. If you have any questions, please post it in the comments section

Comments

  1. Useful content.. how to fetch count of emplid, sum of hours using group by clause using with clause function..

    ReplyDelete

Post a Comment

Popular posts from this blog

Do you know how to cancel the Work Relationship using HDL in Oracle Cloud HCM?

In this blog, we are going to see how to cancel the Work Relationship using HDL (HCM Data Loader) Please make sure you have taken the back up of all the necessary data before cancelling the Work Relationship To cancel the Work Relationship, we use HDL Source Key method. Use the below sample template for reference SET PURGE_FUTURE_CHANGES N METADATA|WorkRelationship|SourceSystemOwner|SourceSystemId|PersonId(SourceSystemId)|LegalEmployerName|PersonNumber|DateStart|WorkerType|CancelWorkRelationshipFlag DELETE|WorkRelationship|FUSION|1234|WRK_1234|Legal_Employer|1234|2018/02/01|E|Y In the above template, we do not know the values of the source keys SourceSystemOwner SourceSystemId PersonId(SourceSystemId) Use below query to fetch the SourceSystemOwner Query: select source_system_owner from hrc_integration_key_map where surrogate_id = period_of_service_id   -- pass period_of_service_id corresponding to Work Relationship Use below query to fetch the SourceSy...

Do you know how to generate output using eText Template in Oracle Cloud BI Publisher?

In this blog, we are going to see how to generate output using eText Template in Fusion BI Publisher I am going to show this using sample data. This is my sample data Click "Export" button to download the XML file. This is my XML File Open a blank Word Document and save it as type "Rich Text Format" Add a format setup which is given below <TEMPLATE TYPE> FIXED_POSITION_BASED <OUTPUT CHARACTER SET> UTF-8 <NEW RECORD CHARACTER> Carriage Return Add a Header table which is given below. For headers, give the header name within quotes ex: 'Emplid'. For headers, use the level DATA_DS which is the top level and appears only once in the XML <LEVEL> DATA_DS <POSITION> <LENGTH> <FORMAT> <PAD> <DATA> <COMMENTS> <NEW RECORD> DATA_DS Alpha ‘Emplid’ ...

Do you know how to fetch Oracle Cloud HCM Performance Questionnaire data using SQL

In this session, we are going to see how to how to fetch Oracle Cloud HCM Performance Questionnaire data using SQL SQL Text: with response_view as ( select ep . evaluation_id , ep . person_id , ep . role_type_code , pq . qstnr_question_id , pq . question_type , pq . question_text question , coalesce ( qr . answer_text , qa . long_text ) answer from hra_eval_participants ep , hrq_qstnr_participants qp , hrq_eval_ptcpnt_questions_v epq , hrq_ptcpnt_questions_v pq , hrq_qstnr_latest_resps_v qnlr , hrq_qstn_responses qr , hrq_all_qstn_answers_v qa where 1 = 1 and qp . participant_id = to_char ( ep . eval_participant_id ) and epq . participant_id = qp . participant_id and epq . subject_id = qp . subject_id and pq . qstnr_section_id = epq . qstnr_section_id and pq . qstnr_question_id = epq . qstnr_question_id and qnlr . subject_id = to_char...