Complex Inner Join and outer subquery where clause to join internal?

The DDL for this post was too big and I had to put on my site:
http://www.harbortownsolutions.com/DDLForDORTable.txt

My goal is to create a report of sales data for 116 stores containing daily sales and count of comments in recent years. My challenge is that I am pulling data from the same table, but with different dates to get data from last year and the week in data from date of last year.


The dates used for reports are:

SalesDate = 06/04/2009
SalesDateForLastYear = 2008-06-05
SalesDateBeginningOfWeek = 01/06/2009
SalesDateBginningOFWeekLastYear = 2008-06-02

PSEUDOQUERYS:
---======= DOLLAR VARIANCE = You Said you have $100 worth of meat(inventory) but you only have $50 worth of meat
SELECT 
    BUSI_DATE as BusinessDate, 
    ORACLE_KEY as StoreID,
    FIELD1  as Sales,
    FIELD2  as GuestCount,
    FIELD3  as DollarVariance,
    FIELD4  as Cars
FROM MYDAILYTOTALS
WHERE Busi_date = SalesDate
   AND ORACLE_KEY IN (SELECT StoreID From MyStores); 
-======= ONLY Guest count and Sales are needed in comparisons against last year    
---======    Last YEar dates = corresponding dates for this year ie Tuesday of this 
week matched to Tuesday of last year (except for leap year)
SELECT 
    ORACLE_KEY as StoreID
    FIELD1  as SalesLastYear
    FIELD2  as GuestCountLastYear   
FROM MYDAILYTOTALS
WHERE Busi_date = SalesDateForLastYear 
   AND ORACLE_KEY IN (SELECT StoreID From MyStores);
       

SELECT 
    ORACLE_KEY as StoreID
    SUM(FIELD1)  as WeekToDateSales
    SUM(FIELD2)  as WeekToDateGuestCount  
FROM MYDAILYTOTALS
WHERE Busi_date BETWEEN SalesDateBeginningOfWeek and SalesDate
   AND ORACLE_KEY IN (SELECT StoreID From MyStores) 
GROUP BY ORACLE_KEY 
   --=======  
SELECT 
    ORACLE_KEY as StoreID
    SUM(FIELD1)  as WeekToDateSalesLastYear
    SUM(FIELD2)  as WeekToDateGuestCountLastYear  
FROM MYDAILYTOTALS
WHERE Busi_date BETWEEN SalesDateBeginningOfWeekLastYear and SalesDateLastYear
   AND ORACLE_KEY IN (SELECT StoreID From MyStores) 
GROUP BY ORACLE_KEY   
Question: Since they all use the same store of nbrs, I specify on each request? Can't I just specify only once on outer query Where clause?

Also, how would fit the following of HOEK script:
See how I put 1 field based on another field value

He reads a record for each store of 116 stores in the database. There are 2 situations that have been addressed by Hoeks code:
For each of the 116 stores in the store table, there must be 1 and only
registration of 1 stock to match the record of MyStoreTotals. However, sometimes there is no record of inventory.
and sometimes there will be 2 (in a case where a manager of database updates)
After daily treatment) files. If so, the record with Is_posted = 1
is the one that should be included in the query. The following text accomplishes this:
-chr (39) is the single quote
 Select StoreId, 
         DollarVariance 
  from   (select store.storeid, 
                   case
                          when inv.storeid is null then 'None'
                          when inv.is_posted = 0 then 'NP'
                          else chr(39)||inv.total_dol_var||chr(39)
                    end as DollarVariance, 
                    row_number() over (partition by store.storeid order by inv.is_posted desc) rn  
               from  myInven inv 
               right outer join mystores  store
                              on store.storeid = inv.fk_str_main_id
                              and inv.busi_date = to_date(SalesDate, 'dd-mon-yyyy'))
      where rn = 1;      
Can I put this request on the place where clause of the outer to query I put on a JOIN clause?

So I guess that's why you used the MAX.

That is right.
I used CASES to identify and complete the necessary columns since the first set of results (WITH).
So then I more or less "swivels somehow" lines of this result set to the columns, each store will appear once the most.

Tags: Database

