How can I write the SQL query for this requirement?

Hello

I have a table that looks like this:

NAME | ANNUAL |     VALUE
==== | ====== | =====
execno |     480.     000004
step |      480.     0400
SCNA |     480. cd_demo
System |     480.     D47-010
type |     480.     step
free_text |     480.     stage 400
rbare |     480.     RBA-1
execno |     482. 000004
SCNA |     482. cd_demo
System |     482.     D47-010
free_text |     482.     step 300
step |          482.          0300
type |      482.     step
rbare |     482.     RBA-1
execno |     483.     000001
type |     483.     step
rbare |     483.     rke1
SCNA |     483.     rke10
step |     483.     0240

Now, say that I want to fetch ONLY annual with execno = '000004' and '400' = step and scna = "cd_demo" and system = "d47-010' and type = 'step', how to write SQL code?
At first, it seemed like a simple writing query but I've been struggling with it for hours without success. I must admit though that I'm not strong in SQL :-)
There, can anyone help? Please...

Thanks in advance.

Emmanuel

Published by: user12138559 on October 30, 2009 03:05

Hi, Emmanuel.

Welcome to the forum!

Here's a way to do what you asked:

SELECT       doc_id
FROM       table_x
GROUP BY  doc_id
HAVING       SUM (CASE WHEN name = 'execno' AND value = '000004'  THEN 1 END) > 0
AND       SUM (CASE WHEN name = 'step'   AND value = '400'     THEN 1 END) > 0
AND       SUM (CASE WHEN name = 'scna'   AND value = 'cd_demo' THEN 1 END) > 0
AND       SUM (CASE WHEN name = 'system' AND value = 'd47-010' THEN 1 END) > 0
AND       SUM (CASE WHEN name = 'type'   AND value = 'step'    THEN 1 END) > 0
;

If you think that a WHERE clause would be used, but WHERE does apply to a single line. You need a condition that checks several rows in the same group.
WHEN has an effect something like WHERE.

Published by: Frank Kulash, October 30, 2009 06:26

This solution assumes that (name, annual) is unique.

Tags: Database

