CASE... Instruction WHEN... Query does not

Hello
Can someone help me fix the following code:
select * from(
select col1,col2,
CASE 
 WHEN 
 Percentage >=75 THEN 'red'
 ELSE
 'green'
 end as color from (select
 ROUND(((COUNT(CASE WHEN class ='A' THEN 1 END)/ 
nullif((SUM(CASE WHEN class ='B' THEN 1 END)+ SUM(CASE WHEN class ='C' THEN 1 END)),0))*100),1)) as Percentage, col4,col5
from table
group by class,col2 
order by class;
P.S: Percentage corresponds to the alias for complex queries, I use... This isn't the name of the column.
Front end is APEX, once the code is syntactically correct, that I can use in the APEX. So don't report it in the forum of the APEX.

Hello

You cannot set a column alias (for example, percentage) and referencing this alais in the same SELECT clause.
What you can do is to set the aliases in a subquery (such as got_aggregates, below), then use that alias anywhere you want in a super-requete:

WITH     got_aggregates     AS
(
     SELECT       region, country, site
     ,       COUNT (CASE WHEN group_closed           = 'A' THEN 1   END)     AS closed_by_a
     ,       COUNT (CASE WHEN group_closed           = 'B' THEN 1   END)     AS closed_by_b
     ,       COUNT (*)                                                      AS total
     ,       COUNT (CASE WHEN group_first_responded = 'B' THEN 1   END)     AS first_responded_by_b
     ,       AVG   (CASE WHEN group_first_responded = 'B' THEN 100
                                                         ELSE 0   END)     AS percentage
     ,       AVG   (CASE WHEN group_closed           = 'A' THEN 100
                                                         ELSE 0   END)     AS closed_by_a_total
     FROM       statistics
     WHERE       group_closed     IN ('A', 'B')
     GROUP BY  region, country, site
)
SELECT       a.*
,       100 * closed_by_b
           / NULLIF ( first_responded_by_b
                   , 0
                 )     AS closed_by_b_1st_resp_by_b
,       CASE
           WHEN  percentage  >= 50  THEN  'RED'
                                     ELSE  'GREEN'
       END                  AS color
FROM       got_aggregates  a
ORDER BY  region, country, site
;

You will notice that I used alaises defined in got_aggregates not only for the color, but also for the closed_by_b_1st_resp_by_b.
Output of your sample data:

`                                                                  CLOSED
                                                                    _BY_B
                                           FIRST_          CLOSED   _1ST_
                    CLOSED CLOSED       RESPONDED  PERCENT  _BY_A RESPOND
REGION COUNTRY SITE  _BY_A  _BY_B TOTAL     _BY_B      AGE _TOTAL   _BY_B COLOR
------ ------- ---- ------ ------ ----- --------- -------- ------ ------- -----
Asia   China   Park      2      1     3         2     66.7     67      50 RED
Asia   India   Shop      1      2     3         1     33.3     33     200 GREEN
Europe France  Park      2      1     3         2     66.7     67      50 RED
Europe France  Shop      1      2     3         1     33.3     33     200 GREEN
Europe Switzer Park      2      2     4         1     25.0     50     200 GREEN
       land

I changed the GREEN/RED 75 to 50 threshold, just so that we would see different colors when using these sample data.

Formatting numbers are better done by your front end. I used SQL * more commands such as

COLUMN     percentage               FORMAT     99999.9          HEADING     PERCENT|AGE

to get the above result. You can use ROUND or TO_CHAR in the query itself if you prefer to do it like that.

Tags: Database

Similar Questions

  • Payroll Run query does not appear on the payroll LOV

    Hi all

    Am on 12.1.3
    DB - 11 GR 1 material

    Wen, trying to run on PAYROLL:
    FACE below error:
    Payroll Run query does not appear on the payroll LOV


    Thank you

    Please post the exact name of the simultaneous program you want to submit as well as all the parameters.

    I understand that you get an error when you try to select the LOV to pass parameters, right?

    If the program has not yet been presented, then it is an expected behavior not to find any file of log/output.

    Please see if these MOS Docs Help.

    Simultaneous request for payroll, payroll name Lov for payroll run Erros: APP-FND-1436: the list of values can not find all the values for you to choose [333524.1 ID]
    Payroll run parameter list of values (LOV) in the process "Retry Payroll Run" does not display any values [1368390.1 ID]

    Thank you
    Hussein

  • My partners hotmail account is blocked we have followed all the instructions, but still does not allow access.

    Hotmail blocked

    My partners hotmail account is blocked we have followed all the instructions, but still does not allow access. The alternative e-mail account is no ideas

    Hello

    The question you have posted is related to Windows Live and will be well suited in the Windows Live Forum. Click on the link below.

    Windows Live Solution Center

    http://www.windowslivehelp.com/product.aspx?ProductID=1

  • Force the print to stop/restart spooler service when it does not

    Our office has a HP plotter, where the driver (note that we have the latest version, directly from the HP site) seems to cause the print spooler service becomes out of order sometimes.  In turn of the programs (including AutoCAD and Outlook) to lock, as well as windows, as the dialog box, which causes devices and printers.

    If I restart the 'Services' print spooler service in computer management, it goes back to work at least some time - if at least I can work around the problem.  It is important to note however, that even by using this method, it throws a dialog box error saying that the service failed.  But since this is a question with all facilities of this liner installed with Vista, I need to have a method easier to use of the service is restarted.

    I wrote a file .bat base with a net stop spooler command followed by a net start spooler command, but the problem is that, unlike with computer management, net stop does not seem to force the service to shut down if it does not, and I can't find a way to force the stop command.

    Is there a tool more powerful/switch available that I could script so that users can easily stop and restart the spooler service when it does not?

    Thank you

    Type sc /?

    --
    ..
    --
    "mgarrison" wrote in message news: d3d768a8-126-b-4246-a4d8-ca6d8f4db6a7...
    > Our Office has a HP plotter, which the driver (note that we later
    (> version, directly from the HP site) seems to cause the print spooler
    > service becomes out of order sometimes. Which causes in turn of programs
    > (including AutoCAD and Outlook) to put him away, windows as the
    dialogue of the RHS.
    >
    > If I restart the spooler service for printing of 'Services' in computer science
    > Management, she goes back to work for at least some time - if at least I have
    > can work around the problem. It is important to note however, that even
    > by using this method, it throws an error dialog box saying that the service
    > did not. But since this is a problem with our Vista
    > facilities having this installed plotter, I need to have a more
    > user-friendly method of restarting the service.
    >
    > I wrote a .bat file to base with a net stop command spooler followed by a
    > net start spooler command, but the problem is that, unlike with a computer
    > Management, net stop does not seem to force the service stops if it is not
    > answer and I cannot find a way to force the stop command.
    >
    > Is there a tool more powerful/switch available that I could then script
    > users can easily stop and restart the spooler service when it is not
    > sensitive?
    >
    > Thank you

  • How do I uninstall ALWIL SOFTWARE AVAST 4.0 on the computer when it does not appear in the Panel

    I use windows 7. some time before I installed ALWIL SOFTWARE AVAST 4.0 antivirus on my computer... BT after a few days I uninstalled it from Control Panel as he was infected... now, I want to install KASPERSKY antivirus on my computer bt to install the wizard configuration askin remove me avast... No Comment could remove avast when it does not show in the conrol Panel... I even searched my local disk c and any computer... BT, it does not appear avast software... wt should I do nxt... plz hlp!

    1. remove all traces of Avast via this link.

    http://files.avast.com/files/eng/aswclear5.exe

    2. I recommend you then install Microsoft Security Essentials instead of Kaspersky, via the link below.

    http://www.Microsoft.com/security_essentials/

    3.I suggest also you scan your computer for malware using Malwarebytes. {free}
    After downloading update immediately.
    Do a full scan of the system

    http://www.Malwarebytes.org/products

  • I was not able to open my my Adobe Illustrator application after update. He says there are a few problems with the preference but after following the instructions, Illustrator still does not work and it keeps giving the samei question. Help, please.

    I was not able to open my my Adobe Illustrator application after update. He says there are a few problems with the preference but after following the instructions, Illustrator still does not work and it keeps giving the samei question. Help, please.

    I think you are talking after upgrade to CC 2015 you're not able to launch I. Also, I believe you speak for read only matter of preference.

    After following the instructions please do not launch Illustrator and check your file preference if the permissions have been set correctly. A good way would be to get inside the preferences folder and then try to create a file manually. If it succeeds, please write to [email protected]

    A few external links to help you set permissions:

    MAC

    OS X Yosemite: set permissions for items on your Mac

    WIN

    Set, view, change, or remove permissions on files and folders

    r-and-file-permissions/465f2b42-63dd-4486-8dd1-c870290efeed http://Answers.Microsoft.com/en-US/Windows/Forum/windows_vista-Files/How-do-i-change-folde

    Kind regards

    Ashutosh

  • How can you save your illustration in Photoshop CC 2014 as PNG or JPEG file when it does not give the option?

    How can you save your illustration in Photoshop CC 2014 as PNG or JPEG file when it does not give the option?

    Thank you for your time and help in advance.

    Thanks for the reply. I have actually managed to do work. I had my work instead of 16 or 8-32 bit/channel value. Once I changed it to 16-bit/channel, he gave me the possibility to register under the registered name. Thanks again.

  • When Lightroom does not display color space?

    When Lightroom does not display color space is assigned to an image? It should be in the metadata Panel. In Bridge, it displays color Mode: RGB/B & W and color space: sRGB/Gray Gamma 2.2. Or Lightroom all converted Adobe RGB or ProPhoto RGB?

    The ExifMeta plugin (free) will show the color profile in the metadata Panel.

  • query does not run strange

    For some strange reason, the following query 1 runs but the 2nd query does not run...
    observe the slight difference for local conditions and quotes from final for her... in the first query the final citation is in the second line.
    I bring as query 2 but she sends me results I also tried ltrim (rtrim (local) = "en_US", it does not help
    What is the problem here?


    Query 1
    --------
    SELECT *.
    Of ats_rules_bkp
    Subclass WHERE = "payments".
    CATEGORY = "qsg.
    AND clazz = 'help '.
    AND right = "ach".
    AND country = 'India '.
    AND region = "South".
    AND market_segment = "primary".
    AND local = "en_US
    '
    AND status = 'Active '.

    ---------------------------------------------
    Query 2

    SELECT *.
    Of ats_rules_bkp
    Subclass WHERE = "payments".
    CATEGORY = "qsg.
    AND clazz = 'help '.
    AND right = "ach".
    AND country = 'India '.
    AND region = "South".
    AND market_segment = "primary".
    AND local = "en_US."
    AND status = 'Active '.

    As I expected, you have a carriage return in your data. the LOCAL column is not store the string "fr_FR." It has an extra carriage return (character ASCII 10) data. You can modify the query to add the character CHR (10)

    local = 'en_us' || CHR(10)
    

    but it is probably more sensible to fix the underlying data, if this is in fact to store the string "fr_FR."

    Justin

  • Correlation query does not work

    create the table cust_age_sal (cust_id integer, integer whole age of sal);

    create the table new_cust_age_sal (cust_id integer, integer whole age of sal);

    insert into cust_age_sal values (1,10,100);

    insert into cust_age_sal values (2,20,200);

    insert into cust_age_sal values (3,30,300);

    insert into cust_age_sal values (4,40,400);

    commit;

    insert into new_cust_age_sal values (2,21,201);

    insert into new_cust_age_sal values (3,31,301);

    commit;


    Question

    I want to update cust_age_sal using the customer_id which are present in the new_cust_age_sal;

    I use queries, query1 and query2. Query1 is working and I get results and I want to know what is the problem with query2 and why it does not work

    Query1:

    UPDATE cust_age_sal u

    SET (age, sal) =

    (

    SELECT age, sal

    OF new_cust_age_sal

    WHERE cust_id = u.cust_id

    )

    where u.cust_id in (select cust_id from new_cust_age_sal)

    ---

    query2:

    UPDATE cust_age_sal

    SET (age, sal) =

    (

    SELECT age, sal

    OF new_cust_age_sal

    WHERE cust_id = u.cust_id

    )

    Hello

    2742751 wrote:

    It is except o/p I'm looking

    O/p: except

    I think you want to say "whereas", not "excluded".

    CUST_ID AGE SAL

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

    1                      10                     100

    2                      21                     201

    3                      31                     301

    4                      40                     400

    But when I run under query that is show below output even if I use the join on the target table condition. Why is it show empty for CUST_ID = 1 and 4

    UPDATE cust_age_sal u

    SET (age, sal) =

    (

    SELECT age, sal

    OF new_cust_age_sal

    WHERE cust_id = u.cust_id

    )

    CUST_ID AGE SAL

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

    1

    2                      21                     201

    3                      31                     301

    4

    A UPDATE instructions without WHERE clause will update with each row in the table.

    In the case of cust_id = 1, what will that do?  Well, what makes the following query?

    SELECT age, sal

    OF new_cust_age_sal

    WHERE cust_id = 1;

    ?  It produces no line.  When you use a subquery in a SET of an UPDATE statement clause (as you do) and the subquery returns no rows, then updated the columns with a NULL value.

    If you don't want to update all rows, but want lines updates which fulfil certain conditions (for example, have a corresponding row in new_cust_age_sal), and then use a WHERE clause in the UPDATE statement, as you did in your first UPDATE statement.

    Instead of an UPDATE statement, you can also use a MERGE statement, like this:

    MERGE INTO dst cust_age_sal

    With the HELP of new_cust_age_sal CBC

    WE (dst.cust_id = src.cust_id)

    WHEN MATCHED THEN UPDATE

    SET dst.age = src.age

    dst.sal = src.sal,

    ;

    In this way, you don't need to repeat what is essentially the same subquery in the GAME and WHERE clauses.

  • Query does not get scanned with Explain plan index

    Hello
    I have a table with Index XXQP_SAMPLE_N1 in PROCESS_FLAG column. When I run the query below and see the plan to explain it, this table is to get access full table (with red color in Toad). It's not scan with the index. I also analyzed the table with command below. can anyone tell why he does not get scanned with index?

    exec DBMS_STATS.gather_table_stats ('XXQP', 'XXQP_SAMPLE', estimate_percent = > 30, cascade = > true);

    SELECT
    *
    Of
    XXQP_SAMPLE
    WHERE
    process_flag = 1;

    Thank you

    the number of records in the table to satisfy the condition process_flag = 1

    I wrote a little note on this topic. Reade case 1. It may be of your interest.

    http://karthickarp.blogspot.com/2008/12/it-is-often-asked-why-SQL-is-going-for.html

    Published by: Karthick_Arp on April 15, 2009 01:41

  • Install on windows 8, when IE does not work

    Computer is connect to the internet, but IE does not.

    Type of topic: preferences #advanced< enter > in the address bar.

    Under Advanced, select network.
    Search for configure how Firefox connects and then press the settings button.
    Check the settings.

    Some problems occurs when your Internet security program has been implemented.
    trust the previous version of Firefox, but not more acknowledges your
    update as being approved. Now how do I solve the problem: to allow
    Firefox to connect to the Internet

    • Make sure your Internet security software is up-to-date (for example, you are using the latest version).
    • Delete Firefox to list your program of programs approved or recognized. For detailed instructions, see

    Configure the firewall so that Firefox can access the Internet. {web link}

  • Why my case of the switch does not work?

    Hey gang,

    I have a switch statement, but it doesn't seem to work.  When the selectedIndex to a drop-down list property changes, the fire of console.log statements, but the currentFlashMode never is updated.

    currentFlashMode is 'property variant currentFlashMode' on the element root of navigationPane on another page.  This switch/case is on a secondary page that is pushed.

    If I use if/else instead, everything works fine.

    for example, it does not:

                       switch (selectedIndex) {                   console.log("entering switch/case for selectedIndex..");
                            case 0:
                                //
                                console.log(" -->App: FlashModeSelector, On");
                                currentFlashMode = 0;
                                break;
                            case 1:
                                //
                                console.log(" -->App: FlashModeSelector, Off");
                                currentFlashMode = 1;
                                break;
                            case 2:
                                //
                                console.log(" -->App: FlashModeSelector, Auto (default)");
                                currentFlashMode = 2;
                                break;
                            case 3:
                                //
                                console.log(" -->App: FlashModeSelector, Light");
                                currentFlashMode = 3;
                                break;
                        }
    

    but this doesn't:

    if (selectedIndex == 0) {
                            currentFlashMode = 0;
                            console.log(" ------------>App: in onSelectedIndexChanged, currentFlashMode is : " + currentFlashMode);
                        }
                        if (selectedIndex == 1) {
                            currentFlashMode = 1;
                        }
                        if (selectedIndex == 2) {
                            currentFlashMode = 2;
                        }
                        if (selectedIndex == 3) {
                            currentFlashMode = 3;
                        }
    

    What I'm doing wrong here?

    Hello

    This is a known bug in Qt 4 (fixed only in Qt 5):

    http://supportforums.BlackBerry.com/T5/native-development/strange-switch-behaviour-in-QML/m-p/228142...

    Only the first line of each statement 'case' is executed. QT has incorrectly transformed the second statement back.

    Workaround: wrap each case in {} or use instead if/else.

    More info about Qt bug tracker:

    https://bugreports.Qt-project.org/browse/QTBUG-17012

  • Query does not work on my iPad (was: media query question)

    I am a site for mobile/tablet/Office of coding and while my office and the iphone respond to the CSS, my ipad does not work. The changes that I do take the tablet in dreamweaver, but when they go live the ipad does not. Of course, my media query must be incorrect, but I can't understand the question. Any help is very appreciated.

    http://gbetza.mydomain.com/WebService2/test/KathrynFee/2014/profile.html

    Thank you.

    Yes.  It is inevitable because smart phones are getting bigger while tablets are smaller & more.  He's always going to be some crossover.   Think of it this way, when a Tablet is behaving as a mobile device, he takes over the mobile network.  When the shelf behaves more like a desktop computer, you need the layout of office.

    Similarly, a great smartphone in landscape mode is likely to resemble the layout of your tablet.

    See the FluidGrid example below:

    http://ALT-Web.com/FluidGrid/Fluid2.html

    Office = 4-col layout

    Tablet = 2-col layout

    Mobile = 1-col layout

    For purposes of illustration, I added a thick border of gold at the disposal of the Tablet only.

    / * Tablet Layout: 481px to 768px. Inherits the styles of: Mobile layout. */

    @media only screen and (min-width: 481px) and (max-width: 768px) {}

    .gridContainer {width: 92%; border: 16px solid gold}

    }

    Nancy O.

  • WS 2.0 query does not work

    Hi all

    I have a small problem...

    I'm trying to create a query with ws 2.0 but I Don t get a response but the 400 error message.
    The connection works very well and I also get my session ID, but when I try to do a query it always comes with the
    Server returned HTTP response code: 400 for URL: https://secure-ausomxgva.crmondemand.com/Services/Integration/CustomObject5;jsessionid=4e06330622aaf934c2068a7b02766c1c0a5eda9a04782b201b63fba91893bb80.e3iRbxiPbxj0bNaKaxaSa3n0;

    I Don t know why it does not work... my SOAP Message I send to the system looks like this:

    <? XML version = "1.0" encoding = "utf-8"? >
    < xmlns:soap: envelope soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance".
    xmlns:WSSE = "http://schemas.xmlsoap.org/ws/2002/xx/secext" container = "http://www.w3.org/2001/XMLSchema".
    SOAP: encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" >
    < soap: Body >

    < CustomObject5QueryPage_Input xmlns = "urn: crmondemand/ws/ECB/customobject5/10/2004" >
    < ListOfCustomObject5 xmlns = "" urn: / crmondemand/CustomObject5/query/xml "pagesize ="100"recordcountneeded ="true"startrownum ="0">"
    < CustomObject5 searchspec = "[CustomObject5Id] AS ' *'" >
    < CustomObject5Id / >
    < / CustomObject5 >
    < / ListOfCustomObject5 >
    < / CustomObject5QueryPage_Input >

    < / soap: Body >
    < / envelope soap: >

    I programmed in Java. The Soap Action is "document / urn: crmondemand/ws/customobject5/10 / 2004:CustomObject5QueryPage.
    I hope someone can help me or identifies an error...

    If you want more information, just ask. I'm looking for this error several hours now without success...

    Thank you very much in advance!

    You could try changing your SOAPAction header to
    "document / urn: crmondemand/ws/ECB/customobject5/10 / 2004:CustomObject5QueryPage.

    Also, you should be able to recover the additional error information in your application. When I tried a request with the HTTP SOAPAction header that you use, I got the following error message:

    The remote server returned an error: bad request (400).

    The HTTP request did not contain a valid SOAP action header. The value of the header has been "document / urn: crmondemand/ws/customobject5/10 / 2004:CustomObject5QueryPage.

  • Query does not allow for good compilation of trigger

    I move some tables to test on my PC (ms access) to a server (oracle). Instead of typing these in hand in SQL * PLUS, I used CFQUERY to create my fields of data and tables. As Oracle requires a sequence and a trigger to reproduce the AutoNumber function, I also used CFQUERY to create triggers and sequences. The table and the sequence work very well. The trigger is my problem. When I use the code below and then try to insert a record into the table, I get an error: "MIKETEST_TRG is not valid and does not re-validation". If I go back and retype the creation of trigger through SQL * PLUS, everything works fine. I guess it has something to do with the compilation of the trigger and the slash at the end of my query below.

    Looking for suggestions how to fix or other means to recreate my access on my server oracle tables. Thanks in advance.

    < cfquery name = "crΘer" datasource = "datadrill_custom" >
    create or replace trigger miketest_trg
    Before Insert on miketest
    for each line
    Start
    Select miketest_seq.nextval in: double new.project_id;
    end;
    /
    < / cfquery >

    I've seen this before when you try to create triggers or still run anonymous PL/SQL blocks. The bad news is that it inserts the characters corrupt after each line. The good news is that you can get this by placing your set create statement "inline", i.e. all on one line, without line breaks. I just tested this with MX 7 and Oracle 9i and it worked. (Oh and leave the rear / as for SQL more.)

    Phil

Maybe you are looking for