return the empty record only when no results not returned by the main query

It is a question of SQL (not PL/SQL)...

Let's say I have the following text:

Full_name SELECT NULL, NULL, NULL last_update_date address
OF the double
UNION ALL
SELECT full_name, address, last_update_date
Employees
WHEN TRUNC (sysdate) = TRUNC (last_update_date)

If the second part of the query retrieves no rows, it works beautifully and I get a blank line; However, if the second part of the query returns records, I get a blank line over my dataset I want.

How can I create a SQL statement that executes only the first query (before the union all) when no result is returned from the second. Also, I don't want to exceute the query twice only if I can get a count of the records because my actual query is much more complex than the example I've provided here.

Thanks in advance!
Emily
Advertisement

Hello

You can try to use a subquery factoring (WITH clause)
The query is executed only once, the query results are cached in memory or in a temporary table.
and you can view these results several times in the main query:

WITH second_query AS (
  -- your second query here
  SELECT full_name, address, last_update_date
  FROM employees
  WHERE TRUNC(sysdate)=TRUNC(last_update_date)
)
SELECT full_name, address, last_update_date
FROM second_query
UNION ALL
SELECT NULL full_name, NULL address, NULL last_update_date
FROM dual
WHERE NOT EXISTS ( SELECT 1 FROM second_query)

Published by: kordirko on 2010-07-08 02:02

Tags: Database

