This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

How to display default value if sql not return any row.

Vemula Muni
Vemula Muni Member Posts: 57
edited Sep 20, 2019 12:00PM in SQL & PL/SQL

Hi All,

Below is my query. In that i will pass the emplid and comparison date dynamically.

For that emplid and date combination, if project id is present then i need to fetch that project id. if project id is not present then i need to display some default value.

  1. When query returns project id i want to exclude the default value, if query not return any row then i need to get some default value.

Note : Query may return one or more projectid for one employee.

SELECT   B.PROJECT_ID  FROM PS_CTSRM_ASGNSR_VW B ,  PS_RS_ASSGN_DETAIL C WHERE B.ASSIGNMENT_ID = C.ASSIGNMENT_ID AND B.EMPLID  = '155114'AND B.ASSIGN_STS IN ('A','C') AND TO_DATE('2019-08-01','YYYY-MM-DD') BETWEEN C.START_DT AND C.END_DT

In below example my query returns value,so no need the default value in this case.

pastedImage_0.png

in below example query not return any row , so i want to display some default value.

pastedImage_1.png

Tagged:
RanagalFrank Kulash

Best Answer

  • L. Fernigrini
    L. Fernigrini Database Practice Lead Rosario, ArgentinaMember Posts: 4,196 Gold Crown
    edited Sep 13, 2019 11:42AM Answer ✓

    Please, this is not the first question you ask, follow the guidelines!!!

    It is imposible to reproduce what you show in the pictures, please copy and paste CODE!!

    You should consider something like:

    WITH vDesiredData as (SELECT xxx, yyy, zzz FROM YourTable WHERE .....),

         vDefaultData as (SELECT  'A', 1, SYSDATE FROM DUAL)

    SELECT * FROM vDesiredData

    UNION ALL

    SELECT * FROM vDefaultData WHERE NOT EXISTS (SELECT 1 FROM vDesiredData);

    In this case if there is data that can be returned, the UNION won't add anything since the NOT EXISTS in the WHERE clause will evaluate to false.

    On the other case, when there is no data on vDesiredData, the first SELECT FROM vDesiredData wont return anything but the UNION ALL will incude whatever you configured in vDefaultData

    Ranagal

Answers

  • L. Fernigrini
    L. Fernigrini Database Practice Lead Rosario, ArgentinaMember Posts: 4,196 Gold Crown
    edited Sep 13, 2019 11:42AM Answer ✓

    Please, this is not the first question you ask, follow the guidelines!!!

    It is imposible to reproduce what you show in the pictures, please copy and paste CODE!!

    You should consider something like:

    WITH vDesiredData as (SELECT xxx, yyy, zzz FROM YourTable WHERE .....),

         vDefaultData as (SELECT  'A', 1, SYSDATE FROM DUAL)

    SELECT * FROM vDesiredData

    UNION ALL

    SELECT * FROM vDefaultData WHERE NOT EXISTS (SELECT 1 FROM vDesiredData);

    In this case if there is data that can be returned, the UNION won't add anything since the NOT EXISTS in the WHERE clause will evaluate to false.

    On the other case, when there is no data on vDesiredData, the first SELECT FROM vDesiredData wont return anything but the UNION ALL will incude whatever you configured in vDefaultData

    Ranagal
  • BrunoVroman
    BrunoVroman BelgiumMember Posts: 1,848 Silver Crown
    edited Sep 13, 2019 11:45AM

    Hello Vemula,

    what default value? And maybe it depends of the reason for "no match" (for example: no such emplid but assignemnt_id and assign_sts and start_dt end_dt OK for at least 1 other emplid?... )

    Or simply:

    WITH your_query AS
    ( SELECT b.project_id, ... FROM ... WHERE ... )
    SELECT NVL( b.project_id, 'your_default_value' ) project_id
      FROM dual
      LEFT OUTER JOIN your_query ON 1 = 1
    ;

    (maybe you want something like "No such combination!" as default value?

    Best regards,

    Bruno Vroman.

    Vemula Muni
  • jaramill
    jaramill Member Posts: 4,299 Gold Trophy
    edited Sep 13, 2019 1:09PM

    Yes I agree with @L. Fernigrini  this is NOT your first post.  Please follow the guidelines and also....don't leave threads unmarked answered!  You have 3 of them recently where you just disappear

    pastedImage_0.png

    Frank Kulash