Query to fetch the output in the given form.

Hi,
I have the following values in the table;
CURRENCY_MASTER -->
Curr_id Curr_name
1 Yen
2 Dollar
3 Rupee
4 Australian Dollar
5 Rubel
6 Taka
7 New Zealand dollar
8 Dinar
9 Euro
Output-
I want an output such that "Dollar" is at the top and the rest of them are arranged alphabetically.
Curr_name
Dollar
Australian Dollar
Dinar
Euro
New Zealand dollar
Rubel
Rupee
Taka
Yen
How to get this output.

Here's one way to do it:
test@ORA10G>
test@ORA10G> with t as (
  2    select 1 as curr_id, 'Yen' as curr_name from dual union all
  3    select 2 , 'Dollar' from dual union all
  4    select 3 , 'Rupee' from dual union all
  5    select 4 , 'Australian Dollar' from dual union all
  6    select 5 , 'Rubel' from dual union all
  7    select 6 , 'Taka' from dual union all
  8    select 7 , 'New Zealand dollar' from dual union all
  9    select 8 , 'Dinar' from dual union all
10    select 9 , 'Euro' from dual)
11  --
12  select
13    curr_id,
14    curr_name
15  from t
16  order by (case curr_name when 'Dollar' then '@'||curr_name else curr_name end);
   CURR_ID CURR_NAME
         2 Dollar
         4 Australian Dollar
         8 Dinar
         9 Euro
         7 New Zealand dollar
         5 Rubel
         3 Rupee
         6 Taka
         1 Yen
9 rows selected.
test@ORA10G>
test@ORA10G>pratz