Similar Questions

  • First, select records only when the criteria correspond to?

    Hello guys!

    I read about a problem all day without finding a solution.

    The problem is the following:

    I have a vision, which is based on 2 other views including 1 uses a database link. (Not a problem yet because it works as it should!)

    In the result of this point of view, I might record n, that have the same attributes 'STR_GEBIET', 'STR_LRT_SUB' and 'INT_QB_NR '.
    My intention is to limit the query result to one of these records only. If I had 5 records the same 'STR_GEBIET', 'STR_LRT_SUB' and 'INT_QB_NR' the query must return only one of these recordings and discard the others. If there is no duplicate, he should select all records.

    I hope I could explain my problem? I would be really grateful for your contribution to this problem because I don't see any appropriately to solve.
    By selecting the rownum does not work with the joined views and so I'm kind of lost.

    The select query for my opinion is as follows:
    select
    x.CNT_QB_MAIN, 
    y.LNG_QB_SUB,
    x.STR_GEBIET, 
    x.STR_LRT_SUB, 
    x.INT_LRT_ANTEIL, 
    x.STR_BE, 
    x.INT_QB_NR, 
    x.INT_FLAECHE, 
    sum(x.INT_FLAECHE) over (partition by x.INT_QB_NR || x.STR_GEBIET || x.STR_LRT_SUB),
    x.INT_TB_NR,
    y.STR_BEARBEITER,
    y.INT_STATUS,
    y.STR_BEMERKUNG,
    y.INT_BEZUG_VERJUENG,
    y.INT_BEZUG_BESTAND,
    y.INT_BEZUG_TOTHOLZ,
    y.INT_BEZUG_BIOTOPBAUM,
    G.CNT_GEBIET,          
    G.STR_NAME,                
    G.STR_BESCHREIBUNG         
    FROM VT_TBL_GEBIET G
    INNER join VT_N2K_LRT_FL X
    ON G.CNT_GEBIET = X.STR_GEBIET
    LEFT OUTER JOIN VT_TBL_QB Y
    ON Y.LNG_QB_SUB = X.CNT_QB_MAIN
    Thanks for your help! You are great!

    Kind regards
    Sebastian

    Hello

    Have you tried rownum, but have you also tried row_number()?

    Something like:

    not tested

    select *
    from ( select x.cnt_qb_main
           ,      y.lng_qb_sub
           ,      x.str_gebiet
           ,      x.str_lrt_sub
           ,      x.int_lrt_anteil
           ,      x.str_be
           ,      x.int_qb_nr
           ,      x.int_flaeche
           ,      sum(x.int_flaeche) over (partition by x.int_qb_nr || x.str_gebiet || x.str_lrt_sub)
           ,      row_number() over (partition by x.int_qb_nr, x.str_gebiet, x.str_lrt_sub order by x.int_qb_nr, x.str_gebiet, x.str_lrt_sub) rn
           ,      x.int_tb_nr
           ,      y.str_bearbeiter
           ,      y.int_status
           ,      y.str_bemerkung
           ,      y.int_bezug_verjueng
           ,      y.int_bezug_bestand
           ,      y.int_bezug_totholz
           ,      y.int_bezug_biotopbaum
           ,      g.cnt_gebiet
           ,      g.str_name
           ,      g.str_beschreibung
           from   vt_tbl_gebiet g
           inner join vt_n2k_lrt_fl x on g.cnt_gebiet = x.str_gebiet
           left outer join vt_tbl_qb y on y.lng_qb_sub = x.cnt_qb_main
         )
    where rn=1
    

    And one more thing: creating views on views is something you should avoid.
    In your case it is perhaps best to use a view that selects the distinct values for x.int_qb_nr, x.str_gebiet and x.str_lrt_sub instead of limit thereafter by using Analytics already... It'll be less overhead.

    Published by: hoek on 2 Sep, 2009 10:47 Oops, removed concats action copy/paste in row_number

  • Problem with messages sent by post El Capitan: my sent messages appear only when someone answers, not in my "Sent" folder as they did.  Can anyone help?

    Problem with messages sent by post El Capitan:

    My sent messages appear only when someone answers, i.e., in the context of a thread.

    not in my folder "sent" as they did.  Can anyone help?

    The mailbox list is divided into categories with such topics on my Mac. When you position the cursor on one of these positions (with the exception of the mailboxes), you should see the Word display / hide it on the right. Click view, if that's what you see.

    In the category of the mailboxes, the mailboxes are arranged in groups, such as Inbox. To the left of each group is a small tip of arrow. If the arrow points to the right, click it so that it points downwards.

    Also note that you may need to scroll the list of mailboxes upwards or downwards to see it all.

  • subquery returns no row in the main query, but hard returns the value of the master query

    Oracle 11g, Solaris SPARC 64-bit:

    This is the master query: its not to return all the lines

    Select count (*) in the sadmin.usr_mods where to_char (ts_id) in ((select to_char (rtrim (ltrim(TS_MODULES,','), ',')) in the sadmin.usr_rfc where TS_KINTANA_PACKAGE_NUMBER = '123' and ts_projectid = 3));

    COUNT (*)

    ----------

    0

    But wherever if I use subquery only returns he lines:

    SQL > select rtrim (ltrim(TS_MODULES,','), ',') from the sadmin.usr_rfc where TS_KINTANA_PACKAGE_NUMBER = '123' and ts_projectid = 3;

    RTRIM (LTRIM(TS_MODULES,','), ',')

    --------------------------------------------------------------------------------

    49,54

    If I use these values as hardcoded to a select master it will return 2 rows:

    SQL > select count (*) in the sadmin.usr_mods where to_char (ts_id) in ('49 ', ' 54');

    COUNT (*)

    ----------

    2

    TS_MODULES datatype is CLOB

    TS_ID datatype is set to number

    Help, please

    with

    usr_rfc as

    (select 3 ts_projectid, 123 ts_kintana_package_number, to_clob(',49,54,') ts_modules

    of the double

    ),

    usr_mods as

    (select ts_id 49 Union double all the)

    Select 50 in all double union

    Select double 54

    )

    Select count (*)

    of usr_mods

    where to_char (ts_id) in (select regexp_substr (to_char (rtrim (ltrim(TS_MODULES,','), ',')),'[^,] +', 1, level))

    of usr_rfc

    where TS_KINTANA_PACKAGE_NUMBER = '123'

    and ts_projectid = 3

    connect by level<= regexp_count(to_char(rtrim(ltrim(ts_modules,','),',')),',')="" +="">

    )

    COUNT (*)
    2

    Concerning

    Etbin

    Select count (*)

    of usr_mods

    where instr ((select ',' | to_char (rtrim (ltrim(TS_MODULES,','), ',')) |)) ','

    of usr_rfc

    where TS_KINTANA_PACKAGE_NUMBER = '123'

    and ts_projectid = 3

    ),

    ',' || TO_CHAR (TS_ID) | ','

    ) > 0

  • [BUG] Timeline poster frame empty or invisible when it is not empty. (Flash CC 2014)

    Hello! I don't know why flash does, but at a certain point in my timeline a keyframe appears to be when the scene clearly shows a clip. Everything works normally, the clips spread properly and I can double click in and out of the it scene, I can even point to one of the apparently empty frames where the clip exists and it selects the clip on the stage. If it's not hinder my work, but it is a little disconcerting. y at - it anyone who has experienced this bug and found a way to solve it? I think it might have something to do with this flash being excessively long, 18 177 image file. But I'm not sure that this is necessarily the cause.

    I tried to restart flash and my computer.

    I tried to copy and paste the clip somewhere else on the timeline but it always also has invisible, or empty, in the timeline. It does same when I drag the clip on the stage from the library.

    I tried to copy the entire file in a new flash file and the same two clips are still invisible.

    Other clips from the library also refuse to appear in the calendar when you drag from the library onto the stage in the same place on the timeline. Clips placed on the stage, or created prior to this point in the timeline normally appear on the timeline.

    Screenshot:

    Screen Shot 2015-05-27 at 1.38.21 PM.png

    You can see how the clip is selected, indicated by the blue box on the stage and the frame of the timeline is selected, but the border appears as empty. This is clearly not empty. Any ideas?

    I don't know if it's important but:

    -two clips at the end of the animation are those in question, an image 18 177 file.

    -I am running on an iMac of 2013, OS 10.10.3, Adobe Flash CC 2014 build: 14.2.0.20

    It is not really a bug, it's a limitation, that you get also in other cases, the limit of width for bitmapdata instance.

    kglad solution works for frames up to 32000, and could in theory it takes an another movieclip in the last image of the movieclip inside, give you another 16000 images and so on. But there is another approach that might work for frames up to 256 million.

    I see that in your scenario, you have video clips which last as long as the sound that accompanies it. You could place each of these clips in consecutive images on the main timeline, and on every image, you would also have a sound event audio for this movieclip. Or you could put the audio of this movieclip into the movieclip itself, as its flow.

    In the first frame of the main timeline, you would stop, and action in the last frame of each movieclip, you might have this script:

    MovieClip (parent) .gotoAndStop (MovieClip (parent) .currentFrame + 1);

    When you play this at the playhead stops on frame 1 of the scenario main, then movieclip 1 will play to its effect, its own internal audio stream, either with a scenario main audio event. When it gets to the last image, it will tell the main timeline to go to frame 2, where movieclip 2 would play through his own, and the same script on its last image.

    In this way, you can have 16000 images on main timeline containing 16000 footage, 16000 views each.

  • OfficeJet 6700: ADF works only when you scan not copy

    Our officejet 6700 has an irrational problem - will take the ADF and scanning a document using the pc without problem, but if load you something using the function copy on the machine itself, he acknowledges that he is a responsible original, you hit copy and is not feeding the sheet through and copy from scratch!

    Any ideas?

    Thank you

    Hello @edtech,

    Welcome to the Forums of HP Support! I see that you are not able to use the ADF on your HP Officejet 6700 Premium to make Copies, but you can use it to perform a scan.

    I would like you to do a hard reset:

    1. disconnect all cables and cards of memory in the printer. Unplug the power cord, directly from the back of the printer when the printer is still on.

    2. unplug the other end of the power cord.

    3. If there is a power supply on the power cord, disconnect one end and reconnect it.

    4. at the end of 60 seconds, plug it directly into a wall outlet (not a surge protector or battery backup because they can cause the printer to lose over time basic functionality).

    5. connect the power cord to the back of the printer.

    6. the printer should automatically power back upward. If not, turn it on with the power button.

    7. try to make a photocopy (if your printer is a printer all in one) before you connect the cables from the printer.

    8. connect the other cables needed to the printer.

    Are now able to make a copy using the ADF? If you are still unable to make a copy, you will need to contact our technical support at www.hp.com/contacthp/. If you do not live in the United States / Canada region, please click the link below to get help from your region number. http://WWW8.HP.com/us/en/contact-HP/WW-phone-assist.html

    Thank you for being a part of the community of HP. Please let me know if this helps solve the problem by marking this message as "accept as Solution" . If you need more assistance let me know and I will gladly do whatever I can to help. If you want to say 'Thank you' to my effort to help, click on the "thumbs up" to give me a Kudos.

  • Oracle RTD records show when he is not at all used

    Hi all

    We do not use Oracle RTD however, on log files, we have some errors related to the Oracle RTD (RTD-10158: call to BatchManager.registerAgent...) We checked decision in Real-time system time OBIEE 11 g. is there a way to disable RTD Oracle?

    Thank you

    Sushil

    I would check the newspapers once by prepared under status.

  • Void results use of query in the main query.

    I need to query a specific parameter in a table called parameter_id which will return a list of location code using those should I then ask another table for values of max (date_time) in another table.

    Select location_id FROM TABLE1

    WHERE PARAMETER_ID ='ELEV_POOL;

    SELECT

    TABLE1. PARAMETER_ID,

    TABLE1. CWMS_TS_ID,

    TABLE2. VALUE,

    TABLE1. BASE_LOCATION_ID,

    TABLE2. DATE_TIME,

    FROM TABLE2

    JOIN IN-HOUSE

    TABLE1:

    WE

    TABLE1. CWMS_TS_ID = TABLE2. CWMS_TS_ID

    WHERE

    TABLE1. CWMS_TS_ID LIKE '% - REV'

    AND TABLE2. UNIT_ID = ('FT')

    AND TABLE2. VALUE IS NOT NULL

    AND (TABLE2. CWMS_TS_ID, TABLE2. DATE_TIME) IN

    (SELECT CWMS_TS_ID, MAX (DATE_TIME)

    FROM TABLE2

    GROUP OF TABLE2. CWMS_TS_ID)

    ;

    And it does not work?

    SELECT  TABLE1.parameter_id,
    TABLE1.cwms_ts_id,
    TABLE2.value,
    TABLE1.base_location_id,
    TABLE2.date_time
    FROM
    TABLE2
    INNER JOIN
    TABLE1
    ON
    TABLE1.cwms_ts_id = TABLE2.cwms_ts_id
    WHERE
    TABLE1.cwms_ts_id LIKE '%-rev'
    AND TABLE2.unit_id = ('ft')
    AND TABLE2.value IS NOT NULL
    AND TABLE1.base_location_id IN ( SELECT BASE_LOCATION_ID
                                     FROM Table1
                                     WHERE PARAMETER_ID = 'Elev-Pool'
                                     )
    AND (TABLE2.cwms_ts_id, TABLE2.date_time)
    IN
    (SELECT cwms_ts_id, Max(date_time)
    FROM TABLE2
    GROUP BY TABLE2.cwms_ts_id);
    
  • How to use a formula in column in the main query?

    Hi all
    I tried to use a formula columns defined in a query in the State of the query like this:
    where (: cf_ex -: cf_ex2) > = 5
    but when I run the report returned data! Why? and how to use the status of the query?

    Note: I'm using Forms 6i

    I gave you the reason:

    Formula columns are not part of the select statement (which runs in the database), but are dealt with in the report.
    So you actually reference the user settings in the query, not formulas columns.

    (Well, I should have said "you are referencing empty connection parameters", but with your experience you could have understood that)
    and the solution:

    Create database for cf_ex and cf_ex2 functions and use them in your application.

  • * How to display the group from the main query for the first time in the report?

    Hello

    I have developed a group of master / detail in a report
    The master has its extensible framework and the detail of one another framework extensible and put to another page in the page layout.
    The problem is that the master one appears (the same record) in the pages in time equal the number of records in detail that is specific for this master record!
    Example:
    If the master record got three Details then database will show three times (three pages) and then running page number 4 will display the three detail records.

    How to make the master data in shown only for once (page number 1 only) and then display the details three records to the next page (page number2) at run time?

    Hello

    the question is, what you've done for this. By default the primary record is displayed only once. Could it be, that the master-fields are inside the extendable frame for more details?

    Concerning
    Rainer

  • Title of view when no results

    Hello

    Is it possible to display the title view only when no results?

    Thank you
    Sirjacobs

    Hello
    Try to use the guided navigation and show the report with the single title discovers when returns no results (or) another way is, you can have a no display of results of your choice in the same report.

    Steps guided way-
    (1) create a report with the only title discovers say r1 and r2 is the report itself.
    (2) r2 here itself will act as interim report.
    (3) edit dashboard and shoot two sections in a section pulling other traction r2 and r1.
    (4) now go to section properties and guided naviagtion section with r1 and r2, select report as interim report and show that if ask returns nothing.
    (5) in the same way go to properties of section and guided naviagtion section with report r2 and select report r2 as interim report and see that if ask return lines.

    I hope this helps.

    Kind regards
    Srikanth

  • How to filter records in a main query of RDF based on the values of the calculated fields

    Hello

    I'm working on a report of RDF, where I have my main request which has four fields.

    (1) the unique id for the field

    (2) numerical value 1

    (3) numeric value 2

    (4) numeric value 3

    I added a CF_Total of calculated field.

    The main query returns all the records, but I want to only show that these fields for which the CF_Total has a particular value.

    Can anyone help me about how to proceed.

    Concerning

    Hawker

    Hello Hawker,

    This is an example, how you can filter based on the formula column. I don't know exactly where you would use. And other related issues.

    Give the idea and example how you can start.

    Thank you

    Hamid

  • Subselect query returns "invalid identifier", but the nested query return lines

    I don't think it's a general SQL question.

    Select * from persons where person_id in)

    Select person_id with people whose name = 'Obama' - subquery

    ) and age > 18;

    When I run the subquery, I get:

    ORA-00904: "PERSON_ID": invalid identifier

    00904, 00000 - '% s: invalid identifier '.

    * Cause:

    * Action:

    Error on line: column 5: 8

    This is because the table people do not have the person_id field.

    But when I run the nested together query it returns all the lines in people with the AGE greater than 18.

    How is he succeeds when the subquery is obviously wrong?

    363f652b-263D-4418-933F-74a1d0a41b4c wrote:

    I don't think it's a general SQL question.

    Select * from persons where person_id in)

    Select person_id with people whose name = 'Obama' - subquery

    ) and age > 18;

    When I run the subquery, I get:

    ORA-00904: "PERSON_ID": invalid identifier

    00904, 00000 - '% s: invalid identifier '.

    * Cause:

    * Action:

    Error on line: column 5: 8

    This is because the table people do not have the person_id field.

    But when I run the nested together query it returns all the lines in people with the AGE greater than 18.

    How is he succeeds when the subquery is obviously wrong?

    Yes - this is a general SQL question and ask often enough.

    Correlated subqueries depend on the inner query, be able to see and access to the columns of the outer query. Normally see you referenced in the WHERE clause of the subquery and not in the SELECT clause, but the reference is valid in both places. This works because the columns of the tables in the main query are accessible in the subquery. "Person_id" is probably a column in the table 'people '.

    Which can be a cause of problems 'odd' when the column (in your case "person_id") is more of a table.

    Use an alias in the subquery in the subquery and you will find that it will not succeed.

    See these two articles AskTom where he addresses this specific issue

    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:3317493900346468494

    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:155200640564

  • How to preset the order of the lines in the outer query of a correlated query?

    Hello

    I have the following simple query:
    select empno,
           ename,
           sal,
           sum(case
                 when rn = 1 then sal
                 else -sal
               end) over (order by sal, empno) as running_diff
       from (
             select empno,
                    ename,
                    sal,
                    row_number() over (order by sal, empno) as rn
               from emp
              where deptno = 10
             );
    Who calculates a difference running and uses «row_number() over (...)» which is a specific feature of the Oracle to do it. We get the following result (that we will consider correct):
         EMPNO ENAME             SAL RUNNING_DIFF
    ---------- ---------- ---------- ------------
          7934 MILLER           1300         1300
          7782 CLARK            2450        -1150
          7839 KING             5000        -6150
    I wanted to arrive at a different solution from the solution, it was not a specific Oracle . I tried the following code:

    (EDIT: after further thought, this code is entirely different sense and will never get closer, the result above.) Considers that it is wrong and ignore this attempt.)
    select a.empno,
           a.ename,
           a.sal,
           (select case
                     when a.empno = min(b.empno) then sum(b.sal)
                     else sum(-b.sal)
                   end
              from emp b
             where b.empno <= a.empno
               and b.deptno = a.deptno) as running_diff
      from emp a
     where a.deptno = 10;
    but the result is
         EMPNO ENAME             SAL RUNNING_DIFF
    ---------- ---------- ---------- ------------
          7782 CLARK            2450         2450
          7839 KING             5000        -7450
          7934 MILLER           1300        -8750
    that is a long way from the initial result. I tried everything I could think to order lines before running difference is calculated, but were unsuccessful.

    Is there a way to change this second request-(without using Oracle specific features) - without the help of windowing functions that give the same result as the first query?

    Rephrase the question above:

    Is it possible, using plain vanilla SQL (which is the aggregate functions and operations such as joins and unions) to create a query that produces the same result as the first?

    In addition, it is not for production code. It's just an exercise in manipulation set I would like to see a solution for.

    Thank you for your help,

    John.

    Published by: 440bx - 11 GR 2 on July 18, 2010 12:50 AM - correct ' ho w "to"how ".

    Published by: 440bx - 11 GR 2 on July 18, 2010 01:42 - struck all references to row_number and windowing being features of Oracle functions.

    Published by: 440bx - 11 GR 2 on July 18, 2010 03:51 - pointed out that my essay is terribly wrong and it had reaffirmed the goal to make it clearer.

    Hi, John,.

    A way to get a total operating (which is essentially what you want) must make a self-join. Join each line (let's call it the current line, or c) for himself and everything that preceded it (call this the previous line or p), and do a regular lump SUM, like this:

    WITH     got_base_sal     AS
    (
         SELECT       deptno
         ,       2 * MIN (sal)     AS base_sal
         FROM       scott.emp
         GROUP BY  deptno
    )
    SELECT       c.deptno
    ,       c.empno
    ,       c.ename
    ,       c.sal
    ,       b.base_sal - SUM (p.sal)     AS running_diff
    FROM       scott.emp     c
    JOIN       scott.emp     p     ON     c.deptno     = p.deptno
                        AND     (     c.sal     > p.sal
                             OR     (     c.sal     =  p.sal
                                  AND     c.empno     >= p.empno
                                  )
                             )
    JOIN       got_base_sal     b     ON     c.deptno     = b.deptno
    WHERE       c.deptno     IN (10)
    GROUP BY  c.deptno
    ,       c.empno
    ,       c.ename
    ,       c.sal
    ,       b.base_sal
    ORDER BY  c.deptno
    ,       running_diff     DESC
    ;
    

    Output:

        DEPTNO      EMPNO ENAME             SAL RUNNING_DIFF
    ---------- ---------- ---------- ---------- ------------
            10       7934 MILLER           1300         1300
            10       7782 CLARK            2450        -1150
            10       7839 KING             5000        -6150
    

    I said you basically want a total runninng. There are two differences between a cumulative and your needs
    (1) you want to have a total of the negation of what is in the table. That's trivial: use a sign less.
    (2) you want the first point to count as positive rather than negative. It's not so trivial. The above query counts all know as negative, but adds an offset to make it appear as if the first item had been posted as a positive, not negative result.

    You don't say what you want to do in the case of a tie (two or more lines having the same sal). The above query uses empno as a tiebreaker, so all sals are calculated as if they were separate. This is similar to what the analytical functions do when the window comes from the ranks. If you want something similar to windowing by scope, which could actually be simpler.

    The above query calculates a running_diff separate for each deptno, similar to "PARTITION BY deptno" in analytic functions. You happen be interested in a single deptno right now, but you can change the WHERE clause of the main query, or to omit, and the query still works. If you don't want this feature (analagoud for not having any PARTITION BY), it is easy to change the query.

    You can also get these results by using a WITH recursive clause. Which meets the criteria to avoid analytical functions and features specific to Oracle, but not on the use of the only clear and simple SQL features.

  • Dynamic display of columns in the pivot query

    I have a table called STUDENT_SCORE. I need to display the avarage score/month for 2 years and the monthly score until today "month.

    Then assume that it is April 2009. The report will look like

    Roll_id 2007avg 2008avg Jan09-Mar09 Feb 09
    101 80.9 70.9 89.7 90.9 56.8
    102 70.9 23.9 87.2 90.0 76.8


    I tried a bit, but can't fix the monthly score display dynamically.


    DDL/DML is as below


    create the table STUDENT_SCORE
    (number of roll_id,
    date of mth_id,
    The partition number);


    insert into STUDENT_SCORE values
    (101, to_date (January 1, 2006 ', ' dd/mm/yyyy'), 67.5);
    insert into STUDENT_SCORE values
    (101, to_date (February 1, 2006 ', ' dd/mm/yyyy'), 77.5);
    insert into STUDENT_SCORE values
    (101, to_date (March 1, 2006 ', ' dd/mm/yyyy'), 87.5).
    insert into STUDENT_SCORE values
    (101, to_date (April 1, 2006 ', ' dd/mm/yyyy'), 27.5);
    insert into STUDENT_SCORE values
    (101, to_date (May 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (June 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (July 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (August 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (September 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (October 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (November 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (December 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (January 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (February 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (March 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (April 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (May 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (June 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (July 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (August 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (September 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (October 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (November 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (December 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (1 January 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (February 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (March 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (April 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (May 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (June 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (July 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (August 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (September 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (October 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (November 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (December 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (101, to_date (January 1, 2009 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (January 1, 2006 ', ' dd/mm/yyyy'), 67.5);
    insert into STUDENT_SCORE values
    (102, to_date (February 1, 2006 ', ' dd/mm/yyyy'), 77.5);
    insert into STUDENT_SCORE values
    (102, to_date (March 1, 2006 ', ' dd/mm/yyyy'), 87.5).
    insert into STUDENT_SCORE values
    (102, to_date (April 1, 2006 ', ' dd/mm/yyyy'), 27.5);
    insert into STUDENT_SCORE values
    (102, to_date (May 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (June 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (July 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (August 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (September 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (October 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (November 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (December 1, 2006 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (January 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (February 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (March 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (April 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (May 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (June 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (July 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (August 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (September 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (October 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (November 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (December 1, 2007 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (1 January 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (February 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (March 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (April 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (May 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (June 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (July 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (August 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (September 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (October 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (November 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (December 1, 2008 ', ' dd/mm/yyyy'), 57.5);
    insert into STUDENT_SCORE values
    (102, to_date (January 1, 2009 ', ' dd/mm/yyyy'), 57.5);

    Hello

    The script below produces what you want (as far as I can tell, without your desired results).

    Student_score_smry subquery Gets the year and monthly averages of student_score.
    It is completely static: it always produces 11 monthly columns (from January to November) and the names of columns hardcoded (m01 through m11).
    For dynamic column names you need, I used SQL * COLUMN more orders.
    To get the variable number of monthly columns you need, SELECT from the main query clause uses a script under (dynamic_month.sql) to only show the correct number (which can be 0).

    The student_score_wk_smry of subquery retrieves the weekly averages of the student_score_wk.
    He, too, is completely static: it always produces 6 weekly columns with names hardcoded (w1 through w6).

    There are four preliminary motions, that write two scripts under:
    Prelimiary C1, C2 and C3 queries write orders of COLUMN for annual, monthly and weekly columns in dynamic_columns.sql
    Preliminary motion M written between 0 and 11 items in SELECT-clause in sub-script dynamic_month.sql

    The main script is:

    SET     FEEDBACK     OFF
    SET     LINESIZE     200
    SET     PAGESIZE     0
    SET     VERIFY          OFF
    
    SPOOL     dynamic_column.sql
    
    -- Preliminary query C1: Columns for last 2 years
    SELECT     'COLUMN     y'
         ||     LEVEL
         ||     '     HEADING     "'
         ||     TO_CHAR     ( ADD_MONTHS     ( &target_month
                             , 12 * (-3 + LEVEL)
                             )
                   , 'YYYY'
                   )
         ||     'avg"'
    FROM     dual
    CONNECT BY     LEVEL <= 2
    ;
    
    -- Preliminary query C2: 11 prior months in this year
    SELECT     'COLUMN     m'
         ||     TO_CHAR     (LEVEL, 'fm00')
         ||     '     HEADING     "'
         ||     TO_CHAR     ( ADD_MONTHS     ( TRUNC (&target_month, 'YYYY')
                             , LEVEL - 1
                             )
                   , 'MonYY'
                   )
         ||     '"'
    FROM     dual
    CONNECT BY     LEVEL <= 11
    ;
    
    -- Preliminary query C3: 6 weeks before target_month
    WITH     got_w_num     AS
    (
         SELECT     SUBSTR (week_desc, 1, 6)     AS mon_dd
         ,     ROW_NUMBER () OVER
                   (     ORDER BY     week_id     DESC
                   )     AS w_num
         FROM     week
         WHERE     TO_DATE (week_desc, 'Mon DD, YYYY')     < &target_month
    )
    SELECT     'COLUMN     w'
         ||     TO_CHAR (7 - w_num)
         ||     '     HEADING     "Week of '
         ||     mon_dd
         ||     '"'
    FROM     got_w_num
    WHERE     w_num     <= 6
    ORDER BY     w_num     DESC
    ;
    
    SPOOL     OFF
    
    -- Preliminary Query M: 0-11 months (in SELECT clause)
    SPOOL     dynamic_month.sql
    
    SELECT     ',     m'
         ||     TO_CHAR (LEVEL, 'fm00')
    FROM     dual
    WHERE     MONTHS_BETWEEN (&target_month, TRUNC (&target_month, 'YYYY')) >= 1
    CONNECT BY     LEVEL <= MONTHS_BETWEEN (&target_month, TRUNC (&target_month, 'YYYY'))
    ;
    
    SPOOL     OFF
    
    -- Restore SQL*Plus features suppressed earlier
    SET     FEEDBACK     ON
    SET     LINESIZE     80
    SET     PAGESIZE     50
    
    -- COLUMN Commands
    @dynamic_column.sql
    
    -- Main Query
    WITH     student_score_smry     AS
    (
         SELECT     roll_id
         ,     AVG (CASE WHEN TRUNC (mth_id, 'YYYY') = TRUNC (ADD_MONTHS (&target_month, -24), 'YYYY')     THEN score END)     AS y1
         ,     AVG (CASE WHEN TRUNC (mth_id, 'YYYY') = TRUNC (ADD_MONTHS (&target_month, -12), 'YYYY')     THEN score END)     AS y2
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') =             TRUNC (&target_month, 'YYYY')      THEN score END)     AS m01
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  1) THEN score END)     AS m02
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  2) THEN score END)     AS m03
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  3) THEN score END)     AS m04
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  4) THEN score END)     AS m05
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  5) THEN score END)     AS m06
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  6) THEN score END)     AS m07
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  7) THEN score END)     AS m08
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  8) THEN score END)     AS m09
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'),  9) THEN score END)     AS m10
         ,     AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 10) THEN score END)     AS m11
         FROM     student_score
         GROUP BY     roll_id
    )
    ,     got_w_num     AS
    (
         SELECT     week_id
         ,     ROW_NUMBER () OVER
                   (     ORDER BY     week_id     DESC
                   )     AS w_num
         FROM     week
         WHERE     TO_DATE (week_desc, 'Mon DD, YYYY')     < &target_month
    )
    ,     student_score_wk_smry     AS
    (
         SELECT     roll_id
         ,     AVG (CASE WHEN W_NUM = 6 THEN score END)     AS w1
         ,     AVG (CASE WHEN W_NUM = 5 THEN score END)     AS w2
         ,     AVG (CASE WHEN W_NUM = 4 THEN score END)     AS w3
         ,     AVG (CASE WHEN W_NUM = 3 THEN score END)     AS w4
         ,     AVG (CASE WHEN W_NUM = 2 THEN score END)     AS w5
         ,     AVG (CASE WHEN W_NUM = 1 THEN score END)     AS w6
         FROM     student_score_wk     s
         JOIN     got_w_num          w     ON     s.wk_id     = w.week_id
         WHERE     w_num     <= 6
         GROUP BY     roll_id
    )
    SELECT     NVL (ss.roll_id, ssw.roll_id)     AS roll_id
    ,     y1, y2
    @dynamic_month.sql
    ,     w1, w2, w3, w4, w5, w6
    FROM          student_score_smry     ss
    FULL OUTER JOIN     student_score_wk_smry     ssw     ON     ss.roll_id     = ssw.roll_id
    ORDER BY     roll_id
    ;
    

    Sub-script dynamic_column.sql might look like this:

    COLUMN     y1     HEADING     "2007avg"
    COLUMN     y2     HEADING     "2008avg"
    COLUMN     m01     HEADING     "Jan09"
    COLUMN     m02     HEADING     "Feb09"
    COLUMN     m03     HEADING     "Mar09"
    COLUMN     m04     HEADING     "Apr09"
    COLUMN     m05     HEADING     "May09"
    COLUMN     m06     HEADING     "Jun09"
    COLUMN     m07     HEADING     "Jul09"
    COLUMN     m08     HEADING     "Aug09"
    COLUMN     m09     HEADING     "Sep09"
    COLUMN     m10     HEADING     "Oct09"
    COLUMN     m11     HEADING     "Nov09"
    COLUMN     w1     HEADING     "Week of Dec 21"
    COLUMN     w2     HEADING     "Week of Dec 28"
    COLUMN     w3     HEADING     "Week of Jan 04"
    COLUMN     w4     HEADING     "Week of Jan 11"
    COLUMN     w5     HEADING     "Week of Jan 18"
    COLUMN     w6     HEADING     "Week of Jan 25"
    

    Other sub-script, dynamic_month.sql, might look like this:

    ,     m01
    ,     m02
    ,     m03
    

Maybe you are looking for

  • How can I cancel Sync?

    People, I don't want to use Sync, but the pop up keeps appearing on my screen. In an attempt to cancel it, I tried to find a solution in the Help Section. I realized that I had to use Sync first in order to cancel it. So, I recorded and then canceled

  • How to export a picture with the filename

    iPhotos used to export a photo in a chosen directory, and then type a file name then export it. Apparently the 'upgrade' to Photos (ver. 1.2) means that I can give is no longer the photo file name specific when I export. The closest I can get, it's c

  • Windows 8 crashed on my Satellite C55-B815

    Hello! My Toshiba Satellite C55-B815, I bought last may 2014 suddenly as corrupted when I wrong trying to put out of service and it came out this 0xc000000e error message and he said my PC must be repaired using recovery media, but the problem is I d

  • Pavilion P6-2100: usb 3.0

    I just installed a USB 3.0 PCIe card.  Shows Device Manager Renesas host controller and root hub are working and the drivers are installed.  But when I insert a disc HP 128 GB flash (USB 3.0), nothing happens, it is not recognized.  If I insert a San

  • BlackBerry smartphones tilt 8900 9320 data

    HelloConfused and frustrated!I'm trying to swtich all 8900 (4.6.1.133) data to (7.1) 9320Do I need to install two different versions of the Desktop Manager? Or if I install the latest version, it'll work on the old 8900 How can I go about quickly Tha