Case Based Condition of inner join in SQL query

Hi Experts,
CREATE TABLE PRODUCT_PRICE(PRODUCT_ID INTEGER,PRICE FLOAT);
INSERT INTO PRODUCT_PRICE VALUES(1,1);
INSERT INTO PRODUCT_PRICE VALUES(2,2);
INSERT INTO PRODUCT_PRICE VALUES(3,1);
INSERT INTO PRODUCT_PRICE VALUES(4,1);
INSERT INTO PRODUCT_PRICE VALUES(5,3);
INSERT INTO PRODUCT_PRICE VALUES(0,4);
CREATE TABLE PRODUCT_TABLE (PRODUCTID INTEGER,OTHERID INTEGER);
INSERT INTO PRODUCT_TABLE VALUES (1,0);
INSERT INTO PRODUCT_TABLE VALUES (2,0);
INSERT INTO PRODUCT_TABLE VALUES (3,1);
INSERT INTO PRODUCT_TABLE VALUES (4,1);
INSERT INTO PRODUCT_TABLE VALUES (5,2);
I need to match the product_id = 0 to get the price of the product when there is othersid in the product table (otherid > 0). For this I created the query below.
SELECT 
    PRODUCT_ID,
    PRICE 
FROM 
    PRODUCT_TABLE
    INNER JOIN PRODUCT_PRICE ON PRODUCT_ID=PRODUCTID
WHERE
    OTHERID=0
UNION ALL
SELECT 
    PRODUCT_ID,
    PRICE 
FROM 
    PRODUCT_TABLE
    INNER JOIN PRODUCT_PRICE ON PRODUCT_ID=0
WHERE
    OTHERID>0;
My Question is, is that any way the SQLQuery above can be simplified in CASES and INTERNAL CONDITION JOIN in a single SQL QUERY?

I can work around something like that,
SELECT 
    PRODUCT_ID,
    PRICE 
FROM 
    PRODUCT_TABLE
    INNER JOIN PRODUCT_PRICE ON * CASE WHEN OTHERID>0 THEN PRODUCT_ID=0 ELSE PRODUCT_ID=PRODUCTID END*
Thank you
SELECT CASE WHEN PT.OTHERID = 0 THEN PT.PRODUCTID ELSE 0 END AS PRODUCT_ID, PP.PRICE
  FROM PRODUCT_TABLE PT,  PRODUCT_PRICE PP
    WHERE CASE WHEN PT.OTHERID =0 THEN PT.PRODUCTID ELSE 0 END = PP.PRODUCT_ID

Should it?

Tags: Database

