How to use the Pivot function for range group in oracle SQL

Hello

Hello!!!

I need to display the data in the format below. There are 2 columns 1 is State and another is rate.

State < 100 100-199, 200-299 300-399 400-499, 500-599 600-699 700-799 800-899 900-999 > = 1000 Total
AK 1 2 0 4 1 4 4 35 35 4 1 25
AL 0 0 2 27 10 17 35 2 2 35 0 103
AR 0 0 1 0 0 2 2 13 13 2 0 6
AZ 0 1 2 14 2 14 13 3 3 13 0 57
CA     0     0     1     6     2     7     3     4     4     3     0     34

Developed the sub query but unable to use the beach on the pivot function. Please help on this.

(select (SELECT short_description
OF CODE_VALUES
WHERE CODE_TYPE_CODE = ad. STATE_TYPE_IND_CODE
AND VALUE = ad. STATE_CODE
) STATE,
Nr.rate
N-NEUTRAL
c contacts,
announcement of addresses,
xref_contacts_addresses xca,
neutral_rates nr
where n.contact_id = c.contact_id
and n.address_id = ad.address_id
and xca.address_id = ad.address_id
and xca.contact_id = c.contact_id
and nr.contact_id = n.contact_id
and nr.rate_frequency = 'HOUR')

Like this

with t
as
(
select 'AL' state, 12 value from dual union all
select 'AL' state, 67 value from dual union all
select 'AL' state, 23 value from dual union all
select 'AL' state, 12 value from dual union all
select 'AL' state, 12 value from dual union all
select 'AL' state, 78 value from dual union all
select 'AL' state, 34 value from dual union all
select 'AL' state, 4 value from dual union all
select 'AL' state, 12 value from dual union all
select 'AL' state, 15 value from dual union all
select 'AZ' state, 6 value from dual union all
select 'AZ' state, 123 value from dual union all
select 'AZ' state, 123 value from dual union all
select 'MA' state, 23 value from dual union all
select 'MA' state, 120 value from dual union all
select 'MA' state, 456 value from dual union all
select 'MA' state, 11 value from dual union all
select 'MA' state, 24 value from dual union all
select 'MA' state, 34 value from dual union all
select 'MA' state, 87 value from dual union all
select 'MA' state, 23 value from dual union all
select 'MA' state, 234 value from dual union all
select 'MA' state, 789 value from dual union all
select 'MH' state, 54321 value from dual
)
-- End of test data
select state,
       nvl(count(decode(value, 0, 0)), 0) "<100",
       nvl(count(decode(value, 1, 1)), 0) "100-199",
       nvl(count(decode(value, 2, 2)), 0) "200-299",
       nvl(count(decode(value, 3, 3)), 0) "300-399",
       nvl(count(decode(value, 4, 4)), 0) "400-499",
       nvl(count(decode(value, 5, 5)), 0) "500-599",
       nvl(count(decode(value, 6, 6)), 0) "600-699",
       nvl(count(decode(value, 7, 7)), 0) "700-799",
       nvl(count(decode(value, 8, 8)), 0) "800-899",
       nvl(count(decode(value, 9, 9)), 0) "900-999",
       nvl(count(decode(value, 10, 10)), 0) ">=1000"
  from (
          select state, case when value < 100 then 0
                             when value between 100 and 199 then 1
                             when value between 200 and 299 then 2
                             when value between 300 and 399 then 3
                             when value between 400 and 499 then 4
                             when value between 500 and 599 then 5
                             when value between 600 and 699 then 6
                             when value between 700 and 799 then 7
                             when value between 800 and 899 then 8
                             when value between 900 and 999 then 9
                             when value >= 1000 then 10
                        end value
            from t
       )
 group
    by state

Tags: Database