Similar Questions

  • What can be the SQL query for this requirement?

    Hello
    I have a table with the fields like this:

    ID DESC PARENT
    01 02 ABC
    02 01 ABC1
    03 01 ABC2
    04 02 ABC4


    In the table above column PARENT refers to the column ID, but actually in the SQL query, I want to have ID DESC and PARENTDESC (i.e., desc ID value corresponding)

    Output real I need is

    SELECT ID, DESC? from table where ID = someValue. Now, if I provided ID = 01 then output should be like this:

    ID DESC PARDESC
    ABC1 ABC 01


    Can anyone help on what may be the required sql query?

    Published by: bootstrap on April 29, 2011 06:15

    SELECT T1.ID, T1. DESC, T2. / / DESC
    FROM TABLEA T1, T2 TABLEA
    WHERE T1.ID = '01'
    AND T2.ID = T1. PARENT;

  • How can I write the SQL for this result?

    Hello my dear,
    Here first of all the script.
    CREATE TABLE ACC_TEST(
    AD_ID NUMBER,
    AD_NAME VARCHAR2(50),
    AD_SPM_ID NUMBER);
    /
    the data are
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (136,'Saleh Ahmed',129);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (142,'Hamidur Rahman',136);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (124,'Jasim Uddin',null);
    INSERT INTO ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) VALUES (129,'Sazib',124);
    I need the following result, when passing a value of Ad_Id. For example I go to 142 then result must be
    Select Ad_Id,Ad_Name
    From..
    ...
    where ad_id=142
    
    Ad_Id   Ad_Name
    136     Saleh Ahmed
    129     Sazib
    124     Jasim Uddin
    
    If I Pass Ad_Id=136 Then Result Should Be
    Ad_Id   Ad_Name
    129     Sazib
    124     Jasim Uddin
    
    
    If I Pass Ad_Id=129 Then Result Should Be
    Ad_Id   Ad_Name
    124     Jasim Uddin 
    Database 10 G XE

    Any help will be useful

    Hello

    HamidHelal wrote:
    WoW! You are totally my point. How do you understand that? l

    Guess luck. Guessing is generally not the best way to solve problems. It is generally faster and more reliable to say exactly what you want, as well as give an example.

    fallen little more that I want to know, if I want to restrict the output not more then 2, which would be sql?

    Now, you're not even giving for example!
    Maybe you want something like this:

    SELECT     ad_id
    ,     ad_name
    FROM     acc_test
    WHERE     LEVEL     BETWEEN 2 AND 3          -- Changed
    START WITH     ad_id     = :target_ad_id
    CONNECT BY     ad_id     = PRIOR ad_spm_id
    ;
    

    which will show just the mother and grandmother of the given line.

    I work with the developer of forms a lot. Knowledge of SQL is like sql oracle (9i cerfitication) book. But this type of sql is not available here.
    where can I learn this type of sql? SQL different then regular?

    Certification is another matter entirely.
    There are books and web sites dealing with more advanced techniques. Sorry, I don't know any good enough to recommend. Some authors (such as Tom Kyte) are always good.

    Here are a few sites that explain CONNECT BY queries:
    http://www.adp-GmbH.ch/ora/SQL/connect_by.html
    http://www.oradev.com/connect_by.jsp

  • How can I get the serial code for this product

    Hi, I can't find serail number to install my new photoshop elements it is not on the package, can anyone help thanks

    Serial number FAQ recovery process. Point-of-sale activation products

    Mylenium

  • How can I find the serial number for any of our subscription products?

    One of our subscription products does not appear in our online account page.  How can we find the serial number for this product?

    There is no serial number for CC products. They are simply listed under your plans and your subscriptions when it is active.

    Mylenium

  • How can we use the variables name, for example?

    I use 9 textbox in my step. The names are similar, only difference is the last character. (s1_0, s1_1, s1_2,...). I am using these textbox in a loop. But I couldn't find the right typing... s1_ [i], (i) s1_ or s1_ {i} gives an error.

    s1_0.text = 0
    s1_1.text = 0
    s1_2.text = 0
    s1_3.text = 0
    s1_4.text = 0
    s1_5.text = 0
    s1_6.text = 0
    s1_7.text = 0

    How can he do the shortest hits for this problem?

    Use the operator [] table:

    for (var i: uint = 0; i<>

    This ["s1_" + i.ToString ()] .text = 0;

    }

  • How can I write the trigger for the global temporary Table

    Hi Grus,
    How can I write the trigger for the global temporary Table.

    I created the TWG with trigger using the script below.


    CREATE A GLOBAL_TEMP GLOBAL TEMPORARY TABLE
    (
    EMP_C_NAME VARCHAR2 (20 BYTE)
    )
    ON COMMIT PRESERVE ROWS;


    CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
    BEFORE DELETE, UPDATE OR INSERT
    ON GLOBAL_TEMP
    REFERRING AGAIN AS NINE OLD AND OLD
    FOR EACH LINE
    BEGIN
    INSERT INTO VALUES EMPNAME (: OLD.) EMP_C_NAME);
    END;
    /


    trigger was created successfully, but her would not insert EMPNAME Table...

    Please guide if mistaken or not? If not wanting to give a correct syntax with example


    Thanks in advance,
    Arun M M
    BEGIN
    INSERT INTO EMPNAME VALUES (:OLD.EMP_C_NAME);
    END;
    
    you are referencing old value in insert stmt.
    
    BEGIN
    INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
    END;
    

    then run your app, it works very well...

    CREATE GLOBAL TEMPORARY TABLE GLOBAL_TEMP
    (
    EMP_C_NAME VARCHAR2(20 BYTE)
    )
    ON COMMIT PRESERVE ROWS;
    
    CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
    BEFORE DELETE OR UPDATE OR INSERT
    ON GLOBAL_TEMP
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    BEGIN
    dbms_output.put_line(:OLD.EMP_C_NAME||'yahoo');
    INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
    dbms_output.put_line(:OLD.EMP_C_NAME);
    END;
    /
    
    create table EMPNAME as select * from GLOBAL_TEMP where 1=2
    
    insert into GLOBAL_TEMP values('fgfdgd');
    commit;
    select * from GLOBAL_TEMP;
    select * from EMPNAME;
    
    output:
    1 rows inserted
    commit succeeded.
    EMP_C_NAME
    --------------------
    fgfdgd               
    
    1 rows selected
    
    EMP_C_NAME
    --------------------
    fgfdgd               
    
    1 rows selected
    

    He got Arun

    Published by: OraclePLSQL on December 28, 2010 18:07

  • How can I write the value of floats Unitronics vision230 PLC with modbus Ethernet

    How can I write the value of type Float in unitronics PLC Vision230 modbus ethernet (Ethernet Master Query.vi MB) usinsg I read and write register 32 bits, for example, I want to write the value 23.45 2nd Add. MF. And registry MF is the 32-bit registry. I read and write register 32-bit.

    Narendra.

    Narendra,

    Two characters can type cast into a uint16 you VI supports.

    To summarize. Take one (4 bytes). Flatten it to a string (4-byte) divided this string into two parts of 2 bytes each. Cast in u16 (16 x 2 = 32-bit).

    However, if you really want to follow the IEEE standards then you will need refer this KB. His is not that simple, but seems closer to what you are looking for.

    Amit

  • How can I download the right software for my new epson perfection scanner?

    How can I download the right software for my new epson perfection 1250 scanner?  I have already downloaded TWAIN driver but that does not seem to do.  He came with a disc, I got from the thrift store.  Thank you Lydia

    Monday, February 27, 2012, 21:57:09 + 0000, lydia111111 wrote:

    How can I download the right software for my new epson perfection 1250 scanner?  I have already downloaded TWAIN driver but that does not seem to do.  He came with a disc, I got from the thrift store.

    Go to the Epson website and find the Epson perfection 1250 for
    your version of Windows. I don't know if it exists or not, but if
    for this, you will find it here.

    Whenever you switch to a new version of Windows, you must install
    drivers (software that interfaces between the hardware and)
    Windows) for printers, scanners, video cards, etc., which have been
    written for this new version of Windows.  And also note that you need
    different determinants for a 32-bit version of Windows and a 64-bit
    Version.

    It is the manufacturer of the hardware to write and
    available to download these drivers. Especially if your hardware is
    older, it is quite possible that the manufacturer has decided that it is not
    a value of his time and effort to develop drivers for the hardware that it is
    selling more.
    Check with the manufacturer or on their web site to see if a pilot
    There are. If it, download and install it. If it isn't, you're out
    lucky. Also consult their websites in other countries, if there is
    None; Sometimes you can find the driver you need in this way.

    In the future, before making such an upgrade, make sure you have
    done the appropriate research to find out what drivers do not exist,
    and so what equipment you need to replace if you upgrade.

    Ken Blake, Microsoft MVP

  • How can I change the default icon for a file type

    Hey there,

    I know I did in Windows XP and I am hoping there is a way to do this in Windows 7.

    How can I change the default icon for a file type?

    I want to have all my .sql files appear with a regarless of the specific program's icon is defined as the default program to open with.

    I did some research, but he has only led hacks registry, I tried but not lucky? Does anyone know of a way?

    Here is a free software that will do it without risky registry changes.  Of course I would recommend when even take a few seconds to create a system restore point before using it, just to be sure.

    File of Type utility for Windows 7: http://types.sourceforge.net
  • How can I write a sql with a Union.

    How can I write a sql with a Union.


    Select emp_name, emp_no, emp_sal of the emp


    If show_Less_100000 = "Yes" then emp_sal < 100000 (all values less than 100000)

    otherwise the full list.



    Thank you
    Harsha

    Published by: taty on July 31, 2012 11:28
    SQL> variable show_Less_100000 varchar2(3)
    SQL> exec :show_Less_100000 := 'Yes';
    
    PL/SQL procedure successfully completed.
    
    SQL> select  ename,
      2          empno,
      3          sal
      4    from  emp
      5    where (
      6               :show_Less_100000 = 'Yes'
      7           and
      8               sal < 2000
      9          )
     10       or nvl(:show_Less_100000,'No') != 'Yes'
     11  /
    
    ENAME           EMPNO        SAL
    ---------- ---------- ----------
    SMITH            7369        800
    ALLEN            7499       1600
    WARD             7521       1250
    MARTIN           7654       1250
    TURNER           7844       1500
    ADAMS            7876       1100
    JAMES            7900        950
    MILLER           7934       1300
    
    8 rows selected.
    
    SQL> exec :show_Less_100000 := 'All';
    
    PL/SQL procedure successfully completed.
    
    SQL> select  ename,
      2          empno,
      3          sal
      4    from  emp
      5    where (
      6               :show_Less_100000 = 'Yes'
      7           and
      8               sal < 3000
      9          )
     10       or nvl(:show_Less_100000,'No') != 'Yes'
     11  /
    
    ENAME           EMPNO        SAL
    ---------- ---------- ----------
    SMITH            7369        800
    ALLEN            7499       1600
    WARD             7521       1250
    JONES            7566       2975
    MARTIN           7654       1250
    BLAKE            7698       2850
    CLARK            7782       2450
    SCOTT            7788       3000
    KING             7839       5000
    TURNER           7844       1500
    ADAMS            7876       1100
    
    ENAME           EMPNO        SAL
    ---------- ---------- ----------
    JAMES            7900        950
    FORD             7902       3000
    MILLER           7934       1300
    
    14 rows selected.
    
    SQL> 
    

    SY.

  • How can I change the default zoom for the new tab only?

    The new tab in Firefox 33 zoom is too high to see all 12 of my thumb nail. I changed it using ctrl - but the next time I opened a new tab, the zoom is 100%. How can I change the default zoom for the new tab only?

    I posted a style rule to shrink the tiles, which allows several of them on the page, but naturally reduces their legibility. You can experiment with the dimensions to find a look that works for you.

    https://userstyles.org/styles/106326/shrink-new-tab-thumbnails

    I use the Stylish extension to experiment because of its preview function that allows me to see the effect quickly. You can install it from the site of modules, then after restart of Firefox while searching for his "S" icon in the toolbar to manage Styles so you can edit and experiment.

    https://addons.Mozilla.org/firefox/addon/stylish/

  • How can I change the background color for the bar 'help file edit view history bookmark tools' in Firefox 29,0

    How can I change the background color for the bar 'help file edit view history bookmark tools' in Firefox 29,0

    You can add a theme of solid color to change the color of the top of the browser window, which contains the Menu bar.

    https://addons.Mozilla.org/en-us/Firefox/themes/solid

  • How can I access the album artwork for printing. It is used to be simple, that now I can not find anything

    How can I access the album artwork for printing on a CD? It is used to be simple, that now I can not find anything

    (Retina Powerbook 15, OSX 10.11.2, Itunes 12.3 2.35)

    Right-click on the right track... Get info... Select album art... Drag it to the desktop

    print it from there.

  • How can I find the latest driver for my Canon MX310 all-in-one printer after "redevelopment" of the new Mac OS (El Capitan)?

    How can I find the latest driver for my Canon MX310 all-in-one printer after "redevelopment" of the new Mac OS (El Capitan)?

    Have you looked at the site of Canon?

Maybe you are looking for