Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3978696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:04:47+00:00 2026-05-20T05:04:47+00:00

There is a complex query which generates a report. The query has several sub

  • 0

There is a complex query which generates a report. The query has several sub queries that generate 3-columns table for different products. Each sub query returns one row. All returned rows then need to be united.
But there is one requirement. If there are no result rows for a sub query we need to include the corresponding product to the final report anyway, but specify that Trades_Count is equal to zero.

I can achieve this using set of variables. The following code will work perfectly in MS SQL Server:

DECLARE @PRODUCT_NAME_1 nvarchar(100);
DECLARE @OFFER_VALID_DATE_1 datetime;
DECLARE @TRADES_COUNT_1 int;

DECLARE @PRODUCT_NAME_2 nvarchar(100);
DECLARE @OFFER_VALID_DATE_2 datetime;
DECLARE @TRADES_COUNT_2 int;

--Product 1 
select @PRODUCT_NAME_1 = PRODUCT_NAME, @OFFER_VALID_DATE_1 = MAX(EXPIRY_DATE), @TRADES_COUNT_1 = COUNT(DEAL_NUMBER)
from (
        --Data extractions with several joins goes here....

) as TempTable1
GROUP BY PRODUCT_NAME


--Product 2
select @PRODUCT_NAME_2 = PRODUCT_NAME, @OFFER_VALID_DATE_2 = MAX(EXPIRY_DATE), @TRADES_COUNT_2 = COUNT(DEAL_NUMBER)
from (
        --Data extractions with several joins goes here....
) as TempTable2
GROUP BY PRODUCT_NAME


SELECT ISNULL(@PRODUCT_NAME_1,'Product 1') AS PRODUCT_NAME, @OFFER_VALID_DATE_1 AS MAX_MATURITY, ISNULL(@TRADES_COUNT_1,0)
UNION
(
SELECT ISNULL(@PRODUCT_NAME_2,'Product 2') AS PRODUCT_NAME, @OFFER_VALID_DATE_2 AS MAX_MATURITY, ISNULL(@TRADES_COUNT_2,0)
)

I think that I haven’t used anything T-SQL specific, but pure ANSI-SQL (I’m not 100% sure though).

So this is not working in Oracle.

First of all it requires having only one DECLARE keyword. Then it forces me using Begin … End execution scope. Then it doesn’t allow me to assign variables like I do (see example above) – I need to use “Select INTO” statement instead. After all calculations are done it doesn’t allow me selecting values from local variables. Heck.

Does anyone know how to make it work in Oracle?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:04:48+00:00Added an answer on May 20, 2026 at 5:04 am

    PL/SQL is different than t-sql, I did a change with some comments for you, but definitely look at the links from Andy. This was ran in oracle’s free SQL Developer (which also has a “Translation Scratch Handler (tools>Migration>Translation Scratch Handler) that may be of use.

    --this creates a refcursor to allow us to simply print the results
    var refc refcursor
    /
    
    declare --here we declare our variables
        product_name_1 varchar2(15) ;
        offer_valid_date_1 date ;
        trade_count_1 number ;
        product_name_2 varchar2(15) ;
        offer_valid_date_2 date ;
        trade_count_2 number ;    
    begin
        begin --this creates a block so we may handle any exceptions just to this
              select PRODUCT_NAME,    MAX(EXPIRY_DATE),    COUNT(DEAL_NUMBER)
                into product_name_1 , offer_valid_date_1 , trade_count_1
                --in oracle you select INTO, not var=COL
            from (
                    --Data extractions with several joins goes here....
                    select 
                        123 PRODUCT_NAME,    
                        sysdate EXPIRY_DATE,    
                        5 DEAL_NUMBER
                    from dual --this is a 'fake' table to generate some data for testing
    
            )  TempTable1 --drop the "as"
            GROUP BY PRODUCT_NAME ;
        exception --if not data is found, then this error is thrown
                  --if multiple values are thrown an error will also be thrown (not caught here)
        when no_data_found then
            product_name_1 := null ; --note, to do a var = , we use "var := value;"
            offer_valid_date_1 := null;
            trade_count_1 := null;
        end ;
        begin
              select PRODUCT_NAME,    MAX(EXPIRY_DATE),    COUNT(DEAL_NUMBER)
                into product_name_2 , offer_valid_date_2 , trade_count_2
                --in oracle you select INTO, not var=COL
            from (
                    --Data extractions with several joins goes here....
                    select 555 PRODUCT_NAME,    sysdate EXPIRY_DATE,    6 DEAL_NUMBER
                    from dual
    
            )  TempTable2 -- drop the "as"
            GROUP BY PRODUCT_NAME ;
        exception --if not data is found, then this error is thrown
                  --if multiple values are thrown an error will also be thrown (not caught here)
        when no_data_found then
            product_name_2 := null ;
            offer_valid_date_2 := null;
            trade_count_2 := null;
        end ;
    
        open :refc for  --you cannot just have a select statement, you must "open" a cursor for it    
        --oracle IsNull is NVL (or NVL2 or you can do a case or decode...)
        SELECT nvl(PRODUCT_NAME_1,'Product 1') AS PRODUCT_NAME
              , OFFER_VALID_DATE_1 AS MAX_MATURITY
              , nvl(TRADE_COUNT_1,0)
          FROM DUAL --you also must have a table, DUAL is an oracle table for this tasks
            UNION
       SELECT nvl(PRODUCT_NAME_2,'Product 2') AS PRODUCT_NAME
              , OFFER_VALID_DATE_2 AS MAX_MATURITY
              , nvl(TRADE_COUNT_2,0)
        FROM DUAL;
    
    end ;
    /
    
    --now print the results, if you did this in a proc you would simple have this as an output
    print refc;
    
    -------------
    PRODUCT_NAME MAX_MATURITY              NVL(:B1,0)             
    -------------------------------------- ---------------------- 
    123          18.FEB.2011 08:43         1                      
    555          18.FEB.2011 08:43         1                      
    

    Oracle concepts used here:
    Dual Table , NVL, Variables, pl/sql Exception

    and look at this http://www.dba-oracle.com/t_convent_sql_server_tsql_oracle_plsql.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to generate LINQ queries which are 100% unknown during coding (design time).
I have a very complex MySQL query that includes use of the same subquery
I notice in several API's, that you may create a struct which is used
Is there a way to complex databind to a column's header text? Or is
When I need some complex algorithm I first check if there's anything relevant already
I wouldn't dare do anything complex in a database without transactions. There is nearly
I use MySQL in a fairly complex web site (PHP driven). Ideally, there would
I'm writing an ASP.NET 3.5 web application. There is a rather complex input form
I have an idea for a web-based service. The implementation is very complex. There
I run a rather complex project with several independent applications. These use however a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.