Similar Questions

  • I used the inverse function for a palindrome bulk pl/Sql program, but it is said to declare the inverse function and my code is below! Please suggest me some friends of ideas and thanks in advance

    declare

    i_var varchar2 (30): = '& enter_string';

    Vout varchar2 (20);

    Start

    :=Lower(Reverse(i_var)) Vout;

    If lower (i_var) = vout can

    dbms_output.put_line ('this is a palindrome');

    on the other

    dbms_output.put_line ('this is not a palindrome');

    end if;

    end;

    I agree completely

    SQL > declare

    2 i_var varchar2 (30): = "& enter_string;

    3 vout varchar2 (20);

    4 start

    5 Select lower (reverse (i_var))

    6 in vout

    7 double;

    8 lower (i_var) = vout

    9. can

    10 dbms_output.put_line (i_var |) 'is a palindrome');

    11 other

    12 dbms_output.put_line (i_var |) (' ' is not a palindrome ");

    13 end if;

    14 end;

    15.

    Enter the value of enter_string: Bolton

    2 old: i_var varchar2 (30): = "& enter_string;

    2 new: i_var varchar2 (30): = 'Bolton ';

    Bolton is not a palindrome

    PL/SQL procedure successfully completed.

  • help in the application without using the pivot function

    Hi gurus,

    Can you please help write the query without using the PIVOT function.
     WITH indata AS
             (SELECT 1 sn, '123:456,789,323:456,213,344,345:5454' x
                from dual
              UNION ALL
              SELECT 2, 'abcd:fgrfr,rfrf,rfred,tg:tg,tg:ophhh,op,vdfgbh:poijn' x from dual),
            instr AS
             (SELECT a.SN,
                     rownum RN,
                     B.column_value || CASE
                       WHEN B.column_value NOT LIKE '%:%' THEN
                        ':'
                     END column_value 
                FROM indata a,
                     TABLE(CAST(multiset
                                (SELECT trim(SUBSTR(x,
                                                    (CASE LEVEL
                                                      WHEN 1 THEN
                                                       1
                                                      ELSE
                                                       instr(x, ',', 1, LEVEL - 1) + 1
                                                    END),
                                                    (CASE instr(x, ',', 1, LEVEL)
                                                      WHEN 0 then
                                                       4000
                                                      ELSE
                                                       instr(x, ',', 1, LEVEL) - 1 -
                                                       (CASE LEVEL
                                                          WHEN 1 THEN
                                                           0
                                                          ELSE
                                                           instr(x,
                                                                 ',',
                                                                 1,
                                                                 level - 1)
                                                        END)
                                                    END)))
                                   FROM dual
                                 CONNECT BY level <=
                                            LENGTH(x) -
                                            LENGTH(REPLACE(x, ',', '')) + 1) AS
                                sys.odcivarchar2list)) b)
            SELECT  col1_val2,
                    col1_val1,
                    col2_val2,
                    col2_val1,              
                   systimestamp
              FROM (select SN,
                          ROW_NUMBER() over(partition by SN order by RN) RN,
                           SUBSTR(column_value, INSTR(column_value, ':') + 1) VAL1,
                           substr(column_value, 1, instr(column_value, ':') - 1) val2
                      FROM instr ) PIVOT(MAX(VAL1) VAL1, MAX(VAL2) VAL2 FOR SN IN(1 as col1,2 as col2));
    Thanks in advance!...

    Any help would be appreciated.

    user590978 wrote:

    Can you please help write the query without using the PIVOT function.

    with indata as (
                    select 1 sn,'123:456,789,323:456,213,344,345:5454' x from dual union all
                    select 2, 'abcd:fgrfr,rfrf,rfred,tg:tg,tg:ophhh,op,vdfgbh:poijn' x from dual
                   ),
             t1 as (
                    select  x
                      from  indata
                      where sn = 1
                   ),
             t2 as (
                    select  x
                      from  indata
                      where sn = 2
                   ),
             t3 as (
                    select  level lvl,
                            substr(
                                   x,
                                   instr(',' || x,',',1,level),
                                   instr(x || ',',',',1,level) - instr(',' || x,',',1,level)
                                  ) col1
                      from  t1
                      connect by level <= length(x) - length(translate(x,'A,','A')) + 1
                   ),
             t4 as (
                    select  level lvl,
                            substr(
                                   x,
                                   instr(',' || x,',',1,level),
                                   instr(x || ',',',',1,level) - instr(',' || x,',',1,level)
                                  ) col2
                      from  t2
                      connect by level <= length(x) - length(translate(x,'A,','A')) + 1
                   ),
             t5 as (
                    select  lvl,
                            substr(col1,1,instr(col1 || ':',':') - 1) col1_val1,
                            substr(col1,instr(col1 || ':',':') + 1) col1_val2
                      from  t3
                   ),
             t6 as (
                    select  lvl,
                            substr(col2,1,instr(col2 || ':',':') - 1) col2_val1,
                            substr(col2,instr(col2 || ':',':') + 1) col2_val2
                      from  t4
                   )
    select  col1_val1,
            col1_val2,
            col2_val1,
            col2_val2,
            systimestamp
      from      t5
            full join
                t6
            on (
                t6.lvl = t5.lvl
               )
      order by coalesce(t5.lvl,t6.lvl)
    /
    
    COL1_VAL1  COL1_VAL2  COL2_VAL1  COL2_VAL2  SYSTIMESTAMP
    ---------- ---------- ---------- ---------- -------------------------------------
    123        456        abcd       fgrfr      23-OCT-12 11.50.12.015000 AM -04:00
    789                   rfrf                  23-OCT-12 11.50.12.015000 AM -04:00
    323        456        rfred                 23-OCT-12 11.50.12.015000 AM -04:00
    213                   tg         tg         23-OCT-12 11.50.12.015000 AM -04:00
    344                   tg         ophhh      23-OCT-12 11.50.12.015000 AM -04:00
    345        5454       op                    23-OCT-12 11.50.12.015000 AM -04:00
                          vdfgbh     poijn      23-OCT-12 11.50.12.015000 AM -04:00
    
    7 rows selected.
    
    SQL> 
    

    SY.

  • Oracle: how to use the max() function in expression box

    How to use the max() function in the case where expression, please explain with an example

    Hope this helps and should be explicit

    with t as
    (select 1 col,100 col2 from dual union
    select 2 ,100 from dual union
    select 2 ,200 from dual union
    select 3,100  from dual union
    select 3,200  from dual  )
    select col, case when max(col2)=100 then 'with 100 range'
    when  max(col2)=200 then 'with 200 range' end  from t group by col
    
  • How to use the goto function?

    How to use the goto function in indesign javascript?

    my script begins by chekcing if the input files are present or not... If one of the input files is not present, I want the n function put an end to the operation in the display of the message tht file exists... I was wondering to use goto for tht... then how to use it? or is there an alternative to that?

    GoTo is generally frowned upon by the programmers.

    The only legitimate use of goto is when you need to escape from a nested loop.

    If you need to jump into a routine, use break or simply in return from the current function.

    The model currently used by the SDK looks like this:

    do
    {
         if(condition1 == false)
              break;
         if(condition2 == false)
              break;
         if(condition3 == false)
              break;
         doSomethingReallyCool();
    }while(false);
    

    'break' the jumps at the end of the do / while loop that always comes out when you reach the end of it (while (false))

    Substances

  • How to use the @accum function in the calculation script

    Dear all.

    I'm new in essbase - calculation script so don't know how to use the @accum function.

    I want the aggregation value of child to parent

    Hierarchy

    Product

    100 > Storage Dimensions marked as stored (marked as intentionally as I want to calculate the value using the calculation script)

    100. 10 (+)

    100-20 (+)

    100-30 (+)

    I'm trying calculation Script

    100 = @Accum (@children("100"), jan: Feb);

    It works when I use below

    100 = @Accum("100-10",Jan:Feb);

    Error:-number of Dimensions [1] does not match number of gen/lev [3] office [@Dim]

    Don't know how I can roll up to an aggregate value of all the members of the child to the parent.

    Error.PNGhierachy.PNG

    you do this more difficult it must be. If you want to accumulate up to 100 then you could just make your calc

    '100 '.

    but that would be only 100, to make the OU any dimension would

    AGG (Product);

    Or better yet

    AGG (Product, Market);

  • How to use the NVL function in decoding?

    Hi all

    How to use the NVL function in decoding?

    SELECT Decode (Sign (sum (nvl (7), 0)-nvl (sum (5), 0)), - 1, 0, (sum (nvl (7), 0)-nvl (sum (5), 0)) QTY)

    of the double

    Thank you

    You should not do that...

    Greatest (NVL (Sum (Quantity), 0)-NVL (SUM (quantity_received), 0), 0) AS qty_arrival

    will do the same

    HTH

  • How to use the TRUNC function with dates in the expression builder in OBIEE.

    Hello
    How to use the TRUNC function with dates in the expression builder in OBIEE.
    TRUNC (SYSDATE, 'MM') returns 1 July 2010"where sysdate is July 15, 2010 ' in SQL. I need to use the same in the expression builder in the logical layer mdb column.


    Thanks in advance

    Use it instead:
    TIMESTAMPADD (SQL_TSI_DAY, (DAYOFMONTH (CURRENT_DATE) *-1) + 1, CURRENT_DATE)

  • How to use the add_months() function in a mapping?

    Hello

    I have a variable in ODI with a date and I need to add another variable to add 'x' months...

    How can do that?

    Thank you

    If you say you have variable1 organise a date for example 01/01/2015 and variable 2 holding a certain number of months for example 7?

    If so in your mapping expression simply use the date functions available to your database technology (step/target) and replace the variable in the function

    that is for Oracle your mapping would be ADD_MONTHS (#Variable1,Variable2of #)

  • How to use the NVL function to a parameter with comma delimited values

    Gurus,

    It is confusing to me.  I am trying to use the NVL function, but the setting that I'm passing in my cursor contains multiple values with commas.  The NVL function is confused when analyzing the values.

    'Where' cursor clause...

    and nvl (sn.c_attribute1,'x@#$%') in nvl (p_desig,'x@#$%'( )

    translated parameter values...

    and nvl (sn.c_attribute1,'x@#$%') in nvl ('SPRT''GOOD' 'BAD' 'x@#$%') -NVL does not parse the value correctly

    Any idea on how to get NVL recognize 'x@#$%' , if p_desig is null?

    Thank you

    Scott

    HI, Scott.

    Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.

    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

    My best guess, based on what you posted bone is present, that your problem is discussed in the following:

    http://tkyte.blogspot.com/2006/06/varying-in-lists.html

    http://www.Oracle-base.com/articles/Misc/DynamicInLists.php

  • How to use the different start for the resolution of specific device screen

    How to use different splash Screen for the resolution of specific device in Webworks for Smartphone.
    Please suggest me the solution.

    The BB10, you can use the element of the rim: splash:

    https://developer.BlackBerry.com/HTML5/documentation/rim_splash_element.html

  • How to use the Substring function with Case statement.

    Hi all

    I have a requirement where I have to use the substring function on the ground for the report criteria.
    FOR EXAMPLE
    I branch domain name where I have all the information of the branch names, now some of the branch names are too large with an extension after the name.
    now I want substing it, but the length of characters varies for each branch.
    so is it possible where we use a box where we can define if the character of name plug exceed a value then he substing with this length.

    Try something like this:

    LENGTH WHEN CARTER (tablename. Branch_Name) > n THEN SUBSTRING (...) Of ANOTHER tablename. Branch_Name END

    where n is the number of characters that you want to start the break.

  • How to use the copy utility for app from one server to another

    Hello
    I use hyperion 9.3.1 and I want to synchronize two applications on different server, I know how to use the utility to copy of app, but on the same server, I think that we can also use this utility to copy the application from one server to another using the properties file but I dnt know how to do it because the example is SQL and I am using and database oracle
    can you guys please help.

    Published by: sharad sapra on March 31, 2011 11:33

    Hello

    1. create a property file (this example is relevant if you have a repository of ORACLE, you can find more information in http://download.oracle.com/docs/cd/E10530_01/doc/epm.931/html_hp_admin/frameset.htm?ch09s01s01.html]):

    SOURCE_APP_JDBC_CATALOG = SOURCE_APP
    SOURCE_APP_JDBC_DRIVER = Hyperion.JDBC.Oracle.OracleDriver
    SOURCE_APP_JDBC_URL = jdbc:hyperion:oracle://RepositoryDatabaseServer; SID = DatabaseSID
    SOURCE_APP_JDBC_USERNAME = schemaName
    SOURCE_APP_JDBC_PASSWORD = schemaPassword
    SOURCE_APP_JDBC_DATABASE_TYPE = ORA
    SOURCE_APP_OLAP_SERVER = olapServerName:port
    SOURCE_APP_OLAP_USERNAME = olapUserName
    SOURCE_APP_OLAP_PASSWORD = olapUserPassword
    SOURCE_APP_OLAP_APPNAME = SOURCE_APP

    TARGET_APP_JDBC_CATALOG = TARGET_APP
    TARGET_APP_JDBC_DRIVER = Hyperion.JDBC.Oracle.OracleDriver
    TARGET_APP_JDBC_URL = jdbc:hyperion:oracle://RepositoryDatabaseServer; SID = DatabaseSID
    TARGET_APP_JDBC_USERNAME = DatabaseSchemaName
    TARGET_APP_JDBC_PASSWORD = DatabaseSchemaPassword
    TARGET_APP_JDBC_DATABASE_TYPE = ORA
    TARGET_APP_OLAP_SERVER = olapServerName:port
    TARGET_APP_OLAP_USERNAME = olapUserName
    TARGET_APP_OLAP_PASSWORD = olapUserPassword
    TARGET_APP_OLAP_APPNAME = TARGET_APP

    2. open CMD and type copyapp /S:yourSourceAppName T:yourTargetAppName P:yourPropertiesFileName.properties /L:yourLogFileName.log

    Once the migration has been successful, you must update your users using the UpdateUsers.cmd (UpdateUsers.cmd and PlanningUserName PlanningUserPassword TARGET_APP).

    I hope this helps...

    Kind regards
    André

  • Do I need to use the NVL function for this request?

    I seem to have some problems using the NVL function on a date. I'm trying to create a query pulls back just a date a file was sent between the sysdate and the date_sent more a minute. Here is the code I use...

    Oracle: 10.2 G
    SELECT date_sent as date_sent
    INTO    iDateSent
    FROM    tableX
    WHERE  file_id= 9999
    AND    date_sent BETWEEN sysdate AND date_sent + 1/1440; 
    Currently Output (null):
    DATE_SENT
    Desired output when date_sent is null:
    DATE_SENT
    0:0:000 0:00:00
    Note:
    date_sent column is in a normal date - same format as sysdate format.

    The problem I encounter is when date_sent is null, the stored procedure, I built crashes because date_sent returns a null and a null value cannot be stored in the variable 'iDateSent '. I was thinking about using the NVL function here to resolve, however, I'm not entirely sure who will work with the above query. Any thoughts?

    Published by: user652714 on July 9, 2010 12:58

    Published by: user652714 on July 9, 2010 13:11

    Hello

    you said: "desired output when date_sent is null: date_sent: 0".
    It is not possible to have a date of 'zero '.

    "and a null value cannot be stored in the variable 'iDateSent'. '.

    I do not think: If this variable is declared normally (without any not null not constrained to this topic) so that it can contain a null value. (it's 99% of the time in PL/SQL).

    I think it's that your SQL retrieves no rows, to manage what you should put an exception handler:

    begin
    SELECT date_sent as date_sent
    INTO    iDateSent
    FROM    tableX
    WHERE  file_id= 9999
    AND    date_sent BETWEEN sysdate AND date_sent + 1/1440;
    exception when NO_DATA_FOUND then
     iDateSent:=null;
    end;
    

    So what you can do if you don't want the date empty a nvl:

    return(nvl(iDateSent,sysdate));  -- or another date ...
    

    Published by: user11268895 on July 9, 2010 22:09

  • How to use the node function call library for a function in the dll with the data SUB type

    Hi all

    I would ask for your kind help

    I am facing a problem with the call library node.

    I have a C++ (stdcall) function, which has Sub as data type

    XXXX error code (hwnd, lid, getValue, * Sub data1, * Sub data2)

    data1 and data2 types are constantly changing based on the value of 'getValue '.

    Mainly I can use the call library node several times and adapt each node according to the types of data data1, data2 and extract the values and use in the code. Here is no question. Real question is:

    My question:

    How can I use a node of library time call and make a case according to the 'getvalue', who will control the data1, data2 data type. Here I really seeking solutions.

    My tests:

    I used varaints as entry to the libray call node of the data1, data2 and selected parameters in the call libraby node as "Adapt to type. Here labview just crashed.

    I appreciate your suggestions to feedbackand.

    Thank you

    Karine

    You must allocate enough space for data1 and data2, and then pass a pointer to this space. An easy way to do this is the function to initialize table. Set the U8 type and size for the number of bytes required. Pass this array to the function as a pointer of table data.

    After the function call returns, you need to extract the data in the table. You can do it manually, but a simple approach is to use the array of bytes to a string. Then, in a housing structure, use Unflatten chain to convert the string to the correct data type. This method also converts the "endianness" which will be probably necessary; Be sure to only set all entries for unflatten correctly.

Maybe you are looking for

  • HP: Need to AMD graphics driver

    Having a hard time finding drivers on the hp site. I'm still looking, but if you can help I would really appreciate it. THX Hardware ID PCI\VEN_1002 & DEV_9710 & SUBSYS_3047103C & REV_00 PCI\VEN_1002 & DEV_9710 & SUBSYS_3047103C PCI\VEN_1002 & DEV_97

  • CD-ROM flashes yellow but can not read the CD on Portege

    Hello My CD blinks yellow but just does not pick up the fact that there is a disc in the drive. I had intermittent problems with loading CD and I want to boot from the CD now I'm hurt! Any ideas? I have an image of HARD drive I want to restore, and f

  • Reformat my laptop to its original factory State

    Product: Pavilion DV6-1220TX Operating system: Windows Vista - Home Premium (32 bit) Hello Above are the details of my laptop, and I would like to know the steps to format my computer laptop, as shown in the topic above. I enter the Manager recovery

  • 2 PCS N 2 NET PRINTER HELP IM ASK. SOMEONE KNOWS AN ANSWER HOW TO MAINTAIN DEVICES WITHOUT...

    Question Applies to Windows Windows 10 Network & internet PC 0 views OK girls n boys, how Damnation I keep other awake and accessible home networked PC without... Question Applies to Windows Windows 10 Network & internet PC 0 views OK girls n boys, h

  • I need to reinstall the module world app for my curve 8520 blackBerry smart phones.

    HelloI am a new player here, attracted by the search desperate to find myBlackberry app world who disappered after a verydisappointing error have removed a module of the application of this on my curve 8520. Now any time I try to download app world I