Similar Questions

  • difference between Inner Join join natural r

    Experts,

    I'm trying to understand the difference between Natural Join and inner join.

    Inner join: the following query provides 106 lines based on two tables

    SQL > SELECT EMPLOYEE_ID EID, last NAME, DEPT_NAME DEPARTMENT_NAME

    2 EMPLOYEES A, B MINISTRIES

    3. WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID

    4 order employee_id;

    Natural Join: This is the combination or combined result of all the columns in both tables.

    When I run the Sub statement, it combines employees and the table from department of a HR schema and provides only 32 ranks

    SQL > select employe_id, first_name, department_name

    2 departments of NATURAL JOIN employees;


    EMPLOYEE_ID NAME DEPARTMENT_NAME

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

    Neena 101 Executive

    Lex 102 Executive

    104 Bruce IT

    David 105 HE

    question: why the NATURAL JOIN query omit the employee_id 103 and on what basis it omit a few other employee_id

    Thanks in advance

    Hello

    The main difference is that INNER JOIN is used in real life; NATURAL JOIN is used only in textbooks.

    More rigorously, FRANCKLIN JOIN is an inner join, involving all the columns that have the same name.  The hr.departments and hr.employees have 2 columns with the same name: department_id and manager_id, so

    OF hr.departments d

    NATURAL JOIN e hr.employees

    is equivalent to

    OF hr.departments d

    INNER JOIN hr.employees e ON e.department_id = d.deparment_id

    AND e.manager_id = d.manager_id

    or, using the old join syntax

    OF hr.departments d

    e hr.employees

    WHERE e.department_id = d.deparment_id

    AND e.manager_id = d.manager_id

  • How to do a validation based on the SQL query?

    Hello

    I have a requirement to perform a validation on a field (messageTextinput) in my page OAF.

    When I click the button apply, the value in this field is validated based on a SQL query (for example, the value in the field NOT IN (value1 select from table1).)

    Help, please.

    Best regards

    Joe

    1. create a SQL query based VO, XXVO. For example:-SQL query is select xx_table;

    2. Enter the "Apply" button click event in the controller and run the AM method passing the value entered by the user in the given field, for example:-value is VAL1

    3. in the method of the AM, get a handle of the VO, the whereClause and run.

    OAViewObjectImpl vo = findViewObject ("XXVO1"); XXVO1 is the name of the instance of the VO above, XXVO

    vo.executeEmptyRowSet ();

    vo.setWhereClause (null);

    vo.setWhereClause ("value =" + VAL1 + "'");

    vo.executeQuery ();

    If (VO. GetRowCount() > 0)

    A record is with the value of VAL1. Perform the required action

    I hope this helps.

  • Help of query SQL - inner joins and the separate results

    Hello

    ASP VB, SQL Server

    I have a structure of data base with 3 tables - users, albums and photos. each user has a identifier unique, each record has a unique albumid and also contains a column with the user name. each record in the photo has a unique id so that store the user name and the album in which the image belongs.

    I'm writing a query that returns a list of the albums for a particular user (based on a user name query string) and who will also bring back the id of the first record in the table for each of these albums photo.

    the closest I get is to run a query to select albumid albums where userid = varuserid with a join internal on the pictures table to remove the photo ID - problem I then it comes out all the photos from the photos table where userid = varuserid, so when I do a repeat region to display a list of albums for a certain user It produces a list of all the photos where userid = varuserid

    I really want to return just a list of ID album based on the username variable, but also to return the first record in the table of photos for each of these albumids

    I tried different combinations of inner joins, select distinct etc but no joy.

    any suggestion would be appreciated as am floundering here...




    First, you must define 'first' with regard to the photos. Is there a
    timestamp? They are numbered inside the album? Do you really care who is
    "first", or do you want simply a shot? You also neglected to indicate if they are
    empty photo albums have been allowed. I assumed that the empty albums are not
    allowed.

    Whatever you decide, the answer will be similar.
    SQL Server tends to get better results with joins with subqueries. You will have
    See such a written request more often with subqueries, and there isn't
    nothing wrong with that, but I'll use a join on a derived table. I have
    have not all column names (hint, hint), so I made them, but the
    Comments should help out you.

    SELECT A.Title, P.PhotoID, P.Caption, A.AlbumID, P.ImagePath
    FROM dbo. A albums
    -build a table derived, consisting of photo ID lowest for each
    album.
    INNER JOIN (SELECT AlbumID, MIN (PhotoID) AS FirstPhoto FROM dbo. Photos
    AlbumID GROUP) AS PM WE A.AlbumID = PM. AlbumID
    -details of the photo for the photo shown in the table above
    INNER JOIN dbo. Photos P on A.AlbumID = P.AlbumID AND
    H. FirstPhoto = P.PhotoID
    User A.UserID ='some WHERE '

    "tedstar" wrote in message
    News:ee4pfn$de$1@forums. Macromedia.com...
    > I am writing a query that returns a list of the albums for a
    > particular user (based on a user name query string) and also bring
    > return
    > the id of the first record in the table for each of these albums photo.

  • Inner join and 'PIVOT' results

    Good day to all;

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    "CORE 10.2.0.4.0 Production."
    AMT for Solaris: release 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production


    I have 2 paintings I want to do a query on and then "pivot" results. I understand that, since it is 10 g using the PIVOT operator is not supported.
    (Corrected code :)) issues
    create table CT
    (
    DESIGN_ID NUMBER,
    CT_ID VARCHAR2(15)  
    )
    INSERT INTO CT VALUES ('654321','10QWER123456');
    INSERT INTO CT VALUES ('987654','7ASDF654987');
    INSERT INTO CT VALUES ('321654','82CHEV852963');
     
    create table CXRF
    (
    DESIGN_ID NUMBER,    
    ROW_SEQ_NBR NUMBER,    
    XREF_CT_ID VARCHAR2(15)  
    )
    INSERT INTO CXRF VALUES ('654321','1','25ABCD');
    INSERT INTO CXRF VALUES ('654321','2','262ABCD');
    INSERT INTO CXRF VALUES ('987654','1','14WXYZ');
    INSERT INTO CXRF VALUES ('987654','2','34FRED');
    INSERT INTO CXRF VALUES ('321654','1','1TOM');
    -Oops, measure twice cut once...
    select design_id,
            ct_id,
            xref_ct_id
      from cxrf
         INNER JOIN
           ct
       on(ct.design_id = cxrf.design_id) 
    Expected results would look like this:
    DESIGN_ID             CT_ID            XREF_CT_1     XREF_CT_2
       654321       10QWER123456        25ABCD         262ABCD
    Published by: GMoney on 9 may 2013 08:47

    Published by: GMoney on 9 may 2013 08:49

    Thanks for posting examples of data and version, however, as said, it would be better if you can test, DDLS is full of typos.

    Did you visit the FAQ before posting?

    {message: id = 9360005}

    There are also effective methods in previous versions of 11 g data pivot.
    For example:

    SQL> select ct.design_id
      2       , ct.ct_id
      3       , min(case when cxrf.row_seq_nbr = 1 then cxrf.xref_ct_id end) as XREF_CT_1
      4       , min(case when cxrf.row_seq_nbr = 2 then cxrf.xref_ct_id end) as XREF_CT_2
      5  from cxrf
      6       join ct on (ct.design_id = cxrf.design_id)
      7  group by ct.design_id, ct.ct_id ;
    
     DESIGN_ID CT_ID        XREF_CT_1    XREF_CT_2
    ---------- ------------ ------------ ------------
        654321 10QWER123456 25ABCD       262ABCD
        987654 7ASDF654987  14WXYZ       34FRED
        321654 82CHEV852963 1TOM
     
    
  • I would like to convert a subselect to use an inner join?

    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
    I have the SQL below (it gives the correct data) using several sub selects I want to use in a PS query via the PS request handler. However, Manager queries PS will only a sub select when you create a query, but it will allow an inner join. So, I was hoping to rewrite it as a join but can not understand. Perhaps using a view would work? Or if it happens to be expert PS9.1 out there who might know an effcient way more to create the query that would be great as well. I have several versions of different trails of my SQL inner join if someone wishes to see those.
    select business_unit,
           ledger,
           fund_code,
           account,
           foreign_amount,
           open_item_key
      from ps_open_item_gl a
     where (business_unit, ledger, fund_code, account) in
           (select business_unit, ledger, fund_code, account
              from (select business_unit,
                           ledger,
                           fund_code,
                           account,
                           sum(foreign_amount)
                      from ps_open_item_gl b
                     where b.open_item_status = 'O'
                     group by business_unit, fund_code, account, ledger
                    having sum(foreign_amount) = 0))
       and open_item_status = 'O'
       and business_unit not in ('4110', '5301', '6501', '6602')
     order by 1, 2, 3, 4, 6 

    Hello

    956171 wrote:
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
    I have the SQL below (it gives the correct data) using several sub selects I want to use in a PS query via the PS request handler. However, Manager queries PS will only a sub select when you create a query, but it will allow an inner join. So, I was hoping to rewrite it as a join but can not understand.

    I don't know what you mean by "select sub", or what exactly is the PS limitation.
    Are you talking about the query nested in the IN clause? It is not necessary. The following is equivalent to what you posted:

    select business_unit,
           ledger,
           fund_code,
           account,
           foreign_amount,
           open_item_key
      from ps_open_item_gl a
     where (business_unit, ledger, fund_code, account) in
           (
               select    business_unit, ledger, fund_code, account
               from      ps_open_item_gl b
               where     b.open_item_status = 'O'
               and           business_unit  not in ('4110', '5301', '6501', '6602')
               group by  business_unit, ledger, fund_code, account
               having    sum (foreign_amount) = 0
           )
       and open_item_status = 'O'
     order by 1, 2, 3, 4, 6
    

    I move the condition 'and business_unit not in ('4110', '5301', '6501', 6602') ' in the subquery just for efficiiency.

    I think you would need a subquery, even if you don't re - he writes as a join.

    Perhaps using a view would work?

    No doubt. Again, I know not what PS objects to, but you can probably hide in a vision where the PS can complain about this.

  • Join SQL query help

    I have two DEVICE and CONTACT tables. The tables are joined for CONTACT_ID DEVICE_ID. I want to print device_ID and the corresponding coordinates on the lower contact. PRIORITY.
    SQL> describe DEVICE;
    DEVICE_ID              NOT NULL VARCHAR2(50)
    
    SQL> describe CONTACT;
    CONTACT_ID               NOT NULL VARCHAR2(50)
    CONTACT_TYPE             NOT NULL VARCHAR2(4)
    PRIORITY                 NOT NULL DOUBLE PRECISION
    LASTNAME                 NOT NULL VARCHAR2(30)
    FIRSTNAME                NOT NULL VARCHAR2(80)
    
    SQL> SELECT a.DEVICE_ID, b.LASTNAME,b.PRIORITY from DEVICE a, CONTACT b where a.DEVICE_ID = 'DEVICEA' and a.DEVICE_ID=b.CONTACT_ID(+);
    DEVICE_ID              LASTNAME    FIRSTNAME PRIORITY CONTACT_TYPE
    ---------------------- ----------- --------- -------- ------------
    DEVICEA                CONTACT1     GN               1 ROUT
    DEVICEA                CONTACT2     Hans             2 ROUT
    How to print a single record with the CONTACT the lowest. PRIORITY?

    Thank you
    Ravi

    rmalghan wrote:
    Thank you Frank, Sundar and Sean. I had less than 3 to work. Two questions. My table of DEVICES has 31936 records.
    1. the first one returned 30207, 31936 records 2nd and 3rd a 30348. Since the 2nd result resembles what I expected, the 1st and 3rd one seem to miss some records. For troubleshooting, I tried to limit the query to a few devices, but do not understand why the difference. Any suggestions?

    The 2nd query not check for type_contact = 'ROUT', it is not surprising that it returns more rows.
    The request of 3rd fact actually an inner join. The "WHERE c.rnum = 1" condition is applied after the join, so he rejects all rows that have been added due to the outer join (which is where all the columns of c are NULL). You probably meant to say:

    LEFT OUTER JOIN got_rnum c ON  d.DEVICE_ID     = c.CONTACT_ID
                               AND c.rnum          = 1; 
    

    without a WHERE clause.

    2. are there advantages in terms of efficiency. 2nd one does not seem to run faster, so I think it's the best. Any comments?

    SELECT a.DEVICE_ID, b.LASTNAME,b.FIRSTNAME,b.PRIORITY
    FROM DEVICE a, CONTACT b
    where
    a.DEVICE_ID=b.CONTACT_ID(+) and
    b.CONTACT_TYPE = 'ROUT' and
    b.PRIORITY = (select min(b1.PRIORITY ) from CONTACT b1 where b1.CONTACT_ID = b.CONTACT_ID);
    
    30207 rows selected (10.12 seconds)
    
    SELECT DEVICE_ID, LASTNAME, PRIORITY
    FROM (
    SELECT a.DEVICE_ID, b.LASTNAME, b.PRIORITY, ROW_NUMBER() OVER (PARTITION BY a.DEVICE_ID ORDER BY b.PRIORITY) row_num
    from DEVICE a, CONTACT b
    where a.DEVICE_ID=b.CONTACT_ID(+))
    WHERE row_num = 1;
    
    31936 rows selected (6.68 seconds)
    
    WITH got_rnum AS (
    SELECT LASTNAME, FIRSTNAME, PRIORITY, CONTACT_TYPE, CONTACT_ID,
    ROW_NUMBER () OVER ( PARTITION BY  CONTACT_ID ORDER BY priority) AS rnum
    FROM contact
    where CONTACT_TYPE = 'ROUT'
    )
    
    SELECT d.DEVICE_ID, c.LASTNAME, c.FIRSTNAME, c.PRIORITY
    FROM device d
    JOIN got_rnum c ON d.DEVICE_ID     = c.CONTACT_ID(+)
    WHERE c.rnum = 1; 
    
    30348 rows selected (9.07 seconds)
    

    This calendar is not very accurate. One that you run first is likely to be slower, because the data are less likely to be stored in the memory cache.

    The 2nd query has only 3 columns in the result set. Which probably doesn't change the speed a lot, but you should fix it anyway.

  • Eliminate the duplicate based on the condtion in Select of SQL query.

    Hi all

    I write the SQL query where I have to select values based on the condition in the column.

    Lets say I have 3 columns position, description, used, there are different values in the position but for some positions of the column description of the lines is the same and if column Description is the same and employee is null then that there should be only one row returned and if the description is the same but the employee column is not null then it should be several lines.

    I can't use Group by that we have around 35 columns in the select query.

    Please suggest any Solution.

    Hi Michael,

    I adds a column to the t2 to get the good understanding of my needs.

    Level
    Employee From Date to_date
    1 Test2 21.03.2014 21.04.2014
    2 Test4 21.02.2014 20.03.2014
    2 Test1 21.03.2014 21.04.2014
    2 Test3 21.04.2014
    3 MgrTest 21.03.2014

    Now, the result should look like this.

    Level
    Employee From Date TO Date
    1 Test2 21.03.2014 21.04.2014
    2 Test3 21.04.2014
    2 Test1 21.03.2014 21.04.2014
    3 Mgrtes 21.03.2014
    4

    There was an addition more as if this day is not null for the given level, then the query must return a single line of balnk more with the same position, I am reached using any Union and works very well I'm stuck with the point above.

  • CASE of Condition causing Plan difference

    Version
    SQL> SELECT * FROM v$version;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    CORE    11.2.0.2.0      Production
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    
    5 rows selected.
    Optimizer
    SQL> show parameter optimizer
    
    NAME                                 TYPE        VALUE
    ------------------------------------ ----------- ------------------------------
    _optimizer_autostats_job             boolean     FALSE
    _optimizer_join_elimination_enabled  boolean     FALSE
    optimizer_capture_sql_plan_baselines boolean     FALSE
    optimizer_dynamic_sampling           integer     2
    optimizer_features_enable            string      11.2.0.2
    optimizer_index_caching              integer     0
    optimizer_index_cost_adj             integer     100
    optimizer_mode                       string      ALL_ROWS
    optimizer_secure_view_merging        boolean     FALSE
    optimizer_use_invisible_indexes      boolean     FALSE
    optimizer_use_pending_statistics     boolean     FALSE
    optimizer_use_sql_plan_baselines     boolean     TRUE
    Problem

    We have a system of providers that built the SQL to be sent to the database. These queries have the following generic structure for a condition in WHERE clause:
    CASE WHEN <condition> THEN <table>.<column> END = '<value>'
    We have noticed different execution plans when the optimizer compares the numbers instead of varchar2. The examples below show that. In our case, this difference is an impact on the execution of a longer query plans effectively doubling buffer gets and leading to the execution times that are 2 to 4 times as worse.

    Query 1
    SQL> SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 1=1 THEN 'X' END = dummy;
    
    D
    -
    X
    
    1 row selected.
    
    SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null,'ALLSTATS LAST'));
    
    PLAN_TABLE_OUTPUT
    ----------------------------------------------------------------------------------------------
    SQL_ID  6ghjubgpwpr61, child number 0
    -------------------------------------
    SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 1=1 THEN
    'X' END = dummy
    
    Plan hash value: 272002086
    
    ---------------------------------------------------------------------------------------------
    | Id  | Operation         | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
    ---------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |      |      1 |        |      1 |00:00:00.02 |       2 |      2 |
    |*  1 |  TABLE ACCESS FULL| DUAL |      1 |      1 |      1 |00:00:00.02 |       2 |      2 |
    ---------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("DUMMY"='X')
    Query 2
    SQL> SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 'A'='A' THEN 'X' END = dummy;
    
    D
    -
    X
    
    1 row selected.
    
    SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null,'ALLSTATS LAST'));
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------
    SQL_ID  gcpwzksqr2w9n, child number 0
    -------------------------------------
    SELECT /*+gather_plan_statistics*/ * FROM DUAL WHERE CASE WHEN 'A'='A'
    THEN 'X' END = dummy
    
    Plan hash value: 272002086
    
    ------------------------------------------------------------------------------------
    | Id  | Operation         | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |      |      1 |        |      1 |00:00:00.01 |       2 |
    |*  1 |  TABLE ACCESS FULL| DUAL |      1 |      1 |      1 |00:00:00.01 |       2 |
    ------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("DUMMY"=CASE  WHEN ('A'='A') THEN 'X' END )
    To my eyes the status 'A' = 'A' is equivalent to the condition 1 = 1. How did in the case of the comparison digital Oracle allows to eliminate the case expression, but compared to the character he can't?

    Thank you!

    Centinul wrote:
    To my eyes the status 'A' = 'A' is equivalent to the condition 1 = 1.

    Well, question is Oracle looks like 'A' = 'A' as the string comparison. And the result of a string comparison is NLS charging. This is why it can not evaluate CASES at compile time.

    SY.

  • Independent research - how to join ODI - SQL Server

    Hi all

    My Sql server query joins as follows

    Select...
    Of A.DBO. MAIN
    A.DBO inner join. LOOKUP_STANDALONE on 1 = 1

    How to convert ODI?

    Thank you for the time and help.

    Hello

    I think what you want to do is a Cartesian join (join all the lines of the HAND with all the lines of LOOKUP_STANDALONE).

    You can do this in ODI by dragging a random column of the HAND on a column of LOOKUP_STANDALONE (any column). Then click on your join and check the box 'Cross.

    It will be useful,

    JeromeFr

  • Issue of inner joins

    Hi all
    I have a few tables with samples as:
    CREATE OR REPLACE TABLE hotel
    ( 
         hotel_id             int  NOT NULL ,
         hotel_name           varchar2(50)  NULL ,
         hotel_address        varchar2(100)  NULL
    );
    
    CREATE OR REPLACE TABLE people_type
    ( 
         people_type_id       int  NOT NULL ,
         type_des             varchar2(30)  NOT NULL
    );
    
    CREATE OR REPLACE TABLE hotel_people
    ( 
         hotel_id                int  NOT NULL ,
         people_type_id      int  NOT NULL ,
         age_min                int  NULL ,
         age_max               int  NULL
    );
    
    CREATE OR REPLACE TABLE hotel_service
    ( 
         hotel_id              int  NOT NULL ,
         qty_room             int  NOT NULL ,
         qty_people           int  NOT NULL,
         qty_people_extra      int  NOT NULL
    );
    
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(1,'hotel1','hotel1 address1');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(2,'hotel2','hotel2 address2');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(3,'hotel3','hotel3 address3');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(4,'hotel4','hotel4 address4');
    
    insert into people_type (people_type_id, type_des)
    values (1,'Baby');
    insert into people_type (people_type_id, type_des)
    values (2,'Young');
    insert into people_type (people_type_id, type_des)
    values (3,'Adult');
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,1,0,2);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,2,3,10);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,3,11,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (2,2,3,17);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (2,3,18,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (3,3,18,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,1,0,2);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,2,3,10);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,3,11,200);
    
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (1,10,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (1,5,4,0);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (2,6,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (2,12,3,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (3,10,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (3,5,4,0);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (4,6,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (4,12,3,1);
    As you can see I have Hotels with basic information, persons of type hotel_people (1:baby, 2:young and 3:Adult), which contains the type of people that each hotel can accommodate with min max age and hotel_service that contains the info from the number of rooms and the amount of people that can be in each room.
    I'm trying to get the records that match my condition as:
    1. I want to get the hotels that can accommodate 4 pieces with adults of 3 for each room.
    2 get hotels that can accommodate 6 rooms 2 where 2 of the bedrooms can have 1 young (3 people per room) and young people 9 years.

    For 1:
    select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max
    from hotel h
    inner join hotel_service ss on h.hotel_id = ss.hotel_id
    inner join hotel_people p on h.hotel_id = p.hotel_id
    inner join people_type t on p.people_type_id = t.people_type_id
    where ss.qty_room >= 4
    and t.people_type_id = 3
    and ss.qty_people >= 3
    For 2:
    select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max
    from hotel h
    inner join hotel_service ss on h.hotel_id = ss.hotel_id
    inner join hotel_people p on h.hotel_id = p.hotel_id
    inner join people_type t on p.people_type_id = t.people_type_id
    where ss.qty_room >= 6
    and ss.qty_people >= 3
    and t.people_type_id in (2,3)
    and 9 between p.age_min and p.age_max
    This does not work, the start of the query becomes more complex if I try to search for multiple rooms with a different number of people and children of different ages.
    Can anyone help with this?
    Thank you

    Published by: user9542267 on April 3, 2012 22:26

    Hello

    user9542267 wrote:
    ... Yes, what I need is a hotel that can accommodate TWO adults and youth. What do you mean by two copies?

    Right now I read something on the Grand Duke by Gilbert. I also have a book with the complete works of Gilbert, so I can leave it open, and when I see a reference in the commentary, I see exactly what the author is talking about in the book. However, when the comment compares some parts of this game to the other plays of Gilbert, The Yeomen of the Guard , it gets hard, because I have to keep changing the pages in the complete works, and I can't see both at the same time. It would be much easier if I could get a full second copy of the work; then I could leave a copy of the Grand Duke and leave the other copy open to The Yeomen of the Guard . This is similar to what you should do with the table of hotel_people, and what I do with the table scott.emp below.

    Imagine (using tables in the scott schema) you have to find the departments where both an analyst and a clerk to work.
    This:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       scott.emp     e  ON     d.deptno     = e.deptno
    WHERE       job          IN ('ANALYST', 'CLERK')
    ORDER BY  d.deptno
    ;
    

    product

    DEPTNO DNAME          LOC
    ------ -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
    

    because all these departments have either an analyst or a clerk. However, only deptno = 20 a time .
    If, instead of a single table of big job, you have a separate table for each Olympic Games, then you might do something like this:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       analyst     a  ON     d.deptno     = a.deptno
    JOIN       clerk          c  ON     d.deptno     = c.deptno
    ORDER BY  d.deptno
    ;
    

    Of course, you don't have tables separated like that, but you could generate result sets who behaved as separte tables, for example:

    WITH     analyst          AS
    (
         SELECT  *
         FROM     scott.emp
         WHERE     job     = 'ANALYST'
    )
    ,     clerk          AS
    (
         SELECT  *
         FROM     scott.emp
         WHERE     job     = 'CLERK'
    )
    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       analyst     a  ON     d.deptno     = a.deptno
    JOIN       clerk          c  ON     d.deptno     = c.deptno
    ORDER BY  d.deptno
    ;
    

    Note that the main request is exactly what I posted earlier.
    What produces the right output:

    DEPTNO DNAME          LOC
    ------ -------------- -------------
        20 RESEARCH       DALLAS
    

    There is no need to actually generate separate result sets, however. You can use two copies of the table scott.emp in the same query, as follows:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       scott.emp     a  ON     d.deptno     = a.deptno
    JOIN       scott.emp     c  ON     d.deptno     = c.deptno
    WHERE       a.job          = 'ANALYST'
    AND       c.job          = 'CLERK'
    ORDER BY  d.deptno
    ;
    
  • Update with INNER JOIN

    Hello

    My update with the inner join does not seem to work.

    UPDATE RECAP R SET R.FLAVOR = (SELECT FN. FLAVOR_NDC FN FLAVOR, REPLACE CAP R WHERE R.NDC11 = FN. NDC11)

    When I write the query above, the inner circle question (SELECT FN. FLAVOR_NDC FN FLAVOR, REPLACE CAP R WHERE R.NDC11 = FN. NDC11) returns multiple lines, and it's a new syntax for me (as I was Teradata and SQL server).

    Can you please how this request can be written to make it work?

    I get the error message below

    SQL error: ORA-01427: einreihig subquery returns multiple rows
    01427 00000 - "einreihig subquery returns several lines.

    1. fix your code:

    UPDATE RECAP R SET R.FLAVOR = (SELECT FN.FLAVOR FROM FLAVOR_NDC FN WHERE R.NDC11 = FN.NDC11)
    

    2. you can use the fusion

    merge into RECAP R
    using FLAVOR_NDC FN
    on(R.NDC11 = FN.NDC11)
    when matched then
         update
         set R.FLAVOR = FN.FLAVOR
    

    Kind regards
    Malakshinov Sayan

  • The join SQL query help

    I'm just having a bit of troubel get a correct join query - I thought it was an Inner Join, but I don't get the results I expect.

    My table structure is:

    Table: lodges

    LodgeID (PK)

    Lodge

    etc.

    Table: implemented application

    NominationID (PK)

    Category

    LodgeID

    Year

    So I try to use this structure to replicate this page:

    http://www.safariawards.com/nominees12/

    That is to say a list of boxes for each category, they are appointed on.

    The query I've tried looks like this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. NominationID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

    But this product:

    http://www.safariawards.com/nominees12/southernafrica.php

    Its the right number of results, but not the list on the right of the boxes - for example British Airwways is not LodgeID 786

    If anyone could help with the SQL right for what would be well appreciated.

    That you join on the wrong column. Try this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. LodgeID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

  • Urgent help please.  Inner join caused the error ora-00933

    I ran it, works great:
    SELECT DIFFERENT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'),
    TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,

    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,

    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    left outer join
    (SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE
    ) LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE
    ) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID
    inner join TT_DATU_LRN_MAP on exp. EXP_ID = NAB NAB. EXP_ID AND TRIM (NAB. LRN) AS p_LRN
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE THERE IS NOT (SOME SATELLITE_EXP_ID OF TT_HOST_SATELLITE WHERE EXP. EXP_ID = SATELLITE_EXP_ID)
    AND EXP.IS_PRIMARY_ADDRESS LIKE p_isPrimary;
    ON THE OTHER
    OPEN FOR V_cursor
    SELECT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'), TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,

    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,

    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    (in left outer join
    SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE)
    LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE
    ) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID ORDER BY EXP. DATU_EXP_CLLI
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE THERE is NOT (SOME SATELLITE_EXP_ID OF TT_HOST_SATELLITE WHERE EXP.. EXP_ID = SATELLITE_EXP_ID) AND EXP.IS_PRIMARY_ADDRESS love
    p_isPrimary;


    However this one:

    SELECT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'),
    TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,


    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,


    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    left outer join
    (
    SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE) LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID

    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE
    ) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID ORDER BY EXP. DATU_EXP_CLLI
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE the EXP.IS_PRIMARY_ADDRESS as p_isPrimary;

    It does not work kept send me errors:
    [ORA-00933: SQL not correctly completed command]


    Any guru can help? I need to have this resolved effect today.
    Thanks in advance.
  • helps the inner join

    Hi all

    I am trying to learn something new, and I'm having a little difficulty.  I'm trying to learn more about inner joins.  I looked at countless books, tutorials, and all the rest have that I can find, but I can't seem to understand that.  Nothing I try works.

    I have related tables, and I try to get the data to display.  I have the database set up as follows:

    films (table name) has the movie_id, movie_name and movie_star_rating fields

    family_rating (table name) has star_id, star_name, and star_file_name fields

    in PHPMyAdmin, I had movies.movie_star_rating internally associated with family_rating.star_id

    When I view the data, I'm pulling the table of movies, but instead of having a number, I would like to have displayed family_rating.star_name.  I have the following code to do this:

    $query="SELECT * FROM movies
      INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id 
      WHERE movies.movie_name_series='$search'";
    
    $result= mysql_query($query)or die (mysql_error());
    
    while($row=mysql_fetch_array($result)){
    
    // Display the data 
    
     echo $row['movies.movie_name'];
     echo $row['family_rating.star_file_name'];
    }
    


    Can someone let me know what I am doing wrong?  Thanks for your help.

    $query = "SELECT * FROM movies, family_rating"
    INNER JOIN family_rating ON movies.movie_star_rating = family_rating.star_id
    WHERE movies.movie_name_series = '$search';

    OK, that SQL is not the same as your original post.

    > Not unique table/alias: 'family_rating '.

    This tells us what's wrong. You specify the two tables after the from clause. Try again with your original query:

    $query = "SELECT * FROM movies"
    INNER JOIN family_rating ON movies.movie_star_rating = family_rating.star_id
    WHERE movies.movie_name_series = '$search';

Maybe you are looking for