Similar Messages

  • Can you please explain how this query is fetching the rows?

    here is a query to find the top 3 salaries. But the thing is that i am now able to understand how its working to get the correct data :How the data in the alias table P1 and P2 getting compared. Can you please explain in some steps.
    SELECT MIN(P1.SAL) FROM PSAL P1, PSAL P2
    WHERE P1.SAL >= P2.SAL
    GROUP BY P2.SAL
    HAVING COUNT (DISTINCT P1.SAL) <=3 ;
    here is the data i used :
    SQL> select * from psal;
    NAME SAL
    able 1000
    baker 900
    charles 900
    delta 800
    eddy 700
    fred 700
    george 700
    george 700
    Regards,
    Renu

    ... Please help me in understanding the query.
    Your query looks like anything but a Top-N query.
    If you run it in steps and analyze the output at the end of each step, then you should be able to understand what it does.
    Given below is some brief information on the same:
    test@ora>
    test@ora> --
    test@ora> -- Query 1 - using the non-equi (theta) join
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p1.sal AS p1_sal, p1.NAME AS p1_name, p2.sal AS p2_sal,
    12         p2.NAME AS p2_name
    13    FROM psal p1, psal p2
    14   WHERE p1.sal >= p2.sal;
        P1_SAL P1_NAME     P2_SAL P2_NAME
          1000 able          1000 able
          1000 able           900 baker
          1000 able           900 charles
          1000 able           800 delta
          1000 able           700 eddy
          1000 able           700 fred
          1000 able           700 george
          1000 able           700 george
           900 baker          900 baker
           900 baker          900 charles
           900 baker          800 delta
           900 baker          700 eddy
           900 baker          700 fred
           900 baker          700 george
           900 baker          700 george
           900 charles        900 baker
           900 charles        900 charles
           900 charles        800 delta
           900 charles        700 eddy
           900 charles        700 fred
           900 charles        700 george
           900 charles        700 george
           800 delta          800 delta
           800 delta          700 eddy
           800 delta          700 fred
           800 delta          700 george
           800 delta          700 george
           700 eddy           700 eddy
           700 eddy           700 fred
           700 eddy           700 george
           700 eddy           700 george
           700 fred           700 eddy
           700 fred           700 fred
           700 fred           700 george
           700 fred           700 george
           700 george         700 eddy
           700 george         700 fred
           700 george         700 george
           700 george         700 george
           700 george         700 eddy
           700 george         700 fred
           700 george         700 george
           700 george         700 george
    43 rows selected.
    test@ora>
    test@ora>This query joins PSAL with itself using a non equi-join. Take each row of PSAL p1 and see how it compares with PSAL p2. You'll see that:
    - Row 1 with sal 1000 is >= to all sal values of p2, so it occurs 8 times
    - Row 2 with sal 900 is >= to 9 sal values of p2, so it occurs 7 times
    - Row 3: 7 times again... and so on.
    - So, total no. of rows are: 8 + 7 + 7 + 5 + 4 + 4 + 4 + 4 = 43
    test@ora>
    test@ora> --
    test@ora> -- Query 2 - add the GROUP BY
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p2.sal AS p2_sal,
    12         COUNT(*) as cnt,
    13         COUNT(p1.sal) as cnt_p1_sal,
    14         COUNT(DISTINCT p1.sal) as cnt_dist_p1_sal,
    15         MIN(p1.sal) as min_p1_sal,
    16         MAX(p1.sal) as max_p1_sal
    17    FROM psal p1, psal p2
    18   WHERE p1.sal >= p2.sal
    19  GROUP BY p2.sal;
        P2_SAL        CNT CNT_P1_SAL CNT_DIST_P1_SAL MIN_P1_SAL MAX_P1_SAL
           700         32         32               4        700       1000
           800          4          4               3        800       1000
           900          6          6               2        900       1000
          1000          1          1               1       1000       1000
    test@ora>
    test@ora>Now, if you group by p2.sal in the output of query 1, and check the number of distinct p1.sal, min of p1.sal etc. you see that for p2.sal values - 800, 900 and 1000, there are 3 or less p1.sal values associated.
    So, the last 3 rows are the ones you are interested in, essentially. As follows:
    test@ora>
    test@ora> --
    test@ora> -- Query 3 - GROUP BY and HAVING
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p2.sal AS p2_sal,
    12         COUNT(*) as cnt,
    13         COUNT(p1.sal) as cnt_p1_sal,
    14         COUNT(DISTINCT p1.sal) as cnt_dist_p1_sal,
    15         MIN(p1.sal) as min_p1_sal,
    16         MAX(p1.sal) as max_p1_sal
    17    FROM psal p1, psal p2
    18   WHERE p1.sal >= p2.sal
    19  GROUP BY p2.sal
    20  HAVING COUNT(DISTINCT p1.sal) <= 3;
        P2_SAL        CNT CNT_P1_SAL CNT_DIST_P1_SAL MIN_P1_SAL MAX_P1_SAL
           800          4          4               3        800       1000
           900          6          6               2        900       1000
          1000          1          1               1       1000       1000
    test@ora>
    test@ora>
    test@ora>That's what you are doing in that query.
    The thing is - in order to find out Top-N values, you simply need to scan that one table PSAL. So, joining it to itself is not necessary.
    A much simpler query is as follows:
    test@ora>
    test@ora>
    test@ora> --
    test@ora> -- Top-3 salaries - distinct or not; using ROWNUM on ORDER BY
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT sal
    12  FROM (
    13    SELECT sal
    14      FROM psal
    15    ORDER BY sal DESC
    16  )
    17  WHERE rownum <= 3;
           SAL
          1000
           900
           900
    test@ora>
    test@ora>
    test@ora>And for Top-3 distinct salaries:
    test@ora>
    test@ora> --
    test@ora> -- Top-3 DISTINCT salaries; using ROWNUM on ORDER BY on DISTINCT
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT sal
    12  FROM (
    13    SELECT DISTINCT sal
    14      FROM psal
    15    ORDER BY sal DESC
    16  )
    17  WHERE rownum <= 3;
           SAL
          1000
           900
           800
    test@ora>
    test@ora>
    test@ora>You may also want to check out the RANK and DENSE_RANK analytic functions.
    RANK:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions123.htm#SQLRF00690
    DENSE_RANK:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions043.htm#SQLRF00633
    HTH
    isotope

  • RSBBS jump query not fetching the document no in R3

    Dear Gurus
    With RSBBS transcation jump is given to R3 tansaction FB03. When query is executed and with right click go to option and with display document (FB03) its fetching the document no in DEVELOPMENT server . But when the query is transported to Production its not fetching the document no.
    Kindly do the needful at the earliest
    Regards,
    R.Satish

    Hi
    You said it is not fetching the doc no. Is it failing and showing some error?
    Have all the pre-requisite settings been done via Tcode SICF.
    Regards
    Nageswara

  • Query on fetching the no.of days between Invoice date and due date in rtf template embedded BI Publisher Report

    Hi Experts,
    We have a requirement to fetch the value of 'No of days' between Invoice date and due date (Two variable date fields on the template) in an Analysis.
    Please let me know the procedure of how to achieve the same.
    Regards,
    Rev

    it's good for ideas but implementation a bit different
    Oracle Business Intelligence Publisher Report Designer's Guide
    This function provides a method to get the difference between two dates in the given locale. The dates need to be in "yyyy-MM-dd" format. This function supports only the Gregorian calendar. The syntax is as follows:
    <?xdoxslt:date_diff(‘format’, ‘YYYY-MM-DD’, ‘YYYY-MM-DD’, $_XDOLOCALE, $_XDOTIMEZONE)?>
    where
    format is the time value for which the difference is to be calculated
    Example:
    <?xdoxslt:date_diff(‘d’, ‘2006-04-08’, ‘2006-04-01’, $_XDOLOCALE, ‘America/Los_Angeles’)?>
    returns
    -7

  • Query not fetched the record

    Hello,
    Could someone help me please ?
    I have a listing of my sales orders and I want to make changes in my order by opening the form and fetched with that record. When I click on that particular orderno in my listing of order and call the form to display the details, it calls the form but says "Query could not fetch the record". I do not know why ? Please help me with the solution.
    Thanx

    Hello,
    I think you are passing orderno to called form as a parameter. If you are using parameter list check..
    1. If parameter data is getting in form correctly ?
    2. Next, have you changed where clause of other block,so that is will display record with passed orderno ?
    I am expecting more details from you.
    Thanx
    Adi

  • SQL query to fetch the catalog category for a PLM item

    Hi All,
    Can anyone help me with a sql query of how we can fetch the catalog category for a PLM item.
    Regards,
    Shruti

    Hi,
    You could monitor the SMSProv.log when you check the category of an application. It shows that SQL query the SCCM provider used is related to function fn_ListCategoryInstances.
    Best Regards,
    Joyce

  • Query to fetch the list of Journal Entry without cancelled JE.

    Dear All,
    I have tried the following query to get the list of JE without Cancelled JE.
    SELECT T0.[TransId] FROM OJDT T0 WHERE T0.[TransId] NOT  IN  (SELECT T0.[StornoToTr] FROM OJDT T0)
    It is not working . can any one solve this?
    Thanks in advance,
    Dwarak

    Hi,
    Check this :
    SELECT T0.TransId FROM OJDT T0 WHERE T0.TransId NOT IN (SELECT T0.StornoToTr FROM OJDT T0 where t0.stornototr is not NULL)
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Query to fetch the max date

    Hi All,
    Taking the below data as sample,we have a requirement wherein we have to fetch the date based on the following criteria.
    Item     Cost     Transaction Date
    A     23     12-feb-2012 1:00 AM
    P     23     12-feb-2012 2:00 AM
    B     24     13-feb-2012
    C     43     14-feb-2012 3:00 AM
    M     43     14-feb-2012 8:00 AM
    D     12     16-feb-2012
    User will pass date as the parameter and if the table has Transaction Date same as the date parameter then that date is picked.
    Eg: if the parameter passed is 12-feb-2012, since it is present in table then 12-Feb-2012 2:00AM will be fetched. The table has 2 records, the onewith latest date is picked.
    Now if the table does not have Transaction Date same as the date parameter then the max date previous to that date passed as parameter should be picked.
    Eg: if the parameter passed is 15-feb-2012, since 15- FEB-2012 is not present in table, it should pick 14-FEB-2012 8:00 AM (latest date).
    I am not able to frame queries for the above criteria.
    If someone can help me with the same, it would be of great help.
    Regards,
    Shruti

    Hi, Shruti,
    this does waht you requested:
    SELECT     MAX (transaction_date)     AS max_transaction_date
    FROM     table_x
    WHERE     transaction_date < TO_DATE ( :given_date
                              , 'DD-Mon-YYYY'
                           ) + 1
    ;where :given_date is a string, such as '12-feb-2012'.
    Since the call to TO_DATE doesn't specify hours, minutes or seconds, they will befault to 00:00:00, so '12-feb-1012' would mean mignight at the beginning of February 12. We want it to mean anything later on the same day; that's why I added 1 to the date, and used < instead of <=.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    See the forum FAQ {message:id=9360002}

  • Need Query to fetch the users related to a particular process.

    Using FDI DB, Is there a way to fetch the # of users related to a particular process.

    For example,
    If all the roles within a PROCESSP starts with 'ABCXXXXXXXXX',
    I am using SELECT DISTINCT(FUEGO_ID) FROM FUEGO_PART_ASSIGROLE WHERE FUEGO_ASSIGROLE LIKE 'ABC%' to get the count of the participants.
    Is it the correct way?

  • Sql query to get the given output

    Hi Friends
    My table data is given below
    user transaction_date transaction_type
    1 01-Aug-2011 a
    1 02-Aug-2011 c
    1 03-Aug-2011 a
    1 03-Aug-2011 b
    1 04-Aug-2011 a
    1 04-Aug-2011 b
    2 03-Aug-2011 a
    2 03-Aug-2011 b
    2 04-Aug-2011 c
    2 04-Aug-2011 b
    2 05-Aug-2011 a
    2 05-Aug-2011 b
    2 07-Aug-2011 a
    2 07-Aug-2011 b
    I want the count for each user as, how many times he did a transaction type 'b' immediately after transaction type a ?
    Like the output for above data should be
    user count
    1 2
    2 3
    Thanks in advance :)

    Assuming there was information in the transaction date to indicate the sequence of the transactions then it would be something like...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as usr, to_date('01-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') as transaction_date, 'a' as transaction_type from dual union all
      2             select 1, to_date('02-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'c' from dual union all
      3             select 1, to_date('03-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      4             select 1, to_date('03-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      5             select 1, to_date('04-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      6             select 1, to_date('04-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      7             select 2, to_date('03-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      8             select 2, to_date('03-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      9             select 2, to_date('04-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'c' from dual union all
    10             select 2, to_date('04-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
    11             select 2, to_date('05-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
    12             select 2, to_date('05-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
    13             select 2, to_date('07-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
    14             select 2, to_date('07-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual
    15            )
    16  --
    17  -- end of test data
    18  --
    19  select usr, sum(sum_ab) as sum_ab
    20  from (
    21        select usr
    22              ,case when transaction_type = 'b'
    23                     and lag(transaction_type) over (partition by usr order by transaction_date) = 'a'
    24               then 1
    25               else 0
    26               end as sum_ab
    27        from t
    28       )
    29* group by usr
    SQL> /
           USR     SUM_AB
             1          2
             2          3which is essentially the same as Etbin posted, but without the assumption that the ordering is by transaction_type.

  • Query to fetch the space occupied by  table

    i unable to get a query to find out the space occuppied by a partition in a table
    plz do the needful

    before that you need the analyze the table and use the below given query
    select table_name,(num_rows*avg_row_len)/(1024*1024) MB from user_tab_partitions
    Regards,
    Abu

  • How to fetch the tabular form column value into page items

    Hi,
    I have created tabular form. I have set LOV for one column. Here my requirement is when i select the value from lov then it will fetch that value into page item.
    can any help?
    I am using apex 4.1
    Thanks
    vijay

    It's not so easy to make a dynamic action on a tabular form.
    The best thing you can do is to use the javascript API from APEX: http://docs.oracle.com/cd/E23903_01/doc/doc.41/e21676/javascript_api.htm
    So if you have a tabular form then open your item and make it a column link.
    In the link location select URL.
    Your URL would be something like this
    javascript:$s('P1_PAGE_ITEM', #COLUMN_ID#);doSubmit('COLUMN_ID');The doSubmit is not required but if you don't submit your page then the server would never know about your page item.
    Off course it is possible to submit at a later point. For example you can just use the $s to set the value off an item and afterwards create a button that would submit your page.
    Be aware that if you just set the value off your page item and just link to an other page without submitting or without branching that other page would never know about your page item.
    If you want the server to know the value off a page item you need to submit it's value to the server. The easiest way is just to do the doSubmit() call after you have set your value.
    Hope this helps,
    Regards
    Nico

  • How to fetch the first part of string from a whole lenghty string

    Hi,
    I have a data like - ex1:
      Auto Update : 10/04/2014 08:04  NEW data: xxxx OLD data: yyyy Ack - etr03 : 10/04/2014 08:13
     Auto Update : 10/04/2014 15:24  NEW data1 :xxx  OLD data1: yyyy Auto Update : 10/04/2014 09:36  NEW data1: xxx1 OLD data1: yyy1 Auto Update : 08/04/2014 12:28  NEW data1: xxx2 OLD data1: yyy2 Ack - jmi08 : 10/04/2014 09:53 Ack -
    WWI13 : 10/04/2014 15:32
    I want to compare only NEW data: xxxx OLD data: yyyy or  NEW data1 :xxx  OLD data1: yyyy Auto Update : 10/04/2014 09:36  NEW data1: xxx1 OLD data1: yyy1 Auto Update : 08/04/2014 12:28  NEW data1: xxx2 OLD data1: yyy2with the same ste
    of value from another database but since I used substring I am getting even Ack - etr03 : 10/04/2014 08:13 value whose length changes for above 2 examples.
    USed substring to get NEW data1 :xxx  OLD data1: yyyy Auto Update : 10/04/2014 09:36  NEW data1: xxx1 OLD data1: yyy1 Auto Update : 08/04/2014 12:28  NEW data1: xxx2 OLD data1: yyy2 
    or
    NEW data: xxxx OLD data: yyyy
    but the ack part cannot be removed due to variable lenghts for each data hence could not compare this column between 2 databases. Any help would be highly appreciated.
    Thanks,
    Preetha

    HI ,
    THE INPUT IS
     EX 1:Auto Update : 10/04/2014 15:24  NEW data1 :xxx  OLD data1: yyyy Auto Update : 10/04/2014 09:36  NEW data1: xxx1 OLD data1: yyy1 Auto Update : 08/04/2014 12:28  NEW data1: xxx2 OLD data1: yyy2 Ack - jmi08 : 10/04/2014 09:53
    Ack - WWI13 : 10/04/2014 15:32
    EX 2: Auto Update : 10/04/2014 08:04  NEW data: xxxx OLD data: yyyy Ack - etr03 : 10/04/2014 08:13
    AND THE EXPECTED RESULT IS :
    EX1:NEW data1 :xxx  OLD data1: yyyy Auto Update : 10/04/2014 09:36  NEW data1: xxx1 OLD data1: yyy1 Auto Update : 08/04/2014 12:28  NEW data1: xxx2 OLD data1: yyy2
    EX2 : NEW data: xxxx OLD data: yyyy
    SINCE THE FILED LENGTH CHANGES I WANT TO WRITE A GENRIC QUERY WHICH FETCHES THE ABOVE DATA.I DO NOT WANT TO CREATE ANY TABLE
    Hi Preetha7,
    This make no sense. We cannot guess you database structure. 
    Please post DDL (Create table script)
    and DML (your current query)
    and expected output.
    For more info, please have a look at this thread:
    POSTING TIPS - Code, Images, Hyperlinks, Details
    Thanks!
    sqldevelop.wordpress.com

  • How to fetch the Alias column values based on different column values?

    Hello Gurus,
    I have a table with the following struture -
    "drop table T;
    create table T(Name, Symbol, Amount,dep) as select
    'Anderia', 'AA', 1050000,'HR' from dual union all select
    'Michael', 'ML',150000,'Sales' from DUAL union all select
    'Bill', 'BL', 1050000,'Finance' from dual union all select
    'Nancy', 'NY', 4000,'HR' from DUAL union all select
    'Anderia', 'AA',3000,'HR' from dual union all select
    'Michael', 'ML',1050000,'Sales' from DUAL union all select
    'Bill', 'BL', 1200000,'Finance' from DUAL union all select
    'Anderia', 'AA', 1200000,'HR' from DUAL union all select
    'Vish', 'VH', 1200000,'Marketing' from DUAL;"Ans try to find out the values of the column like
    Name,symbol,dep,Amount,%Total,$Cumm Total, Rank but some additional columns like -
    HR Amount, %HRTotal,$HR Cumm Total,
    Finance Amount, %FinanceTotal,$Finance Cumm Total
    Sales Amount, %SalesTotal,$Sales Cumm Total,
    Marketing Amount, %MarketingTotal,$Marketing Cumm Total
    then i am using the following query to fetch the Name,symbol,dep,Amount,%Total,$Cumm Total, Rank columns -
    select name
         , decode(grouping(symbol), 0, symbol, 'Total') symbol
         , dep
         , sum(amount) amount
         , sum(per_total) "% Total"
         , decode(grouping(symbol), 0, max(Cum_per_total), null) "% Cumm Total"
         , decode(grouping(symbol), 0, max(rank), null) rank
      from (
              select name
                   , symbol
                , dep
                   , amount
                   , per_total
                   , sum(per_total) over(order by rk) cum_per_total
                   , rank
                   , rk
                from (    
                        select name
                             , symbol
                    , dep
                             , sum(amount) amount
                             , round((sum(amount)/max(total_amount)) * 100,2) per_total
                             , dense_rank () over (order by sum(amount) desc) as rank
                             , row_number() over(order by sum(amount) desc) as rk
                          from (
                                 select name
                                      , symbol
                                      , amount
                          , dep
                                      , sum(amount) over() total_amount
                                      , sum(amount) over ()
                                   from t
                         group
                            by name, symbol, dep
      group        
         by grouping sets((name, symbol, dep), ())
      order by rank, max(rk) nulls lastBut i want to fetch the following columns as well.......how can i do it?-
    HR Amount, %HRTotal,$HR Cumm Total,
    Finance Amount, %FinanceTotal,$Finance Cumm Total
    Sales Amount, %SalesTotal,$Sales Cumm Total,
    Marketing Amount, %MarketingTotal,$Marketing Cumm Total
    as i want all of the records, then going to specific to the dep.....do i need to use the case here?
    Thanks for all of your time and effort in advance

    Hello Frank Kulash/Łukasz Mastaler,
    Thanks for your time and effort.
    I am using the Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
    I am looking forward to have some additional columns (column alias) along with the the returned by the mentioned query - which are named as -
    1- HR Amount
    2- %HRTotal
    3- %HR Cumm Total
    4- Finance Amount
    5- %FinanceTotal
    6- %Finance Cumm Total
    7- Sales Amount
    8- %SalesTotal
    9- %Sales Cumm Total
    10 -Marketing Amount
    11- %MarketingTotal
    12- %Marketing Cumm Total
    based on the logic like -
    HR Amount = sum of amount case dep ='HR'
    %HR Total = % of amount case dep ='HR'
    %HR Cumm Total (cumulative % based on the cumulative total of %HR Total) = Cumm % of amount case dep ='HR'
    similarly rest of the column........
    Now how do i use case with a logic so that it returns me the columns as i want them ...using the above mentioned query .......
    Kindly help me .....thanks in advance for all of your time

  • SQL query to get the Datetime 06 hours prior to the table Datetime value

    Hi Experts,
                    I'm just trying to create a SQL query to get the Datetime which should be 06 hours prior to my Table column value(Executiontime),
    Eg: my Executiontime(column) value is 07:00AM means, this query should fetch the detail of first VMName from table at 01:00AM, 
    SQL Table Name: TestTable
    Columns: VMName(varchar),status(varchar) Executiontime(Datetime)
    SQL Query : Select Top 1 VMName from
    TestTable where convert(date,Exeutiontime)=convert(date,getdate()) and
    status='0' and ExecutionTime > dateadd(hour,6,getdate())
    Request someone to alter this Query to my requirement or give me the new one.
    Regards,
    Sundar
    Sundar

    Hi All,
            Thanks for your Prompt response. I tried the below queries, but still I don't have any luck. Actually the queries are returning the value before the condition met (say when the time difference is more than 06 hours). I want the
    query to return exactly @ 06 hour difference or less than 06 hours,
    Query 01: Select Top 1 VMName from TestTable where
    convert(date,Exeutiontime)=convert(date,getdate())
    and status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 02: Select
    Top 1 VMName from TestTable where
    status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 03: Select
    Top 1 VMName from TestTable where status='0'
    and ExecutionTime >
    dateadd(hour,-6,ExecutionTime)
              Can someone point out the mistake please.
    Regards,
    Sundar
    Sundar

Maybe you are looking for