mySQL Query Help

Is there an easy way to find all records where he is an ID in a list? For example if I have the following:

TABLE:
resources

FIELDS:
resourceID. listOfIDs | title

RECORDS:
1. 1,7,9 | Point 1
2. 2,3,7,8 | Point 2
5: 8.17. Point 3
4. 2.7 | Point 4

I want to query the table for all records where listOfIDs contains 7. That would be 1, 2, and 3. OR I can want to find where listOfIDs contains 2 AND 8. That would be 2, 3, and 4.

Normally I use another table and have each of the elements of the list as a record that links to the resource ID, but I have 6 columns of type different 'listOfIDs', so I just wanted to know if there is a better way.

Thanks for any help.

dbldutch wrote:

> Normally I use another table and have each of the elements of the list as a
> record that links to the resource ID, but I'll have 6 different
> 'listOfIDs' type columns so I just wanted to know if there is a better way.
>

The second related table * IS * the best way. That is why all
Ph.d. thesis of database are written on the standardization of data. Trust
them, six related tables will be much easier then six columns each
containing a list of numbers.

Why, because a list of numbers is not a list of numbers in a database
It is a string. And the only way to search is inefficient string
functions. Your where clause will have to consider something like this:

' WHERE listOfIDs LIKE ' 7, % ' OR listOfIDs LIKE '%, 7% ' OR listOfIDs like '%, 7' '.

And that is just to search for a possible value in the list, that
progresses of exponential complexity with each additional number
that research.

Tags: ColdFusion

