Complex select query

Hi again!
Now here's another question...
I need to make a request to find which rep gave the most rebates to his clients.
The tables goes as such
REP *(rep_num*, last name, first name, etc..) <--- CUSTOMER (*customer_num*, last name, first name, rep_num, etc.) <--- ORDERS *(Order_num*, date, customer_num) <---- ORDER_LINE (*Order_num*, Part_num, num_ordered, quoted_price) ----> PART (*part_num*, class, description, price, etc.)
The rebate is calculated from price-quoted_price.
Hope it is somewhat clear enough... thanks again!
Woopsie, sorry about that Tubby!
Edited by: [email protected] on 11-Feb-2009 11:04
Edited by: [email protected] on 11-Feb-2009 11:06

Hi,
In Tubby's example the WITH clause just mimicks a table called DATA. This is done instead of actually creating such table and then populate it.
Your error just says that you dont have a column called rebate (as Tubby has). If this doesn't help, then paste your exact query, along with the exact error message. And please enclose everything in {noformat}{noformat} tags to make it readable.
Regards
Peter                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • Complex select query read in Java

    I want to use a custom complex select query with join of multiple tables Explicitly to read the object which itself is associated with other object through one-to-many mapping. The objects have been generated through Toplink without Indirection.
    The scenario is like this:
    Table: Purchase_Order     Object:     PurchaseOrder
    2nd Table: Purchase_Order_Line     Object:     PurchaseOrderLine
    Relationship is one-to-many between Purchase_Order and Purchase_Order_Line. Corresponding to this, PurchaseOrder has the Collection of PurchaseOrderLine without indirection.
    I want to override the internal approach of Toplink by my custom select query to fetch a few attributes only. I tried it in some way but it does not return me PurchaseOrderLine collection.
    Please elaborate what should be the approach to run such query in Java using Toplink API?
    Thanks in advance!

    Could you include the query that you have currently tried.
    TopLink 9.0.4 did not support joining 1-m relationships, so this would not be possible, but TopLink 10.1.3 did add this support. You would need to add the 1-m joined attribute to the query to be able to use SQL that joins the 1-m relationship.
    In terms of only selecting partial objects, you would need to enable partial object reading on the query to only have some of the object's attributes built, but I believe that 1-m joins are not currently supported for partial object reading.
    You may also consider using batch reading on the 1-m in TopLink, as batch reading is more efficient at reading 1-m relationships than joining.

  • Splitting complex SQL select query in to independent sequence of small queries.

    =====================
    Problem Description
    =====================
    Consider I have the following table structures with approx no. of records in each.
    Employee Table
    emp_id:
    name :
    dept_id:
    #Approx no. of rows in the table: 1 million
    Department Table
    dept_id:
    dept_name:
    #Approx no. of rows in the table: 1 Hundred
    =====================
    Target Result: is to get the employee name who's id is '100' and the department name he belongs to.
    =====================
    =====================
    Ideal Query : Consider it as COMPLEX QUERY
    =====================
    select emp.name,dept.name
    from     employee emp, department dept
    where      
         emp.dept_id = dept.dept_id
         and
         emp.emp_id = '100';
    =====================
    Optimized Independent Queries : Consider it as SIMPLE QUERIES
    =====================
    Now i want to split the above query in to two independent queries.
    Query #1
    select emp.name
    from     employee emp
    where emp.id = '100';
    :: Returns one row as emp.id is unique.
    Query #2
    select dept.dept_id, dept.name
    from department dept
    :: Returns 100 rows as there are only 100 rows.
    =====================
    Need Your Suggestions Here
    =====================
    If you compare the COMPLEX QUERY against SIMPLE QUERIES, i prefer to use SIMPLE QUERIES, get the results
    and compare department id against two results programmatically and then get the final result. I know the database
    query engines will do similar query optimization like dividing COMPLEX QUERY to SIMPLE QUERIES and run them
    in some specific order to get results faster.
    Now is it possible to get the exact sub queries which the database query engine (ANY database query engine) generates
    out of complex select query? I mean i don't want to do the splitting manually but i still need those sub-queries.
    Can any one help me out please?

    Why on earth would you want to keep the queries separate? As Tom Kyte would say, databases are born to join!
    Providing emp_id has an index on it (and it should, assuming it's the table's primary key!) your "complex" (or, what I would call simple*), it'll most likely be faster than doing it separately. You've not got the context switches for one thing.
    If you want to see what the optimizer does with your query, you should run a trace on the query. An explain plan would give you an idea of what it's going to do, too.
    In general, doing something like this procedurally is going to almost certainly be worse than doing it in SQL (and by worse, I mean slower).
    * I'd hate to see what you'd call some of the monster queries I work with, if you think that query is complex!

  • How to write the select query with complex where condition

    Hi all,
    Can u help me in writing  following select query.
    select * from zu1cd_corr where time_stamp between firstday and lastday .
    In the above query time_stamp contains the date and time.
    where as firstday and lastday contains the dates.
    I need to compare the date in the time_stamp with the firstday and lastday.
    But time_stamp contains the time also and it is char of 14 characters length.

    Hi,
    If that is the case u can do as advait specified....
    if the firstday and secondday are select-options then declare two more variables having 14 character length and then concatenate '000000' to firstday variable and '240000' to last day variable and then write ur query.
    CLEAR : lv_firstday,
                 lv_lastday.
    concatenate firstday '000000' to lv_firstday.
    concatenate lastday '240000' to lv_lastday.
    ranges : r_Date for zu1cd_corr-time_stamp.
    r_date-sign = 'I'.
    r_date-option = 'BT'.
    r_Date-low = lv_firstday.
    r_Date-high = lv_lastday.
    append r_date.
    select * from zu1cd_corr  into table it_zu1cd_corr where time_stamp in  r_Date.
    I hope it helps.
    Regards,
    Nagaraj

  • Query in Select query operation

    Please excuse me for posting this again.
    I'm stuck up with this for 3 days. Lenghty post pls do read and help me.
    How is the select query going to be changed in the SAP so that the original database behind it understands it. I want to know the operation of IN operation in where clause where in we can use ranges.
    I'm asking this because I'm facing a problem using Native SQL.
    Query is like this:
    EXEC SQl.
    OPEN C1 for
    SELECT field1 from table@domain where field2 IN :ABAP_VAR.
    ENDEXEC.
    EXEC SQL.
    FETCH C1 NEXT INTO :VAR1 , :VAR2
    ENDEXEC.
    Oracle usually recognises this statement IN ( '2222' , '3658' , '6895' )
    But here this field that is build dynamically like this is not recognising them as seperate fileds but instead it is taking them as a single string and not selecting anything.
    But if a single value is passed it is fetching the data into the cursor. And the code is working fine.
    Any help in this is highly appreciable.

    "if a in r_range" is is effectively: does the value held in 'a' comply with the values held in range 'r_range'. Ranges can hold NE, patterns etc so this can get quite complex.
    You can see that it is very different from the SQL type IN.
    If your range hold several values you could unstrip it into a string varaible so that it ended up having a value something like: ( '2222' , '3658' , '6895' ). Not sure if you need the '(' but it would be something like this:
    eg,
    assuming your range is simple and only holds 'I' and 'EQ' values:
    data v_string type string.
    loop at r_range.
    concatenate v_string '@''' r_range-low  '''@' into v_string.
    endloop.
    REPLACE
      ALL OCCURRENCES OF '@'
      IN v_string
      WITH ' '.
    Then you can use:
    SELECT field1 from table@domain where field2 IN :v_string.

  • Performance issue with select query and for all entries.

    hi,
    i have a report to be performance tuned.
    the database table has around 20 million entries and 25 fields.
    so, the report fetches the distinct values of two fields using one select query.
    so, the first select query fetches around 150 entries from the table for 2 fields.
    then it applies some logic and eliminates some entries and makes entries around 80-90...
    and then it again applies the select query on the same table using for all entries applied on the internal table with 80-90 entries...
    in short,
    it accesses the same database table twice.
    so, i tried to get the database table in internal table and apply the logic on internal table and delete the unwanted entries.. but it gave me memory dump, and it wont take that huge amount of data into abap memory...
    is around 80-90 entries too much for using "for all entries"?
    the logic that is applied to eliminate the entries from internal table is too long, and hence cannot be converted into where clause to convert it into single select..
    i really cant find the way out...
    please help.

    chinmay kulkarni wrote:Chinmay,
    Even though you tried to ask the question with detailed explanation, unfortunately it is still not clear.
    It is perfectly fine to access the same database twice. If that is working for you, I don't think there is any need to change the logic. As Rob mentioned, 80 or 8000 records is not a problem in "for all entries" clause.
    >
    > so, i tried to get the database table in internal table and apply the logic on internal table and delete the unwanted entries.. but it gave me memory dump, and it wont take that huge amount of data into abap memory...
    >
    It is not clear what you tried to do here. Did you try to bring all 20 million records into an internal table? That will certainly cause the program to short dump with memory shortage.
    > the logic that is applied to eliminate the entries from internal table is too long, and hence cannot be converted into where clause to convert it into single select..
    >
    That is fine. Actually, it is better (performance wise) to do much of the work in ABAP than writing a complex WHERE clause that might bog down the database.

  • Select query in routing(operation,sequence)

    Hello frds,
         i am into change routing BDC actually i want a select query .....for like existing of operation for a particular sequence in routing....
    if i will found operation for particular sequence i will run the bdc code lese i will exit...any one can help me...
         just provide me a query....to check existnece of operation for a particular sequence......ASAP...

    Why on earth would you want to keep the queries separate? As Tom Kyte would say, databases are born to join!
    Providing emp_id has an index on it (and it should, assuming it's the table's primary key!) your "complex" (or, what I would call simple*), it'll most likely be faster than doing it separately. You've not got the context switches for one thing.
    If you want to see what the optimizer does with your query, you should run a trace on the query. An explain plan would give you an idea of what it's going to do, too.
    In general, doing something like this procedurally is going to almost certainly be worse than doing it in SQL (and by worse, I mean slower).
    * I'd hate to see what you'd call some of the monster queries I work with, if you think that query is complex!

  • (view hint or Query  writing method) using views in Select Query

    Need Assistance,
    Hi all,
    My database having many views. while i am joining the views in a select query increase the cost of the querys due to the complex joins in the conditions.
    While checking the select query with views individualy taking less cost.
    Does any hint or query writing method is availabe to avoid the merging of the execution plan of the views?
    I have browsed many related to the mentioned issues,couldn't found any thing that helps me.
    Please help me on this issue,it will be very much helpfull for me.
    Thanks,
    Senthur

    Please read the two threads linked to in the FAQ: {message:id=9360003} and post the relevant details they request, so that people can help you better with your issue.
    We cannot help if you just say "my query is slow what do I do to fix it" as we cannot see your queries, your data, or any other information we would need to help. If the answer was that simple, it would be built into oracle so that the optimiser would fix it for you.

  • Performance Tuning on a Select Query

    Hi,
    We have a select statement as mentioned below.
          SELECT loobj1 gpart vkont proid tdate lotyp lockr
          INTO TABLE tb_dfkklocks
             FROM dfkklocks
             FOR ALL ENTRIES IN tb_fkkvkp
             WHERE
            lotyp  = '02'
             AND   proid  = '01'
             AND   lockr >= '0'
             AND   lockr <= '9'
             AND   gpart = tb_fkkvkp-gpart
             AND   vkont = tb_fkkvkp-vkont.
    We have Primary key as shown below.
    CLIENT
    LOOBJ1
    LOTYP
    PROID
    LOCKR
    FDATE
    TDATE
    GPART
    VKONT
    We have a secondary index only on TDATE. What can be the ideal select stement to improve the performance?
    Regards,
    Lijo Joseph

    Hi Lijo,
    Couple of pointers from my end.
    1). Try avoiding a FOR ALL ENTRIES if the base table is huge. Instead use a array fetch and pick up more data instead of trying to restrict the data using a FOR ALL ENTRIES as this does a LOOP on the database while selecting the data.
    So i would suggest not use FOR ALL ENTRIES if the base table is huge. So analyse before using a select query.
    2). It is actually very difficult to mention which option is better. I would say that it all depends on the scenario. What amount of data is to be read, from which table it is being read, what kind of processing is required on database,..... and many more analysis goes to write a an efficient select statement.
    3). If i am not wrong i think if you are using a FOR ALL ENTRIES and dont specify the key completely then there is no gaurentee that you fetch all the unique records. The system will compare all the character fields as a KEY field and it will not fetch all the records. So while using a FOR ALL ENTRIES its recommended to use all the key fields.
    4). If you would like to really spend time and work on a efficient select statement then try using the tcode ST05 --> Select the button EXPLAIN SQL REQUEST.
    type in your select query and see what is the cost of the select statement fired on the database. This will help you to select the best statement. This is a very useful tool if we have complex select statements.
    Hope this info is helpful.
    Cheers
    VJ
    Message was edited by: Vijayendra  Rao

  • Expression too complex in query expression

    Page code creates a string of all ManufacturerPartNumbers
    that share a common ProductFamilyID based on a
    ManufacturerPartNumber form selection. This string is then included
    in the recordset query.
    The following error message only occurs when the
    ManufacturerPartNumber string has numerous listings. When the
    string contains only 3 listings of ManufacturerPartNumbers, this
    error message does not appear.
    [Microsoft][ODBC Microsoft Access Driver] Expression too
    complex in query expression 'LATITUDE &amp;lt;=
    40.8210861895984 AND latitude &amp;gt;= 40.3865898104016 AND
    longitude &amp;gt;= -74.2737786508004 AND longitude
    &amp;lt;= -73.7014913491996 AND
    (((((((((((((((((((((((((((((ManufacturerPartNumber = '3408H')) OR
    ((ManufacturerPartNumber = '4608H'))) OR ((Manufacture'.
    /customer_locator/testprocessform2.asp, line 233
    Is there a size limit to a recordset query that is causing
    this error or is there a possible syntax issue with my query
    code?

    That doesn't sound like an error that would normally occur on a lot of pages... Do you have AdBlock (Plus), or any Greasemonkey userscripts, or any similar add-ons that customize sites? I wonder whether one of those might have some faulty logic.

  • Select Query Problem

    Hi Experts,
    I am having a select query in which I am using a variable in the where condition but it is giving error. Please suggest how to use variable in the select query.
    The query I am using is a s below.
    select * from zexc_rec into table it_ZEXC_REC
          where
           LIFNR in S_LIFNR and
          DOCNO in S_DOCNO and
          DOCTYP in doc_typ and
          DATE1 in S_DATE1 and
          MATNR in S_MATNR.
    Here doc_typ is a variable.
    Thanks.

    Rahul,
    use RANGES type variable instead of variable . It acts as a select-options variable. Thn use this variable in SELECT query with IN.
    Eg :
    RANGES r_t510 FOR t510-lgart.
        r_t510-low = '1600'. 
        APPEND r_t510.
        r_t510-low = '3190'. 
        APPEND r_t510.
    Note  : can be use SIGN, OPTIONS properties too in RANGES type.
    More deatils go through on HELP of RANGES
    Rgds,
    Ranjith

  • Oracle 11g :SELECT query blocked..??

    Hi Experts,
    could you please explain why the below SQL query is blocked?
    SELECT 1 FROM DUAL is blocking the SQL statement on GTTAPPUSR@gttccuatcriba04 ( SID=469 ) blocked SQL -> DELETE FROM GTTDB.PURCHASE_ENTRY_ID=:1
    SELECT 1 FROM DUAL is blocking the SQL statement on GTTAPPUSR@gttccuatcriba04 ( SID=367 ) blocked SQL -> DELETE FROM GTTDB.PURCHASE_ENTRY_ID=:1
    I am scratching my head without any solution when I had a look at the db today. Thanks in advance for your help.
    Regards,
    Boris
    Edited by: user12075620 on Dec 4, 2012 8:58 AM

    The SELECT statement is not blocking the UPDATE. As I said in the previous reply, the string that this query produces does not match the logic.
    This query is (at least on the surface) correctly identifying that session 1 is blocking session 2. Session 1 holds some lock that session 2 is waiting on. So far, so good. Since session 2 is waiting on the lock, we can easily enough see what session 2 is running (the UPDATE statement). But since session 1 is not blocked, it is potentially off running a ton of other SQL statements (or no SQL statement at all). The query is looking to see what session 1 is running currently. It has no way of determining what session 1 ran at some point in the past to acquire the lock in the first place.
    Going back to my KING example,
    At noon, session 1 runs
    UPDATE emp
       SET sal = sal * 2
    WHERE ename = 'KING'Session 1 now has a lock on the KING row in the EMP table. But session 1 neither commits nor rolls back, it is still in a transaction. Session 1 might not have any more activity for a long time-- the user might go off to lunch, for example (obviously, applications should not be designed to allow users to maintain open transactions indefinitely, but not all applications are designed correctly). Or it might start running other queries. Let's say that session 1 now runs a query that is going to go for an hour
    SELECT *
      FROM giant_view_with_lots_of_computationsNow, at 12:45, session 2 comes in and runs
    UPDATE emp
       SET bonus = 100
    WHERE ename = 'KING'Session 2 is blocked. Session 2 is running the UPDATE statement. Session 1 still holds the lock but it is running some completely unrelated SQL statement.
    If we run the query you posted, the query will correctly report that session 1 is running the query against the GIANT_VIEW_WITH_LOTS_OF_COMPUTATIONS but incorrectly imply that this SELECT query is the source of the lock. It is not. It simply happens to be the query that the session that does hold the lock happens to be executing at the current moment (why the application seems to be running a lot of queries that select a constant from dual is a separate question).
    Justin

  • Oracle SQL Select query takes long time than expected.

    Hi,
    I am facing a problem in SQL select query statement. There is a long time taken in select query from the Database.
    The query is as follows.
    select /*+rule */ f1.id,f1.fdn,p1.attr_name,p1.attr_value from fdnmappingtable f1,parametertable p1 where p1.id = f1.id and ((f1.object_type ='ne_sub_type.780' )) and ( (f1.id in(select id from fdnmappingtable where fdn like '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#%')))order by f1.id asc
    This query is taking more than 4 seconds to get the results in a system where the DB is running for more than 1 month.
    The same query is taking very few milliseconds (50-100ms) in a system where the DB is freshly installed and the data in the tables are same in both the systems.
    Kindly advice what is going wrong??
    Regards,
    Purushotham

    SQL> @/alcatel/omc1/data/query.sql
    2 ;
    9 rows selected.
    Execution Plan
    Plan hash value: 3745571015
    | Id | Operation | Name |
    | 0 | SELECT STATEMENT | |
    | 1 | SORT ORDER BY | |
    | 2 | NESTED LOOPS | |
    | 3 | NESTED LOOPS | |
    | 4 | TABLE ACCESS FULL | PARAMETERTABLE |
    |* 5 | TABLE ACCESS BY INDEX ROWID| FDNMAPPINGTABLE |
    |* 6 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
    |* 7 | TABLE ACCESS BY INDEX ROWID | FDNMAPPINGTABLE |
    |* 8 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
    Predicate Information (identified by operation id):
    5 - filter("F1"."OBJECT_TYPE"='ne_sub_type.780')
    6 - access("P1"."ID"="F1"."ID")
    7 - filter("FDN" LIKE '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#
    8 - access("F1"."ID"="ID")
    Note
    - rule based optimizer used (consider using cbo)
    Statistics
    0 recursive calls
    0 db block gets
    0 consistent gets
    0 physical reads
    0 redo size
    0 bytes sent via SQL*Net to client
    0 bytes received via SQL*Net from client
    0 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    9 rows processed
    SQL>

  • Select query differences between oracle 9 and oracle 8.

    Hi,
    I have a problem using the select query between oracle 7 and oracle 9i I don't have the same result :
    ex:
    With oracle7
    SQL> select 'champ1','champ2' from DUAL;
    'CHAMP 'CHAMP
    champ1 champ2
    With Oracle 9
    SQL> select 'champ1','champ2' from DUAL;
    'CHAMP1' 'CHAMP2'
    champ1 champ2
    So Can someone tell me how to solve this problem ? Is there a parameter in oracle 9 to set?
    Thanx.

    Whenever you are posting anything over internet forums like this - you should be very careful about not just posting the details which requires to solve the problem - also should be sensible about your question.
    This is not at all desired when you are posting such question. It may be because - you may not well aware of the fact.
    My suggestion is -> First Go through the basics Of SQL in general.
    Then go for any specific product like Oracle/ SQL Server/ Sybase etc.
    And, finally learn the advanced commands of that DB.
    You asked it - may be you thought the difference in output in terms of lines. But, that is not your actual output. That is the graphical display part only.
    Anyway,
    You can get the quite familier output by first type the following command ->
    set lin 310Regards.
    Satyaki De.

  • Select Query resulting in Scientific Notation

    Hello all,
    I am running a Select query through a batch file that extracts data from an Oracle database. Several of the fields that I am extracting from contain numbers that are up to 38 digits long. When I extract the data, it converts the numbers into scientific notation and it is important for me to have the entire field. Is there something I can change to my query that will pull the data in its entire form? This is what I'm running now:
    select * FROM ML.APPT where APPTDATE >= to_date('01/1/2010','mm/dd/yyyy'
    I apologize in advance if this has been answered already.
    Thanks!

    >
    When the extractor finishes, it returns the data into a flat file.
    don't quite understand the TO_CHAR function. Does this function mean I need to say something like this: select "TO_CHAR('column name', 99999999999999999999999999999999999999" FROM ML.APPT where APPTDATE >= to_date('01/1/2010','mm/dd/yyyy')
    >
    Yes- if the tool you use to extract the data (your 'extractor') is converting the numeric data to a string then it is responsible for creating the string in the proper format. If the number is an integer that can have as many digits as you have '9's in your sample format string then that is what you need to do.
    Here is how sql*plus (Oracle's tool) will display the data using default settings
    SQL> select 12345678901234567890123456789012345678 no_format,
      2  to_char(12345678901234567890123456789012345678, '99999999999999999999999999
    999999999999') with_format
      3   from dual;
    NO_FORMAT WITH_FORMAT
    1.2346E+37  12345678901234567890123456789012345678
    SQL>
    ----- TOAD will display something similiar but the default uses more decimal digits in the scientific notation data
    NO_FORMAT,WITH_FORMAT
    1.23456789012346E37, 12345678901234567890123456789012345678You can either format the numeric data in the query using TO_CHAR or the 'extractor' can do it when it converts the data to a string.

Maybe you are looking for