Similar Questions

  • Convert and To_Char in where clause does not

    Hello

    I try to get the following sql beating, but it always generates an error. Any ideas?

    SELECT

    e_Equipment.EQU_ID,

    e_Equipment.EQU_PC_name,

    e_Equipment.EQU_DESCR,

    e_Equipment.EQU_SERIAL_NO,

    e_Equipment.EQU_STATUS,

    a_POBSYSENUMS. PSS_ID

    Of e_equipment

    LEFT JOIN

    A_POBSYSENUMS

    ON e_Equipment.EQU_STATUS = CONVERT (A_POBSYSENUMS. PSS_ID, ' varchar (2)') AND A_POBSYSENUMS. PSS_PSE_PK = '76'

    WHERE

    e_Equipment.EQU_ORG_PK = 8271;

    Thank you very much

    Christina

    Looks like you are trying to use the MS SQL Server syntax rather than the Oracle syntax.

    You did not say why to_char does not work for you...

    Try...

    e_Equipment.EQU_STATUS = TO_CHAR (A_POBSYSENUMS. PSS_ID)

  • Compare two tables that have columns of nulls and undesirable in where clause

    Table A:

    col1col2COL3COL4col5col6

    ANY

    NULL VALUE

    NULL VALUE

    YES

    YES

    YES

    NULL VALUE

    NULL VALUE

    YES

    NULL VALUE

    YES

    YES

    NULL VALUE

    ANY

    YES

    ANY

    NULL VALUE

    YES

    NULL VALUE

    NULL VALUE

    ANY

    YES

    YES

    YES

    Table B:

    col1col2COL3COL4col5col6

    ANY

    NULL VALUE

    NULL VALUE

    YES

    ANY

    YES

    ANY

    NULL VALUE

    YES

    NULL VALUE

    YES

    YES

    NULL VALUE

    ANY

    YES

    NULL VALUE

    NULL VALUE

    YES

    ANY

    NULL VALUE

    ANY

    YES

    YES

    NULL VALUE

    column values

    Yes: Current value

    No: Null

    All: Junk

    Must match all columns in TableA to TableB.

    TableA.col1 to TableB.col1

    .

    .

    .

    TableA.col6 to TableB.col6

    Any help is very appreciated

    Thank you

    Based on your last post that only good data must match exactly and your definition of junk:

    WHERE (decode (substr (A.col5, 1, 1), ' ~ ', 'junk mail', null, 'null', 12/31/9999 ', "junk," A.col5 ") = decode (substr (B.col5, 1, 1), ' ~ ', 'junk mail', null, 'null', 12/31/9999 ',"junk", B.col5"))

    AND (decode (substr (A.col6, 1, 1), ' ~ ', 'junk mail', null, 'null', 12/31/9999 ', "junk," A.col6 ") = decode (substr (B.col6, 1, 1), ' ~ ', 'junk mail', null, 'null', 12/31/9999 ',"junk", B.col6"))

    And so on for the six columns.  This assumes that this junk * still * starts with a ~.  In addition, it assumes that the unnecessary and null words are not in the form of good data.

  • dynamic where clause ofa

    Dear all,

    I have a problem in the dynamic where clause in the OPS.

    I have a VO query as below:

    SELECT desc, ccuid, containid, XXXTMP conainline

    I'll put whereclause as below:

    VO.setwhereclause ("containid '101' =" + "order by containline desc")

    so the question is request forms as below:

    SELECT desc, ccuid, containid, XXXTMP conainline

    (where containid = '101' containline desc order) -query gives question, adding the command from the inside of the caliper

    Ideally, order by clause should come outside of the Stirrup.

    so, how do I do order by clause order side where clasue or how do I remove the stand before the where clause. So I need the query as below:

    SELECT desc, ccuid, containid, XXXTMP conainline

    (where containid = '101' containline desc order)

    SELECT desc, ccuid, containid, conainline from XXXTMP where containid = '101' containline desc order

    or

    SELECT desc, ccuid, containid, XXXTMP conainline

    (where containid = '101') order by containline desc

    Thank you

    Hello

    Solution 1-

    You can use "setOrderByClause (containline desc);" so that it will get out of Where clause.

    Solution 2-

    Instead of dynamically add Whereclause, you can bind the variable as below-

    Query in VO-

    SELECT desc, ccuid, containid, conainline from XXXTMP where containid =: 1 containline desc order


    In VOImpl-

    public void initQuery (String variable)

    {

    clearCache();

    setMaxFetchSize(-1);

    setWhereClause (null);

    setWhereClauseParams (null);

    setWhereClauseParam (0, variable);

    executeQuery();

    }

    Vall of this VOImpl method, and pass the variable value. It will work according to your requirement.

    Thank you

    Kone

  • Outer joins and null in the 'where' clause condition

    Hi people,

    Please help me on this.

    Here's my query.

    with x

    (select 'a' as a dual union all col1)

    Select 'b' as col1 of union double all the

    Select 'c' as double col1

    ),

    y as

    (

    Select 'b' as col2 from dual Union all the

    Select 'c' as col2 from dual Union all the

    Select would be "as col2 from dual Union all the"

    Select 'e' as col2 from dual

    )

    Select * x y right outer join

    on x.col1 = y.col2 and y.col2 is null

    Get all the lines of 'COL1' as null. Why like this?

    Just add the condition to the WHERE clause for example

    WITH x AS
    (SELECT 'a' AS col1 FROM dual UNION ALL
    SELECT 'b' AS col1 FROM dual UNION ALL
    SELECT 'c' AS col1 FROM dual
    ),
    y AS
    (
    SELECT 'b' AS col2 FROM dual UNION ALL
    SELECT 'c' AS col2 FROM dual UNION ALL
    SELECT 'd' AS col2 FROM dual UNION ALL
    SELECT 'e' AS col2 FROM dual
    )
    SELECT * FROM x LEFT OUTER JOIN y
    ON x.col1=y.col2                     ----want to add "and y.col2 is null " condition to get value "a"
    where y.col2 is null
    
  • Difference-conditions (join and a Where Clause)

    Hi people,

    I need to clearly agree on what a difference exactly when we put any condition in INNER JOIN and the WHERE Clause.

    I have tried both way and found the same results. Even in the statistics Plan not much differences.  Any help would be appreciated.

    As:

    1 here, I use filter store in the join condition - Inner

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    and I.LOCATION_ID in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    Mark



    Hello

    MarkCooper wrote:

    Hi guys,.

    To reply to all - I understand.

    1. its good practice to use conditions / filter (except CLAUSE) in the WHERE Clause rather Inner join? bon ?

    2. now, in my previous example. We could use the location code in where clause as it was in the two tables.

    What is the best practice to use the code to location here ( 1 /2) ?

    1. here I use filter store in the Inner join condition ( guess the location code is not in the article table)).

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

      And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause (assume that the location code is not in the article table)-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

      And Gl.Location_Id in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    If location_id isn't in the item table, then the join condition

    On Gl.Location_Id = I.Location_Id

    will cause an error.

    Once more, it should not affect results or performance if a condition like

    Gl.Location_Id in (1767, 1747,202,1625)

    is in the clause or the WHERE clause.  No matter if it is be a column called location_id in any other table, or if the same column Gl.Location_Id is used in other conditions.

    As this condition only refers to a table (GI), I recommend you put it in a WHERE clause, just to make the code clearer.

    Yet once, this applies only to the inner joins, not for outer joins and not to CONNECT BY queries.

  • Syntax of UPDATE of Oracle with the JOIN and the WHERE clause

    I need to update one of my paintings and can't seem to get the syntax right. I tried 4 different approaches, but does not have a chance.  Any help would be greatly appreciated.

    Attempt #1: Error: not correctly completed SQL command

    UPDATE TESTDTA.F4941
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS';

    Try #2: Error report: SQL error: ORA-00904: "TESTDTA." "" F4981 ". "' FHUOM ': invalid identifier 00904. 00000 - '% s: invalid identifier '.

    UPDATE (SELECT TESTDTA.F4941
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS')
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM


    Try #3: Error report: SQL error: ORA-00904: "TESTDTA." "' F4941 ': invalid identifier 00904. 00000 - '% s: invalid identifier '.

    UPDATE (SELECT *
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS')
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;


    Try #4: Error: ORA-00925: Missing keyword 00925. 00000 - "missing IN the keyword"

    MERGE TESTDTA.F4941
    USING TESTDTA.F4981
    ON (TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    AND TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS'
     
    )
    WHEN MATCHED THEN
    UPDATE SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ
      
    ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;

    Finally managed to do work, adding DISTINCT to select.  I have pasted the SQL error in Google to begin with.  Laughing out loud!

    Code snippet work for anyone who comes across this thread...

    MERGE INTO TESTDTA. F4941 DST

    USING (SELECT DISTINCT TESTDTA. F4941. RSSHPN,

    TESTDTA. F4941. RSDSTN,

    TESTDTA. F4941. RSUMD1,

    TESTDTA. F4941. RSADDJ,

    TESTDTA. F4981. FHSHPN,

    TESTDTA. F4981. FHRTDQ,

    TESTDTA. F4981. FHUOM,

    TESTDTA. F4981. FHCGC1

    OF TESTDTA. F4941

    JOIN INTERNAL TESTDTA. F4981

    ON TESTDTA. F4941. RSSHPN = TESTDTA. F4981. FHSHPN

    WHERE - TESTDTA. F4941. RSSHPN = 647143 AND

    TESTDTA. F4941. RSDSTN = 0

    AND TESTDTA. F4941. RSADDJ > 110365

    AND TESTDTA. F4981. FHCGC1 = 'SAY') CBC

    on (DST. RSSHPN = SRC. FHSHPN)

    When matched then update

    SET DST. RSDSTN = SRC. FHRTDQ, SUMMER TIME. RSUMD1 = SRC. FHUOM;

    Thanks to everyone who helped with this.

  • Difference between Inner join and right outer join...

    Which is precisely the difference between an Inner join and right outer join...

    JOIN INTERNAL:-to return all rows from the two tables where there is a football game. That is to say. the table resulting from all the rows and columns will have values.

    AND

    RIGHT OUTER JOIN:-Returns all rows in the second table, even if there is no match in the first table.

  • Update statement with joins of tables and where Clause

    Hi, I have MS SQL background and I try to execute an update statement in Oracle with joins of tables. However, the syntax below does not work but I think it works for MS SQL.

    Basically, the base table must be attached to a master table trend with monthly snapshots, an account will be only an entry for a given date monthly. Where clause must be limited to accounts within a certain range of interest rates.

    The first approach returns command SQL ORA-00933 not correctly completed, and the second approach returns ORA-01427 row below query returns multiple rows. Can anyone help? Thanks in advance!



    1:

    Update PenaltyAll
    Set a.indicator = month (b.)
    of PenaltyAll an inner join Master b on a.acctno = b.accountnumber
    where a.monthend='01/31/2009' and b.date='12/31/2008' and b.apr < 20

    2:

    Update PenaltyAll
    adjustment indicator =
    (select to_char (b., 'MM')
    of PenaltyAll an inner join Master b on a.acctno = b.accountnumber
    "where to_char (a.monthend,'mm/dd/yyyy ') = 31 January 2009"
    (et to_char(b.date,'mm/dd/yyyy') = December 31, 2008 "
    and b.apr < 20)

    Published by: sqlrookie on August 21, 2009 07:04

    I edited my post, that was my mistake, ANC you try now?

  • Question about the order of evaluation of the clause WHERE CLAUSE when the Oracle OF the syntax used to join tables

    Hello

    Oracle version: 11.1.0.7.0 - 64 bit

    I read the documentation online at joins. The page is avialable here: joins at

    My question is about the join order of evaluation of the conditions in clause and the conditions of those

    are not the join conditions and are placed in the WHERE clause.

    Consider the following pseudocode

    SELECT

    T1. Col1,

    T2.Col1

    Of

    Table1 t1 LEFT OUTER JOIN table2 t2

    WE

    (condition_expression1)

    WHERE

    (condition_expression2)

    Is it correct to say that if there is no column on the status of join (condition_expression1) in condition_expression2, then condition_expression2 is executed before condition_expression1? In other words, oracle always trying to filter based on the WHERE clause individually each table as much as possible before joining them based on the conditions on the article?

    Thanks in advance,

    Hello

    dariyoosh wrote:

    Hello

    Oracle version: 11.1.0.7.0 - 64 bit

    I read the documentation online at joins. The page is avialable here: joins at

    My question is about the join order of evaluation of the conditions in clause and the conditions of those

    are not the join conditions and are placed in the WHERE clause.

    Consider the following pseudocode

    SELECT

    T1. Col1,

    T2.Col1

    Of

    Table1 t1 LEFT OUTER JOIN table2 t2

    WE

    (condition_expression1)

    WHERE

    (condition_expression2)

    Is it correct to say that if there is no column on the status of join (condition_expression1) in condition_expression2, then condition_expression2 is executed before condition_expression1? In other words, oracle always trying to filter based on the WHERE clause individually each table as much as possible before joining them based on the conditions on the article? ...

    The reverse is actually closer to the truth, but we can't really make general statements like that.

    SQL is not a language of the proceedings.  Looking at the code SQL, we could say that the code does, but we cannot say much about how that code it.  In other words, SQL is a language that describes the results you get, not the way to get them.

    The optimizer will do everything what he thinks is faster if it does not change the results.  If any order in which they are applied (in outer joins or CONNECT BY queries, for example), then think of the join is done first, and the value of the WHERE clause is applied to the result of the join.

    Here is a query looks very much like you posted:

    SELECT d.deptno

    e.ename, e.sal

    OF scott.dept d

    LEFT OUTER JOIN scott.emp e ON e.deptno = d.deptno

    WHERE e.sal > = 3000

    ORDER BY d.deptno

    ;

    Output:

    DEPTNO ENAME SAL

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

    10 KING 5000

    20 FORD 3000

    20 3000 SCOTT

    The scott.dept table contains deptnos 30 and 40; Why are they not in the result set?  The query behaves as if the outer join is made first (production 15 rows), then the WHERE clause has been applied.  All lines with deptno = 30 had sals down han 3000 and all single line with deptno = 40 was NULL in the sal column, then these lines are excluded (as well as other lines of deptnos 10 and 20), and only 3 lines above are left.

  • May I ask where clause to satisfy two conditions at the same time (and)

    Hello

    I wrote a few joins and usually in the final stage where condition with certain conditions be satisfied in where clause. In all conditions where I only want one and condition fulfilled only if both are collected.

    If we look at the this funciton is I should meet (get_order_type (ogr.order_number) <>'R')) AND wh1.third_party_warehouse! = 'Y') but I'm not get rid off both that if order_type is 'R' they are removed and if tooling_group is yes then ther are removed the result. I'm trying with both below pointed out together how can check these conditions? I can do in where clause?

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    Of order_goods_received ogr

    LEFT JOIN part_master pm

    On ogr.part_number = pm.part_number

    -stock_groups sg left join

    SG stock_groups left join

    on pm.stock_group = sg.stock_group

    and ((sg.tooling_stock_group! = 'Y'))

    and (get_order_type (ogr.order_number)! = 'R'))

    -Pm.stock_group = sg.stock_group

    LEFT JOIN batch_record_1 br1

    -A FEW MORE CODE HERE-

    WHERE ((ogr.delivery_date > = trunc (date_départ)))

    AND

    ((ogr.delivery_date < = trunc (end_date)))

    AND

    OGR.goods_received_number <>'1' AND

    get_order_type (OGR.order_number) NOT IN ('L') AND

    OGR.returned_to_supplier_indicator not in('S','L') and

    -sg.tooling_stock_group <>'Y '.

    -((sg.tooling_stock_group <> 'Y') and (get_order_type (ogr.order_number) not in ('R')))

    -(get_order_type (OGR.order_number) <>'R' AND sg.tooling_stock_group <>'Y')

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    AND wh1.third_party_warehouse! = « Y »

    UNION

    (

    Thank you.

    Whenever you have a problem with such logic, you should try and draw a logic state diagram:

    In your case, you are looking to two variables, with two possible States (equal to a value or is not it)

    Then, take this sense of the sg.tooling_stock_group T and O as being the get_order_type (ogr.order_number) and create a state diagram logical based on your description of what you want.  We will use a value of "Y" to indicate we want to record and a value of "N" to indicate the folder should not be returned.

    Start with an empty logical diagram...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O = 'R '.
    |         |         |
    |         |         |


    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    First of all, you said: "If there is the result returned by the defined user function is"R"and the table that has the tooling_stock_group y then it should avoid this record."

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Then you said: "If order_type does not return" R' but the tooling_stock_group is 'Y', then I should not avoid recording ".

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now, you have not given any logic for the rest of the diagram, so I made the assumption that you do not want to avoid these records...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now there are different ways to determine the location of the clause of this diagram.
    We could say that we want the records for all of the T column! = "Y" or all the records for all of the O line! " = R...

    where (sg.tooling_stock_group! = 'Y' or get_order_type (ogr.order_number)! = 'R')

    We say we want to EXCLUDE (using DO NOT) all records where T = "Y" AND O = "O" "

    If not (sg.tooling_stock_group = 'Y' and (ogr.order_number) get_order_type = 'R')

    Those are the two most logical ways to do.
    (In my previous answer, that we can see in this diagram, there is no need of the "or get_order_type (ogr.order_number).) (= 'R' "part, which is superfluous, but would not break it)

    Now let's look at your logic to see what it looks like to in the diagram...

    where ((sg.tooling_stock_group <> «Y»))
    and (get_order_type (ogr.order_number) <> 'R')
    )

    In a logic diagram that looks to...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    N    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    As you can see, your logic was only targeting one of the 4 possible results for select records and excluding 3.

  • How to do an INNER JOIN subquery?

    Can't seem to find anything that could help out me with this.
    My prototype query goes like this but its gives me an ORA-00905: lack of keyword


    SELECT Poker_Site.site_name, bonus.sign_up_bonus
    OF poker_site
    INNER JOIN bonus
    WHERE Poker_site.site_name = 'Poker Stars'
    WE Poker_Site.site_name = bonus.site_name;

    Join INNER JOIN must be immediately followed by ONE and only then, where:

    SELECT Poker_Site.site_name, bonus.sign_up_bonus
    FROM poker_site
    INNER JOIN bonus
    ON Poker_Site.site_name = bonus.site_name
    WHERE Poker_site.site_name = 'Poker Stars';
    

    SY.

  • 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 vs WHERE (+) rating

    I am trying to determine the best way to write joins in Oracle. I'm a guy from SQL Server, and I'm used to writing

    Select t1.c1 from table1 t1 INNER JOIN table2 t2 ON t1.c1 = t2.c1

    I have been informed that the Oracle compiler prefers the sign (+) rating to join columns in which the clause. I have also been informed that Oracle has deprecated notation plue for INNER JOIN scoring in 10g. What is the real answer?

    I personally never did a comparison of performance between the two.

    I've always preferred the ANSI rating because I think it is more descriptive than the Oracle outer join notation.

  • Small design on the points of view and a Where Clause Board...

    Hi all...  I just want to check Im doing things right, and he is not a better way to do this...

    Running APEX 4.2.5 and XE 11.2

    Ive built my application around a community of users who have access to certain groups of data...  Ive designed so that there are views and then transparent filter the data of a given user. for example, the view below seamlessly that users can see the filters depending on which application they use, they can see more specifically, what are the features and finally, what group they are actually in.

    Create view ENTITIES like)

    Select * from t_entities t

    where exists (select 1

    of application_groups ag

    where ag.object_name = 'ENTITIES '.

    and ag.object_id = t.entity_id

    and ag.group_name = 'CASH FLOW')

    and exists (select 1 from entity_users EU

    where eu.username = nvl (v ('APP_USER'), USER) / * APEX APPLICATION USER * /.

    and eu.entity_id = t.entity_id)

    and it exists (select 1 from entity_group_members for example

    where eg.entity_group = v('G_ENTITY_GRP_ID') / * ENTITY_GROUP * /.

    and eg.entity_id = t.entity_id)

    )

    The overall costs when I run the explain command is very low (23 or almost), so I think that its an effective way of filtering... However, out of 300 entities or greater, the vast majority of users (75%) can see 75% of data...  Also, I need to create another view to filter another set of data with 2000 records. one with the view in place, 95% of the staff will see 95% of the data and only 10% of the staff see the other 10%...

    I note that its faster when a member of a group brings back only a tiny part of the data compared to the other way around... This is why Im asking before implementing another view of the same type.

    So, on my question. Is there a better way to do this, or the query should be on there head with a WHERE clause does NOT EXIST (xxxxx! = yyyyy)) or soemthing else ther is quite that Ive missed...

    The only other comment is that Im using the upper table "APPLICATION_GROUPS" for more than one type of object (i.e. entities and entity accounts banking..) y at - it no merit in separaitng the two into two tables?

    Thank you very much

    Richard

    Hi Richard,

    Miss me a lot of information but:

    I think that your design could be allowed. It seems a little too mat, maybe, but I think this could work.

    It is important to have the good indexing and use compression of index:

    On application_groups (object_name, the GroupName, groupname) compresses

    IT compresses to entity_users (username, entity_id)

    On the entity_group_members (entity_group, entity_id) compress

    I think v as v ('G_ENTITY_GRP_ID') is a user-defined function? If so, if possible, set the deterministic and uses result_cache.

    Consider partitioning application_groups type (= object_name?)

Maybe you are looking for