Similar Questions

  • Need help to write a MySQL query that returns only the peer matching records

    Because I don't know how to explain it easily, I use the table below as an example.

    I want to create a MySQL query that returns only the records that match counterparts where 'col1' = 'ABC '.

    Notice the ' ABC / GHI' record does not have a Counter-match ' GHI / ABC' record. This record must not be returned because there is no Counter-Party correspondent. With this table, the ' ABC / GHI' record should be the one returned in the query.

    How can I create a query that will do it?


    ID | col1 | col2
    --------------------
    1. ABC | DEF
    2. DEF | ABC
    3. ABC | IGS
    4. DEF | IGS
    5. IGS | DEF


    * Please let me know if you have no idea of what I'm trying to explain.

    I wanted to just the results where col1 = ABC, but I already got the answer I needed on another forum. Thank you anyway.

    SELECT a.col1,
    a.col2
    FROM table_name AS a
    LEFT OUTER
    Table_name JOIN b
    ON b.col1 = a.col2
    AND a.col1 = b.col2
    WHERE b.col1 IS NOT NULL AND a.col1 = 'ABC '.

  • Storage from multiple tables in one MySQL query

    So I would like, with a single MySQL query, extract data from a table (say 15 rows of data) and then extract data from another table (lets say another 15 ranks) and who joined the first, so that I can view and paginate a single list (of 30 rows now).  I don't think that a join is what I'm looking for, because I want the second list should be annexed to the first and not mixed in.  I would just use different queries, but I already have the paging function implemented for a single set of records, and implementation with two (where the second begins when the first leaves) seems daunting. However, if you have any tips on one, they would really help.

    Thank you

    You need to use a UNION to join the two select statements. The number of columns and the data types of each selection must match.

  • MySQL Query Question

    I have looked in the manual, but obviously still need help on the MySQL site.

    Example: I have a table named tbl_clients including the fields of vairious including the Client_Number field.

    I am trying to query the table using php to get all fields in a record that has the customer number. That's what I use in my php:

    $sql = "SELECT * FROM tbl_clients WHERE Client_Number = $_SESSION ["client_number"]";
    $client_info = $connect-> Query or die ($connect-> error);

    (Note: $_SESSION ['client_number'] has been defined when registering for the process based on this information.)

    The result of this is:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expected T_STRING or T_VARIABLE or T_NUM_STRING in C:\.

    I was just trying not to reinvent the wheel by storing all records from the table in a table before you use php to browse and find the correct record line (or because the information would now be in a table rather than a table).

    Thank you.

    UteFanJason wrote:
    > I have looked in the manual, but obviously still need some on the MySQL site
    > help.

    Your problem has nothing to do with MySQL, but with PHP.

    > $sql = "SELECT * FROM tbl_clients WHERE Client_Number = '.
    > $_SESSION ['client_number'] ';

    A quirk of using PHP to citations, it's that you can't include a table
    element inside a string in double quotes, as follows. There are several ways
    round her.

    You can concatenate the element of array at the end of the string like this:

    $sql = "SELECT * FROM tbl_clients WHERE Client_Number =". "."
    $_SESSION ["client_number"];

    You can omit quotes all about the name of the element as follows:

    $sql = "SELECT * FROM tbl_clients WHERE Client_Number = '.
    $_SESSION [client_number] ';

    Finally, you can place the table between braces, like this:

    $sql = "SELECT * FROM tbl_clients WHERE Client_Number = '.
    {$_SESSION ['client_number']} « ;

    --
    Adobe Community Expert David Powers
    Author, "The Essential Guide to Dreamweaver CS4",
    "PHP Solutions" & "PHP Object-Oriented Solutions.
    http://foundationphp.com/

  • Help with MYSQLi query and WHILE statement

    Hello

    Do not know what is the problem here but the same record is printed in the while loop 11 times (the amount of records in the table).

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

    <? PHP

    Query & main connection

    Connection to the base and the error

    $con_host = 'X '.

    $con_username = 'X '.

    $con_password = 'X '.

    $con_database = 'X '.

    $con = mysqli_connect ($con_host, $con_username, $con_password, $con_database);

    ? >

    <? PHP

    Query

    $sql = "SELECT * FROM hardware ORDER BY name ASC";

    $query = mysqli_query ($con, $sql);

    $row = mysqli_fetch_assoc ($query);

    $row_count = mysqli_num_rows ($query);

    Create Variables

    $name = $row ['name'];

    $size = $row ["size"];

    $quantity = $row ['quantity'];

    $protection = $row ['protection'];

    $location = $row ["location"];

    $sublocation = $row ["secondary"];

    $bc = $row ["BC"];

    $id = $row ['id'];

    ? >

    <! doctype html >

    < html >

    < link href = "stylesheets/main_stylesheet.css" rel = "stylesheet" type = "text/css" > "

    < link href =' http://fonts.googleapis.com/CSS?family=slabo+27px ' rel = "stylesheet" type = "text/css" >

    <! - Favicon - >

    < link rel = "shortcut icon" type = "image/png" href = "images/icon.png" / > "

    < style type = "text/css" >

    < / style >

    < head >

    < meta charset = "utf-8" >

    < title > list of < /title > printing equipment

    < link href = "stylesheets/print_stylesheet.css" rel = "stylesheet" type = "text/css" > "

    < script src = "sorttable.js" > < / script >

    <!-< onload = "window.print () of body" >-> ""

    < / head >

    < body >

    < div class = "non-print print_button" onClick = "window.print ()" > print < / div >

    < div class = 'print_text non-print' > select the sort the list by clicking on the categories of the table, and then click Print below < / div >

    < div class = "print_a4page" >

    < div class = "print_header" >

    < div class = "print_header_logo" > < img src = "images/logo.png" width = "306" height = "43" > < / div > "

    < div class = "print_header_text" id = "header_text" > Drama Database < / div >

    < div class = "print_header_info" > printout: < script type = "text/javascript" >

    var currentTime = new Date();

    var month = currentTime.getMonth () + 1;

    var day = currentTime.getDate ();

    year = var currentTime.getFullYear ();

    document. Write (day + "/" + month + "/" + year); < /script >

    < br >

    Total records:

    <? PHP echo $row_count? > < / div >

    < / div >

    < div class = "print_header_divider" > list of equipment < / div >

    < div class = "print_body" >

    < div >

    < name of the form = 'users' method = "post" >

    < div class = "table_print" >

    < table width = "100%" border = "0" cellpadding = "5" class = "sortable" >

    < class tr = "table_header_print" >

    < th = "15%" scope = "col" width > name < /th >

    < th = "12%" scope = "col" width > size < /th >

    < th = "9%" scope = "col" width > quantity < /th >

    < th = "12%" scope = "col" width > Protection < /th >

    < th = "17%" scope = "col" width > location < /th >

    < th = "12%" scope = "col" width > secondary < /th >

    < th = "11%" scope = "col" width > Barcode < /th >

    < th = "12%" scope = "col" width > Internal ID < /th >

    < /tr >

    <? PHP {? >}

    < class tr = "table_body" >

    < td > <? PHP echo $name? > < table >

    < td > <? PHP echo $size? > < table >

    < td > <? PHP echo $quantity? > < table >

    < td > <? PHP echo $protection? > < table >

    < td > <? PHP echo $location? > < table >

    < td > <? PHP echo $sublocation? > < table >

    < td > <? PHP echo $bc? > < table >

    < td > <? PHP echo $id? > < table >

    < /tr >

    <? PHP} while ($row = mysqli_fetch_assoc ($query));? >

    < /table >

    < / make >

    < / div >

    < / div >

    < / div >

    < / body >

    < / html >

    You assign row values to variables outside the loop if of course they never change! Make the way Ben tells you.

  • How to display the results of a MySql query in multiple columns?

    Hi, I know how to display PHP/MySq

    l of the results in a single column, but I'm really stuck to show

    Ying the results of several columns in DW CS4.

    Can someone help me with a code example? I'm new to php/mysql. Thank you

    Are you asking how to rotate the query results? Do a search for a horizontal loop. If this isn't what you're asking, please provide more details.

  • Update mysql query Recordcount

    Hello

    I'm trying to figure out how I can get the number of records that are updated from mysql. Apparently, the query_name.recordcount does not work with mysql. That's what I tried:

    < cfquery name = "UpdateData" datasource = "Source" >
    UPDATE [Table] SET
    [Field] = [value]
    < / cfquery >

    #UpdateData.recordcount # < cfoutput > < / cfoutput >

    But it does not work with MySql . I tried several other things but I can't seem to understand this. Any help would be greatly appreciated.

    Kind regards
    Nebu

    As long as you already have a query first to determine whether to perform an insert or update, then use the number of records in the first query under your account. If the recordcount of the first query is not 0, then I am assuming it is the number of records that must be updated, unless your first request is not by selecting the same records that you intend to update.

    Phil

  • Logical query help

    Oracle 11g Release 2

    Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.

    CASE 1:

    
    

    create table t

    (key primary id number,)

    supplier_id number,

    number of supplier_desc_id

    batch number,

    date of dt_recv

    )

    /

    Insert into t

    values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')

    /

    Insert into t

    values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')

    /

    Insert into t

    values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')

    /

    Insert into t

    values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')

    /

    Insert into t

    values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')

    /

    Insert into t

    values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')

    /

    Insert into t

    values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')

    /

    commit;

    Select * from t;

    ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV

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

    35405 605 3809 0 14 JUNE 2013

    58543 605 3809 0 10 DECEMBER 2013

    3809 605 136793 1 11 NOVEMBER 2014

    96510 605 3809 1 10 JUNE 2014

    94222 605 3809 1 9 MAY 2014

    108229 605 3809 3 09 - SEP - 2014

    114585 605 3809 2 28 OCTOBER 2014

    RULE: when there are 2 or more records with batch = 1, return the two most

    recent recordings with batch = 1 AND any recording (no matter the batch) that

    has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)

    96510 10 June 2014 (2nd most active record with batch = 1)

    136793 11 November 2014 (more current label with batch = 1)

    94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)

    The results should be:

    ID         SUPPLIER_ID SUPPLIER_DESC_ID         STATUS_ID         DT_RECV
    ---------- ----------- ------------------------ ----------------- -------------
    96510      605                     3809                 1          10-JUN-2014
    136793     605                     3809                 1          11-NOV-2014
    114585     605                     3809                 2          28-OCT-2014 >= 10-JUN-2014
    108229     605                     3809                 3          09-SEP-2014 >= 10-JUN-2014
    
    
    This query returns the correct results:
    
    
    WITH    got_r_num    AS
    (
        SELECT  id, supplier_id, supplier_desc_id, status_id, dt_recv
        ,       ROW_NUMBER () OVER ( PARTITION BY  supplier_id,supplier_desc_id,status_id
                                     ORDER BY      dt_recv  DESC
                                   )   AS r_num
        FROM    t
    )
    ,    got_dt_cutoff    AS
    (
        SELECT  id, supplier_id, supplier_desc_id, status_id, dt_recv
        ,       MIN ( CASE
                          WHEN  status_id  = 1
                          AND   r_num      <= 2
                          THEN  dt_recv
                      END
                    ) OVER (PARTITIN BY supplier_id,supplier_desc_id)   AS dt_cutoff
        FROM    got_r_num
    )
    SELECT    id, supplier_id, supplier_desc_id, status_id, dt_recv
    FROM      got_dt_cutoff
    WHERE     dt_recv  >= dt_cutoff
    ORDER BY  dt_recv
    ;
    
    
    NOTE: records are grouped by supplier_id/supplier_desc_id
    
    
    
    
    
    
    
    
    

    CASE 2:

    truncate table t;
    insert into table t
    values(45401,801300,4466,0,TO_DATE('21-AUG-2013','DD-MON-YYYY')
    /
    
    
    insert into table t
    values(44414,801300,4466,0,TO_DATE('08-AUG-2013','DD-MON-YYYY')
    /
    
    
    commit ;
    
    
    select * from t;
    
    
    
    
    
    
    
    
    CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
    ---------- ----------- ------------------------ ----------------- ------------
         451      801300                     4466                 0 21-AUG-2013 
         44414      801300                     4466                 0 08-AUG-2013
    
    
    
    
    
    
    
    
    
     801300                     4466                 0 08-AUG-2013 
    

    RULE: when there is no batch = 1, then return all rows

    The query above does not work for this case.

    CASE 3:

    truncate table t;
    
    
    insert into table t
    values(29887,609051,1781,0,TO_DATE('19-APR-2013','DD-MON-YYYY')
    /
    
    
    insert into table t
    values(33623,609051,1781,0,TO_DATE('24-MAY-2013','DD-MON-YYYY')
    /
    
    
    insert into table t
    values(45477,609051,1781,0,TO_DATE('22-AUG-2013','DD-MON-YYYY')
    /
    
    
    insert into table t
    values(54013,609051,1781,1,TO_DATE('22-OCT-2013','DD-MON-YYYY')
    /
    
    
    commit;
    
    
    select * from t;
    
    
    CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
    ---------- ----------- ------------------------ ----------------- -------------
         29887      609051                     1781                 0 19-APR-2013
         33623      609051                     1781                 0 24-MAY-2013
         45477      609051                     1781                 0 22-AUG-2013 
         54013      609051                     1781                 1 22-OCT-2013
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    RULE: When there is only to record with batch = 1, return all rows

    The query above does not work for this case.

    Hello

    orclrunner wrote:

    Oracle 11g Release 2

    Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.

    CASE 1:

    create table t

    (key primary id number,)

    supplier_id number,

    number of supplier_desc_id

    batch number,

    date of dt_recv

    )

    /

    Insert into t

    values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')

    /

    Insert into t

    values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')

    /

    Insert into t

    values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')

    /

    Insert into t

    values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')

    /

    Insert into t

    values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')

    /

    Insert into t

    values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')

    /

    Insert into t

    values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')

    /

    commit;

    Select * from t;

    ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV

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

    35405 605 3809 0 14 JUNE 2013

    58543 605 3809 0 10 DECEMBER 2013

    3809 605 136793 1 11 NOVEMBER 2014

    96510 605 3809 1 10 JUNE 2014

    94222 605 3809 1 9 MAY 2014

    108229 605 3809 3 09 - SEP - 2014

    114585 605 3809 2 28 OCTOBER 2014

    RULE: when there are 2 or more records with batch = 1, return the two most

    recent recordings with batch = 1 AND any recording (no matter the batch) that

    has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)

    96510 10 June 2014 (2nd most active record with batch = 1)

    136793 11 November 2014 (more current label with batch = 1)

    94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)

    The results should be:

    1. ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV
    2. ---------- ----------- ------------------------ ----------------- -------------
    3. 96510 605 3809 1 10 JUNE 2014
    4. 3809 605 136793 1 11 NOVEMBER 2014
    5. 114585 605 3809 2 28 OCTOBER 2014 > = JUNE 10, 2014
    6. 108229 605 3809 3 09 - SEP - 2014 > = JUNE 10, 2014
    7. This query returns the correct results:
    8. WITH got_r_num AS
    9. (
    10. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
    11. , ROW_NUMBER () OVER (PARTITION BY supplier_id, supplier_desc_id, batch)
    12. ORDER BY dt_recv DESC
    13. ) AS r_num
    14. T
    15. )
    16. got_dt_cutoff AS
    17. (
    18. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
    19. MIN (CASE
    20. WHEN batch = 1
    21. AND r_num<=>
    22. THEN dt_recv
    23. END
    24. ) ON (PARTITIN BY supplier_id, supplier_desc_id) AS dt_cutoff
    25. OF got_r_num
    26. )
    27. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
    28. OF got_dt_cutoff
    29. WHERE dt_recv > = dt_cutoff
    30. ORDER BY dt_recv
    31. ;
    32. NOTE: the records are grouped by supplier_id/supplier_desc_id

    CASE 2:

    1. truncate table t;
    2. insert into table t
    3. values (45401,801300,4466,0,to_date('21-Aug-2013','DD-mon-YYYY')
    4. /
    5. insert into table t
    6. values (44414,801300,4466,0,to_date('08-Aug-2013','DD-mon-YYYY')
    7. /
    8. commit;
    9. Select * from t;
    10. CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
    11. ---------- ----------- ------------------------ ----------------- ------------
    12. 451 801300 4466 0 21 AUGUST 2013
    13. 44414 801300 4466 0 AUGUST 8, 2013
    14. 801300 4466 0 AUGUST 8, 2013

    RULE: when there is no batch = 1, then return all rows

    The query above does not work for this case.

    CASE 3:

    1. truncate table t;
    2. insert into table t
    3. values (29887,609051,1781,0,to_date('19-Apr-2013','DD-mon-YYYY')
    4. /
    5. insert into table t
    6. values (33623,609051,1781,0,to_date('24-May-2013','DD-mon-YYYY')
    7. /
    8. insert into table t
    9. values (45477,609051,1781,0,to_date('22-Aug-2013','DD-mon-YYYY')
    10. /
    11. insert into table t
    12. values (54013,609051,1781,1,to_date('22-Oct-2013','DD-mon-YYYY')
    13. /
    14. commit;
    15. Select * from t;
    16. CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
    17. ---------- ----------- ------------------------ ----------------- -------------
    18. 29887 609051 1781 0 19 APRIL 2013
    19. 33623 609051 1781 0 24 MAY 2013
    20. 45477 609051 1781 0 22 AUGUST 2013
    21. 54013 609051 1781 1 22 OCTOBER 2013

    RULE: When there is only to record with batch = 1, return all rows

    The query above does not work for this case.

    Want to get answers that work, or is it possible to get responses that cause errors?

    Make sure that the INSERT statements you post too much work.  Test (and, if necessary, correct) them before posting.  All the instructions insert above have errors.

    The query above (once you correct the spelling of PARTITION) returns all the lines after a date limit.   It's always what you want, only the details of how calculated this date limit changed.  In accordance with the new requirements, the closing date must be earlier than the actual lines dt_recv in there are not 2 (or more) with batch = 1 for any combination of (supplier_id, supplier_desc_id).  All you have to do is change "r_num".<= 2"="" to="" "r_num="2" ,"="" when="" computing="" dt_cutoff,="" and="" return="" an="" impossiblly="" early="" date="" if="" there="" is="" no="" such="" row. ="" (i="" assume="" that="" dt_recv="" can="" not="" be="">

    WITH got_r_num AS

    (

    SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

    ROW_NUMBER () OVER (PARTITION BY supplier_id

    supplier_desc_id

    batch

    ORDER BY dt_recv DESC

    ) AS r_num

    T

    )

    got_dt_cutoff AS

    (

    SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

    , NVL ( MIN (CASE)

    WHEN batch = 1

    AND = 2 r_num - not <=, as="">

    THEN dt_recv

    END

    ) OVER (PARTITION BY supplier_id

    supplier_desc_id

    )

    , TO_DATE ('1', 'J') - first DATE in Oracle

    ( ) AS dt_cutoff

    OF got_r_num

    )

    SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

    OF got_dt_cutoff

    WHERE dt_recv > = dt_cutoff

    ORDER BY supplier_id

    supplier_desc_id

    dt_recv

    ;

    If dt_recv can be NULL, it is a bit more complicated, but only a little.  Post instructions INSERT (work) and outcomes if you would like to help with this scenario.

  • SQL query - help with join

    Dear friends,

    Version of DB - 11.1.0.7... , I'm stuck with SQL basics today... need your help...

    The slot SQL tells me "cache them locks library" in the database that I will put up as a proactive measure.

    I'll be it works via shell script and include the table gv instance_name $ instance ... I'm a little confused as to how a 3rd table "gv$ instance ' can be introduced into the query in order to make the instance_name in the result set...

    SELECT * FROM)

    SELECT / * + LEADING (a) USE_HASH (u) * /.

    instance_name, INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 2, 'INSERT') as "order."

    Event, mod(P1,16) p1, p2, p3

    COUNT (*) totalseconds

    , SUM (CASE WHEN wait_class = 'Application' THEN 1 ELSE 0 END) 'Application '.

    Of

    (SELECT

    a.*

    , TO_CHAR (CASE WHEN session_state = 'WAITING' THEN ELSE null END p1, '0XXXXXXXXXXXXXXX') p1hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN p2 ELSE null END, '0XXXXXXXXXXXXXXX') p2hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN ELSE null END p3, '0XXXXXXXXXXXXXXX') p3hex

    SGS $ active_session_history one) a

    u dba_users

    WHERE

    a.user_id = u.user_id

    AND sample_time BETWEEN sysdate-90 /(24*60) AND sysdate

    - AND a test of ('library cache lock', 'library cache pin")

    AND event like '% library '.

    GROUP BY

    INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 'INSERT', 2),

    event, mod (p1, 16), p2, p3

    Having count (*) > 5

    ORDER BY

    TotalSeconds DESC

    , INST_ID select, blocking_session, username, session_id, sql_id, current_obj #, 'Order', event

    )

    WHERE

    ROWNUM < = 20

    /

    replace

    instance_name

    by

    (select instance_name gv$ instance where INST_ID select = a.inst_id) instance_name

    or select... in... a, u, gv$ instance where... and gv$ instance.inst_id (+) = a.inst_id...

  • Pivot query help

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                     3

    COMP2                    ABC                     5

    COMP1                    CAD                     3

    COMP3                    CAD                     5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                     ABC                     3

    -                     CAD                     2

    -                     TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.


    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    Hello

    Subhash C-Oracle wrote:

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                    3

    COMP2                    ABC                    5

    COMP1                    CAD                    3

    COMP3                    CAD                    5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                    ABC                    3

    -                    CAD                    2

    -                    TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.

    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    This sounds like a job for LISTAGG:

    SELECT NULL AS link

    label

    LISTAGG (comp_name, ',')

    THE Group (ORDER BY ComputerName) AS value1

    OF test1

    GROUP BY label

    ;

    If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.

    Are you sure that the results you posted are what you want from data provided?

    Is of the order of the elements in a significant list?  In other words, when you say you want to get the results:

    COMP1, COMP2

    you'd be just as happy with

    ORDI1, COMP2

    ?  If the order is important, explains what this order.

  • SQL Query Help (not working not properly)

    Hello everyone,

    I use JDeveloper 12.1.2.0.0. I do a two-way communication using 3 tables, with links between them (and using schema HR).

    In my example, I have something like:

    Departments, employees, and the SalaryByJobs (I created this table where it shows a departmentd id, employee id, salary).

    Whenever I click on one OR more departments, the employees up-to-date table by putting on the table, employees who belong to the selected department and the salaryByjob to put the jobs of the employees selected on the employees table.

    So it's something like this:

    The departments selected-> employees selected-> salarybyjbobs of these employees.

    My query used (in the view) has the following code:

    SELECT Employees.COMMISSION_PCT,

    Employees.DEPARTMENT_ID,

    Employees.EMAIL,

    Employees.EMPLOYEE_ID,

    Employees.FIRST_NAME,

    Employees.HIRE_DATE,

    Employees.JOB_ID,

    Employees.LAST_NAME,

    Employees.MANAGER_ID,

    Employees.PHONE_NUMBER,

    Employees.SALARY

    Employees EMPLOYEES

    WHERE (department_id IN (select * from THE (select cast (in_list(:variavel3) as mytableType) double) a))

    Since I use the links, the employees table does not show anything at the beginning, so I added this to my query used to go: OR nvl(:variavel3,0) = 0

    But now, whenever I try to select multiple lines, it gives me the invalid numbers and I don't understand why...

    It's only one line of code and it is not in the bean.

    Can someone help me?

    PS - The bean will Department by Department, adds the departments with ',' and for each Department, gets employees who belongs to them.

    My best regards,

    Frederico Barracha.

    The expression NVL (: variavel3, 0) = 0 is not correct. The data type of the return value of the NVL function is considered to be equal to the data type of the argument of 1 (that is, the data type of the variable binding: variavel3). You said that this variable contained a list separated by commas to ID, so the data type of the variable is VARCHAR2. As long as you compare the NVL expression of a number, you get 'Invalid number' exception, because Oracle expects a numeric data type on the left side of the comparison operator.

    To avoid the exception "Invalid number", you can modify the expression by using one of the following options:

    : variavel3 IS NULL

    NVL (: variavel3, ' *') = ' *'

    NVL (: variavel3, ' 0') = '0'

    Option 1, so the simplest and clearest.

    Dimitar

  • simple query help :)

    I have table with date, customer number, salary... Now, I want to extract the total number of client whose salary is between 2000 and 3000 and deposited consistantly for the last 6 months.
    Can someone help me with this...

    user12183668 wrote:
    I used your query... its working well... but there are a few County for the date (NTC) which I am less than 6... .and I want to exclude these documents at the level of the query.

    How can I do this

    By encapsulating the query as an interior view, something in this way:

    select *
      from (
    select
     empno
    ,count(distinct trunc(hiredate,'MM')) cnt
    from emp
    where
    sal between 2000 and 3000
    and
    hiredate >= add_months (trunc(sysdate, 'MM'), -5 )
    group by empno
    ) a
    where a.cnt = 6;
    
  • Grouping Query Help

    I need help in writing a query. The data that I now contains three columns. The values in the first column have duplicate, and the 2nd column entries. The third is unique. Here is an example of the data that I have today:

    Column1 Column2 Column3
    Tier1 Group1 1
    Tier1 Group1 2
    Group level 1 2 3
    Tier1 Group2 4
    Group level 1 3 5
    Level 1 Group 3 6


    Expected result:

    Column1 Column2 Column3
    Tier1 Group1 1
    2
    2 3 Group
    4
    3 5 group
    6

    Thanks for your help

    Hello

    Your front end that can probably not for you.
    In SQL * Plus, for example:

    BREAK  ON column1    ON column2
    
    SELECT    column1, column2, column3
    FROM      table_x
    ORDER BY  column3
    ;
    

    If you havd do in SQL, here's one way:

    SELECT       CASE
             WHEN ROW_NUMBER () OVER ( PARTITION BY  column1
                              ORDER BY         column3
                            ) = 1
             THEN  column1
    
           END     AS col1
    ,       CASE
             WHEN ROW_NUMBER () OVER ( PARTITION BY  column2
                              ORDER BY         column3
                            ) = 1
             THEN  column1
    ,       END     AS col2
    ,       column3
    FROM       table_x
    ORDER BY  column3
    ;
    

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Highlight a few places where the queries above are getting incorrect results and explain, using specific examples, how you get the results of the data provided in these places.
    Always tell what version of Oracle you are using.

    Published by: Frank Kulash, March 21, 2012 16:15

  • calculate the value of the query help

    Hi all
    Following the script nicely:
    CREATE TABLE ACCOUNT_LOOCKUP
    (
      SERIAL_ID     NUMBER,
      ACCOUNT_ID    NUMBER,
      ACCOUNT_RATE  NUMBER,
      ACCOUNT_MAX   NUMBER
    )
    
    ALTER TABLE ACCOUNT_LOOCKUP ADD (
      CONSTRAINT PK_ACCOUNT_LOOCKUP
     PRIMARY KEY
     (SERIAL_ID, ACCOUNT_ID));
    
    
    
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (1, 1, 10, 200);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (2, 1, 12, 150);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (3, 1, 8, 400);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (1, 2, 7, 100);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (2, 2, 5, 200);
    COMMIT;
    
    
    SELECT * FROM ACCOUNT_LOOCKUP
    
     SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX
    ---------- ---------- ------------ -----------
             1          1           10         200
             2          1           12         150
             3          1            8         400
             1          2            7         100
             2          2            5         200
             
             
    CREATE TABLE ACCOUNT_AMOUNT(
    ACCOUNT_ID NUMBER(10),
    ACCOUNT_AMNT NUMBER(10))
    
    
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (1, 9);
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (1, 35);
    COMMIT;
     
    SELECT * FROM ACCOUNT_AMOUNT
     
    ACCOUNT_ID ACCOUNT_AMNT
    ---------- ---------------
             1               9
             1              35
           
    I want by select query calculte ACCOUNT_TOTAL every ACCOUNT_ID exists in table ACCOUNT_AMOUNT as below ,
    
    1. every ACCOUNT_ID have SERIAL_ID and ACCOUNT_RATE and ACCOUNT_MAX in table ACCOUNT_LOOCKUP,
    2. to calculte ACCOUNT_TOTAL every ACCOUNT_ID : 
       
       - order ascending SERIAL_ID exists in table ACCOUNT_LOOCKUP to every ACCOUNT_ID exists in table ACCOUNT_AMOUNT ,
       - make sum ACCOUNT_AMNT every ACCOUNT_ID in table ACCOUNT_AMOUNT,
       - then, copmare result (sum ACCOUNT_AMNT) with first record after ascending SERIAL_ID,
       - product (sum ACCOUNT_AMNT) * ACCOUNT_RATE  result as ACCOUNT_TOTAL,(in ex: sum ACCOUNT_AMNT  = 44   ) ,
       - if ACCOUNT_TOTAL > ACCOUNT_MAX then 
             * ACCOUNT_TOTAL = ACCOUNT_MAX for first record(in ex SERIAL_ID = 1 ) ,
             * Goto the second record by ascending (in ex SERIAL_ID = 2 ) ,
               make ( ACCOUNT_TOTAL / ACCOUNT_RATE ) for first record ,
               ( 200 / 10 ) result 20 >>
               
             * calculate Remainder ACCOUNT_AMNT =  the sum ACCOUNT_AMNT 44 - 20 = 24 
             
                *Goto the second record by ascending (in ex SERIAL_ID = 2 ) ,   
                   Remainder ACCOUNT_AMNT 24 * (12) ACCOUNT_RATE for second record = 288 as ACCOUNT_TOTAL 
                   -if ACCOUNT_TOTAL > ACCOUNT_MAX then 
                   * ACCOUNT_TOTAL = ACCOUNT_MAX for second record(in ex SERIAL_ID = 2 ) ,
                      * Goto the third record by ascending (in ex SERIAL_ID = 3 ) ,
                      make ( ACCOUNT_TOTAL / ACCOUNT_RATE ) for second record ,
                       ( 150 / 12 ) result 12.5
                   
                        * calculate Remainder ACCOUNT_AMNT =  the sum ACCOUNT_AMNT 24 - 12.5 = 11.5 
                        Remainder ACCOUNT_AMNT 9.5 * (12) ACCOUNT_RATE for third record = 92 result as ACCOUNT_TOTAL 
                        if result <= ACCOUNT_MAX then 
                            ACCOUNT_TOTAL = 92 
    except result
    ------------
    
    SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX   ACCOUNT_TOTAL          ** explain ** 
    ---------- ---------- ------------ -----------  -------------  *****  sum ACCOUNT_AMNT  = 44 for ACCOUNT_ID = 1from table ACCOUNT_AMOUNT ******
             1          1           10         200          200  >> (44 * 10 ) = 440 >>  200 /10 rate = 20 >> 44 - 20 = 24 Remainder ACCOUNT_AMNT
             2          1           12         150          150  >> (22 * 12 ) = 288 >>  150 /12 rate = 12.5 >> 24 - 12.5 = 11.5 Remainder ACCOUNT_AMNT
             3          1            8         400           92  >> (11.5 * 8)  = 92  And so on ....
        
    another insert 
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (2, 10);         
       
       
    SELECT * FROM ACCOUNT_AMOUNT
     
    ACCOUNT_ID ACCOUNT_AMNT
    ---------- ---------------
             1               9
             1              35
             2              10    
             
    
    except result
    ------------
      
    
    SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX   ACCOUNT_TOTAL          ** explain ** 
    ---------- ---------- ------------ -----------  -------------  *****  sum ACCOUNT_AMNT  = 10 for ACCOUNT_ID = 2 from table ACCOUNT_AMOUNT ******
             1          1           10         200          200  
             2          1           12         150          150  
             3          1            8         400           92           
             1          2            7         100           70    10 * 7 = 70 
    Help me please
    Thanks in advance

    Published by: 900510 on December 5, 2011 08:05

    900510 wrote:
    Hi all

    First I want to apologize for my English, this isn't flunet.

    Following the script nicely:

    CREATE OR REPLACE VIEW V_ACCOUNT (ACCOUNT_ID,ID,ACCOUNT_RATE,ACCOUNT_MAX,ACCOUNT_TOTAL)
    AS
    SELECT  1, 2, 10 ,200 , 0 FROM DUAL
    UNION
    SELECT  1, 5, 12 ,150 , 0 FROM DUAL
    UNION
    SELECT  1, 9, 8  ,400 , 0 FROM DUAL
    UNION
    SELECT  2 ,1, 7  ,100 , 0 FROM DUAL
    UNION
    SELECT  2 ,3 ,5  ,200 , 0 FROM DUAL
    ORDER BY 1,2
    
    ACCOUNT_ID         ID ACCOUNT_RATE ACCOUNT_MAX ACCOUNT_TOTAL
    ---------- ---------- ------------ ----------- -------------
    1          2           10         200             0
    1          5           12         150             0
    1          9            8         400             0
    2          1            7         100             0
    2          3            5         200             0
    

    I must be missing something... in your opinion, by definition does only selected literals 5 double. How you expect what you do for any other table to change those returned by the view?

    Published by: EdStevens on December 5, 2011 08:51

  • join query help

    Hello everyone, I kindly need help with a query that I'm writing. I think it's supposed to be some kind of join, but I'm a little uncertain. Here is an example:

    Select a.person_id, a.company, b.name, e.element, f.value
    of a, b, e, f
    where a.person_id = b.person_id
    and e.el_id = f.el_id
    -e.t.c

    Lets say this returns

    person_id, company, name, element value
    ------------------------------------------------------
    1 vol., krog, breakfast, 34
    2, mols, flog, munch, 24

    The problem is now the table e. I want to get all the e table values that meet certain criteria. As in:

    Select e.element
    where e.name = "RATED."

    Lets say this returns

    element
    -----------
    food
    lunch
    Munch

    And combine it with the query at the top of the page. But I also want to show all the other values, a.person_id, a.company, b.name for each line.
    So my goal is to finally have:

    person_id, company, name, element value
    ------------------------------------------------------
    1 vol., krog, breakfast, 34
    1 vol., krog, food, 0
    1 vol., krog, munch, 0
    2, mols, flog, munch, 24
    2, mols, flog, food, 0
    2, mols, flog, 0

    It's to have a default value of zero, where no join does exist for the value and do not duplicate anything even if I could always use separate.
    Can anyone help with this?
    with t1 as (
                select a.person_id, a.company, b.name, e.element, f.value
                  from a, b, e, f
                  where a.person_id = b.person_id
                  and e.el_id = f.el_id
                  -- e.t.c
               ),
         t2 as (
                select e.element
                 where e.name = 'EVALUE'
               )
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
      order by person_id,
               company,
               name,
               t2.element
    /
    

    For example:

    with t1 as (
                select 1 person_id, 'Vols' company, 'krog' name, 'lunch' element, 34 value from dual union all
                select 2, 'Mols', 'flog', 'munch', 24 from dual
               ),
         t2 as (
                select 'food' element from dual union all
                select 'lunch' from dual union all
                select 'munch' from dual
               )
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
               t2.element
      order by person_id,
               company,
               name,
               t2.element
    /
    
     PERSON_ID COMP NAME ELEME      VALUE
    ---------- ---- ---- ----- ----------
             1 Vols krog food           0
             1 Vols krog lunch         34
             1 Vols krog munch          0
             2 Mols flog food           0
             2 Mols flog lunch          0
             2 Mols flog munch         24
    
    6 rows selected.
    
    SQL> 
    

    SY.

Maybe you are looking for