Hibernate 2 SQL  query question

thanks, I use hibernate 2.1
Now I want to do the same thing, but using SQL
THE_QUERY_STRING="UPDATE CUSTOMER_SOMETHING customerSomething set customerSomething.customer = (select customer.objectId FROM CUSTOMER customer where customer.customerId customerSomething.customerId) where customerSomething.userTask = 15608"
With current THE_QUERY_STRING
two parameters are missing...
session.createSQLQuery(UPDATE_CUSTOMER_SQL, , );
What should I put here?

Hi,
Use LAST to find the location_code related to the latest start_date, like this:
SELECT       employee_id
,       MAX (start_date)     AS max_start_date
FROM       position
GROUP BY  employee_id
HAVING       MIN (location_code) KEEP (DENSE_RANK LAST ORDER BY start_date)     = 25
;MIN (location_code) specifies what to do in case of a tie. In your case, ties are impossible, since (employee_id, start_date) is the primary key, so it doesn't matter if you say MIN or MAX (or, if you want to be cute, AVG or SUM) in the HAVING clause: you'll get the same location_code either way.
I hope this answers your question.
If not, post a little sample data (INSERT statements to go with the CREATE TABLE statement you've already posted), and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
See the forum FAQ {message:id=9360002}

Similar Messages

  • Generated SQL query question

    In my OBIEE development environment, a report I create in Answers generates the following SQL query to the database:
    select T29323.LOCATION as c1,
    T29323.TOOL_NAME as c2,
    T29323.JOB_NUMBER as c3,
    from
    EDB.EDB_JOB_NUMBER_PARAMETER_T_V T29323
    where
    ( T29323.JOB_NUMBER = 1319511
    and T29323.CREATION_DATE <= TO_DATE('2008-05-21 08:38:18' , 'YYYY-MM-DD HH24:MI:SS')
    and (T29323.LOCATION in ('MOO', 'RR'))
    and T29323.CREATION_DATE >= TO_DATE('2008-05-18 08:38:18' , 'YYYY-MM-DD HH24:MI:SS'))
    However, in my OBIEE Production environment the same report generates the following SQL query to the database;
    select D1.c12 as c1,
    D1.c11 as c2,
    D1.c10 as c3
    from
    (select T29323.JOB_NUMBER as c10,
    T29323.TOOL_NAME as c11,
    T29323.LOCATION as c12
    from
    EDB.EDB_JOB_NUMBER_PARAMETER_T_V T29323
    where ( T29323.JOB_NUMBER = 1319511
    and T29323.CREATION_DATE <= TO_DATE('2008-05-21 09:19:03' , 'YYYY-MM-DD HH24:MI:SS')
    and (T29323.LOCATION in ('MOO', 'RR'))
    and T29323.CREATION_DATE >= TO_DATE('2008-05-18 09:19:03' , 'YYYY-MM-DD HH24:MI:SS'))
    ) D1
    Any ideas why the queries are different? I have looked through my config files to see if a setting is different, but all are the same.
    I am running OBIEE 10.1.3.3.2
    Any suggestions would be appreciated.
    Thanks,
    Kevin

    Hi Kevin,
    I have the same problem with generated SQL querys, I get different result with the same repository, same query in Answers, same OS... in several environments.
    The BI vesion is 10.1.3.4
    Any idea about the question?
    Regards
    Guillermo

  • Database - Show SQL Query question..

    Greetings:
        I am helping a co-worker with Crystal. I don't use crystal but I am able to explain the problem. My co worker is currently using crystal reports 8.5. If he pulls a query and the report is generated he can then click on Database menu then click on Show SQL Query. From there he is able to edit, add, remove fields. Now we want to place him on Crystal V10. When he does the same task, instead of being able to edit the Show SQL query he can only view it. Is there a way around this so that he can Edit the sql query in version 10 or did that feature go away in versions 9 and above. Also we are going to Purchase Crystal 2008, is this feature available in 08? Any help would be appreicated..

    Hi there!
    I have the same problem as described above.  I have been asked to figure out what is wrong with one of the reports that used to work when we used MAS 4.05.  We are now on MAS 4.20 with Crystal Reports v.10 and the report fails to run.  I loaded the report into Crystal to see if I could see the problem and I discovered that the report is still trying to access a table from the old MAS database.  The SQL code as shown in the Show SQL Query window is as follows:
    SELECT "AR_InvoiceHistoryHeader"."InvoiceNo", "AR_InvoiceHistoryDetail"."ItemCodeDesc", "AR_InvoiceHistoryDetail"."ItemCode", "AR_InvoiceHistoryDetail"."ExtensionAmt"
    FROM   "AR_InvoiceHistoryHeader" "AR_InvoiceHistoryHeader", "AR_InvoiceHistoryDetail" "AR_InvoiceHistoryDetail"   
    WHERE  "AR_InvoiceHistoryHeader"."InvoiceNo"="AR_InvoiceHistoryDetail"."InvoiceNo" AND "AR_InvoiceHistoryHeader"."HeaderSeqNo"="AR_InvoiceHistoryDetail"."HeaderSeqNo" AND "AR_InvoiceHistoryHeader"."CommissionAmt"="AR_InvoiceHistoryDetail"."CommissionAmt" AND      ARO_InvHistoryDetail."SOItemNumber" <> 'COMMENT' AND      ARN_InvHistoryHeader."InvoiceDate" = {d '2007-09-21'}  
    ORDER BY "AR_InvoiceHistoryHeader"."InvoiceNo"
    The problem is that ARN_InvHistoryHeader needs to be AR_InvoiceHistoryHeader and SOItemNumber needs to be ItemCode.
    When I go into the Database Expert, I can only see the tables in the FROM clause.  I don't know how to modify the WHERE clause.  It kind of looks like this report was only partially converted to the new version.  Is there a way to changed these attributes without rebuilding the whole report?
    By the way, I apologize for not setting my SQL off in a code box, but I tried that and it caused the page to be very wide with no horizontal scroll bar.
    Edited by: Jason Schill on Oct 27, 2008 11:30 PM

  • Dynamic sql query question

    hi all,
    i created a report that displays results based on this query initially.
    select *
    from EMP
    where 1=1;
    i'm using a SQL Query (PL/SQL Function Body Returning SQL Query).
    i have search items in my page like empno, lastname, firstname, middle, email_address etc... when i place a value for lastname then in my query it should add AND LASTNAME = :P1_LASTNAME.
    so the query returned should now have
    select *
    from EMP
    where 1=1
    and LASTNAME = :P1_LASTNAME;
    is this possible?
    i was thinking something like looping for all search items in the page (ie. text, popup lov, datetimepicker) then check whether the item is null or not... when not null then probably get the substring of the item name like (ie. substr(:P1_LASTNAME) and get only LASTNAME then add "=" then concat it with the item name. not sure if this is possible though.
    thanks
    allen

    hi denes,
    thanks for the sample... i noticed that the sample hard codes additional conditions to be added to the sql query which is okay when the search criteria is less than 5-10... but i'm thinking if the search criteria will be like 15-30 fields or make all fields searchable, this might be uncomfortable.
    what i was thinking is like loop thru all the items i.e.
    declare
    sql varchar2(4000) := 'select * from emp where 1=1';
    begin
    for i in (select * from all_items_in_apex) -- this is just an example.
    loop
    if i.value is not null then
    sql := sql || ' and ' || i.column || '=' || i.value;
    end if;
    end loop;
    end;
    the only thing is that i don't know if there's a possible replacement for the clause (select * from all_items_in_apex).
    sorry if i'm asking too much but this is how i normally do it in forms. the suggestion in the link you provided should be sufficient but i was just wondering if this idea is also possible.
    thanks again.
    allen

  • Sql query question - been trying for two days

    Hi guys im trying to carry out an sql query
    Im using a left join to do a query, which gives me a set of results:
    select COURSESTUDENT.StudentNo, COURSESTUDENT.CourseCode, COURSESTUDENT.Year, MARKS.ExamMark, MARKS.EntryNo FROM COURSESTUDENT LEFT JOIN MARKS ON COURSESTUDENT.StudentNo=MARKS.StudentNo AND COURSESTUDENT.CourseCode=MARKS.CourseCode AND COURSESTUDENT.Year=MARKS.Year
    but I would like to do a select on this result but do not want to use a create view as if more than one person access this page at a time then if the servlet tries to create the view an error will occur.
    I would like to do the following select statement on the results of the query above..
    select * from (above) where CourseCode='ELE304' AND Year=1999;
    Is this possible, im using postgres..
    Please help......
    thanks
    tzaf

    Sorry, I have never used postgres, but in several databases, the following syntax would work and provide the correct ResultSet.
    select *
    from (
         SELECT
             COURSESTUDENT.StudentNo,
             COURSESTUDENT.CourseCode,
             COURSESTUDENT.Year,
             MARKS.ExamMark,
             MARKS.EntryNo
         FROM
             COURSESTUDENT LEFT JOIN MARKS ON
             COURSESTUDENT.StudentNo=MARKS.StudentNo AND
             COURSESTUDENT.CourseCode=MARKS.CourseCode AND 
             COURSESTUDENT.Year=MARKS.Year
         ) A
    WHERE
        CourseCode='ELE304' AND
        Year=1999

  • A simple SQL query question

    I have an interesting problem and wondering how I can get this result in a single SQL query:
    Here is table emp has data with a row for every year since the employee joined with the salary paid that particular year with following columns:
    emp (
    id varchar2(10),
    name varchar2(25),
    interested_year date,
    salarypaid number(10)
    I would like to print the results as follows:
    id name previousyear_salarypaid currentyear_salarypaid
    x xxxxx xxxxxx xxxxx
    Is this possible to do? I have tried to simplify my actual problem so I hope I have included all necessary details.

    Just to clarify, the columns mentioned in the results are
    previousyear_salarypaid is nothing but
    salarypaid where interested_year = '2007'
    currentyear_salarypaid is nothing but
    salarypaid where interested_year = '2006'

  • SQL query question for SQL Swing Applet

    Hi
    I am building a little SQL Swing applet.
    I was wondering if there is a SQL query statement to
    find:
    * The names of all the tables in the Database loaded.
    * The number of tables in a database loaded.
    Thank you very much in advance, A.

    Not positive, but check out Connection.getMetaData(). It returns a DatabaseMetaData instance that has a number of get... methods that may provide the info you need.

  • SQL-Query-Question: get all ID2 which have specific ID1

    Hello,
    I have a table Tab1 with millions of entries like
    ID1    ID2
    1       aa
    4       aa
    1       bb
    4       cc
    I'm looking for a sql-query which gives me all ID2-values which have a all of the supplied ID1, in the example above if I query for 1 4 then I want aa and not bb or cc because bb has only 1 and cc only 4.
    The values of ID1 could be a lot like (1,2,3,5,7,8,9,10,34,4,67,33,53,43...).
    Greetings
    Stefan

    just this
    SELECT ID1,ID2
    FROM Table t
    WHERE EXISTS (SELECT 1
    FROM Table
    AND ID2 = t.ID2
    HAVING COUNT(DISTINCT CASE WHEN ID1 IN (1,4) THEN ID1 ELSE NULL END) = 2
    If you want you can pass values 1,4 through a parameter to make it generic like below           
    DECLARE @ValueList varchar(10)
    SET @ValueList = '1,4'
    SELECT ID1,ID2
    FROM Table t
    WHERE EXISTS (SELECT 1
    FROM Table
    WHERE ID2 = t.ID2
    HAVING COUNT(DISTINCT CASE WHEN ',' + @ValueList + ',' LIKE '%,' + CAST(ID1 AS varchar(5)) + ',%' THEN ID1 END) = LEN(@ValueList) - LEN(REPLACE(@ValueList,',','')) + 1
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • SQL Query Question

    Hi,
    I am trying to filter my output from the query based on some conditions but not able to figure out how. May be I am just overlooking at the issue or is it something tricky.
    So, I have a query returning 4 rows of output out of which I need to filter the rows. I have created a table from the result of the query that I need to filter to make it simple. So below is my create table script and values that are obtained from my original query.
    CREATE TABLE TEMPACCT
      SOURCEKEY           NUMBER,
      FLAG                VARCHAR2(1),
      ITEMID              NUMBER(9)                 ,
      ITEMNAME            VARCHAR2(10)               ,
      ITEMKEY             NUMBER(9)                
    Insert into tempacct values (0, 'N', 100, 'ITEM1' , 9647);
    Insert into tempacct values (0, 'N', 200, 'ITEM2' , 9648);
    Insert into tempacct values (9648, 'N', 100, 'ITEM3' , 9813);
    Insert into tempacct values (9647, 'Y', 100, 'ITEM4' , 9812);
    SQL> select * from tempacct;
    SOURCEKEY F     ITEMID ITEMNAME      ITEMKEY
             0 N        100 ITEM1            9647
             0 N        200 ITEM2            9648
          9648 N        100 ITEM3            9813
          9647 Y        100 ITEM4            9812
    SQL> Tempacct table is the table created from the resultset of my original query.
    So from the above output, what I need is 3 rows. The logic to filter out the row is - If any of the row thathas sourcekey that is same as Itemkey in any of the 4 rows and flag is Y then remove the row which have flag =N and only display the one with Falg = Y.
    Ok, so, in this case the desired output would be
    SOURCEKEY F     ITEMID ITEMNAME      ITEMKEY
             0 N        200 ITEM2            9648
          9648 N        100 ITEM3            9813
          9647 Y        100 ITEM4            9812So here we compared between the first row and the fourth row, and since the sourcekey in fourth row is same as itemkey in first row and Flag is 'Y' for fourth row, we keep 4th row and remove the first row since the flag is 'N'. (and sourcekey is 0. the row that gets removed will always have sourcekey =0) .
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - ProductionAppreciate your help.

    Hi,
    ARIZ wrote:
    Although the original question is already been answered, I had another small modification to the same question and also seeking some clarification. I do not want to open a new thread just for a similar question.I think you'll get better replies faster if you do start a new thread.
    Not counting this one, there have been 13 replies to this thread. Not many people who havn't already been participating in this thread are going to start reading a thread with 13 replies. Those who do are going to waste a lot of time reading about issues that have already been resolved, and the are likely to understand the remaining issues incorrectly.
    I have been following the thread from the beginnning, and I'm starting to get confused about what the unresolved issues are.
    I believe there are two things you still need:
    (1) An explanation of the solution I posted yesterday, involving the analytic COUNT function.
    (2) A solution for a new problem involving the same tables
    If I got that wrong, start a new thread, asking just what you need to know. Copy any relevant parts (like the CREATE TABLE and INSERT statements) from this thread. You can include a link to this thread, but do your best to make sure people don't have to use it.
    I realize that's more work for you, but getting the best results, and getting them quickly, sometimes does require more work.
    <h2>(1) An explanation of the solution I posted yesterday, involving the analytic COUNT function.</h2>
    ARIZ wrote:
    Hi Frank,
    Just out of curiosity, I was trying to understand the Count analytical function that you have used in the solution.
                    COUNT ( CASE
                                 WHEN  ac.flag = 'Y'
                           THEN  1
                             END
                        ) OVER ( PARTITION BY  CASE
                                                   WHEN  sourcekey = 0
                                       THEN  acctkey
                                       ELSE  sourcekey
                                               END
                                  )     AS y_cntSo what I am thinking is, this would first partition the row with acctkey ( where sourcekey =0) and sourcekey and then within that partition, it will check whether ac.flag = Y or not, if it is 'Y' then it would return count as 1 else 0. Am I correct? In the mean time I am also reading the tutorials on Count() analytical query. I'm not sure I understand your explanation.
    This is not partitioning first by x, and then by y. There is only one expression in the PARTITION BY clause. Most often, a PARTITION BY clause refers to some column in the table, for example:
    SELECT  ename
    ,       job
    ,       sal
    ,       AVG (sal) OVER (PARTITION BY job)  AS avg_sal_for_job
    FROM    scott.emp;This divides the result set into mutually exclusive parts; there will be as many such parts as there are distinct values for the PARTITION BY column. In the simple query above, if there happen to be 5 different values for job, you will get 5 independent averages.
    In your problem, there is no one column that defines a partition. That is, these two rows belong to the same partition:
    . SOURCEKEY F   ACCTKEY
             0 N       9647
          9647 Y       9812even though none of the 3 columns are the same. We could create a view that had a single column, telling to which partition each row belonged, like this:
    . SOURCEKEY F   ACCTKEY PART_NUM
             0 N       9648     9648
             0 N       9647         9647
          9648 N       9813         9648
          9647 Y       9812         9647where part_num is the result of a CASE expression:
    CASE
        WHEN  sourcekey = 0
        THEN  acctkey
        ELSE  sourcekey
    ENDWe could then use that new column, part_num, in a (very simple) PARTITION BY clause. But there is no need to create a view, even an in-line view, for that: we can (and I did) use the CASE expression directly in a (not so simple) PARTITION BY clause.
    Why did I use COUNT? The important thing about each partition is whether or not it includes any rows with flag='Y'. I don;t know of any function that directly answers that question. There are lots of ways to get the correct answer, but I think the one that corresponds most closely to the question we really want to ask:
    "Do any rows have flag='Y'?" is
    "How many rows have flag='Y'?"
    The analytic function COUNT (x) returns a number (possibly 0) of rows in the partition where x is not NULL. So, as the argument to COUNT, I used
    CASE
        WHEN  ac.flag = 'Y'
        THEN  1
    --  ELSE  NULL          -- I did not explicitly say this, but it is the default
    ENDwhich returns either
    (a) the literal number 1 or
    (b) NULL
    Instead of the literanl number 1, I could have used any literal or expression, of any data type, that is not NULL). all that matters is we produce something non-NULL for COUNT to count.
    <h2>(2) A solution for a new problem involving the same tables</h2>
    Also, I was trying to modify this query to fit my other similar requirement where I would need following output
    Original output:
    SOURCEKEY F    ACCTKEY
    0 N       9648
    0   N      9647
    9648 N       9813
    9647 Y       9812
    So, the query should be smart enough to return only the last two rows where sourcekey >0 which is
    SOURCEKEY F    ACCTKEY
    9648 N       9813
    9647 Y       9812
    And In case there are only first two 2 rows in the table then , it should return only those two row and not check for sourcekey > 0 which would be .
    SOURCEKEY F    ACCTKEY
    0 N       9648
    0   N      9647 Is it something that I should be using analytical function to solve this requirement. I am trying to accomplish this new requirement.If I understand this problem correctly, it does indeed involve mutually exclusive divisions, but in this problem, the divisions correspond more closely to a single column in the table. We want to divide the table into two mutually exclusive groups:
    (A) rows where soucekey > 0, and
    (B) rows where sourcekey = 0
    We could do that with a CASE expression, but there happens to be a built-in function that works very nicely.
    SIGN (sourcekey) returns
    (A) 1 if sourcekey > 0, and
    (B) 0 if sourcekey = 0
    But what do we want to do with those divisions? We want to display rows only from the "best" of those divisions, where division (A) is coinsidered "better" than division (B). That is, if there are any rows in division (A), then we want to display only rows in division (A), but if there are no rows in division (A), then (and only then) we want to display rows in divison (B).
    This is an example of a Top-N Query , where we want to display N items from the top of an ordered list. A typical top-N query uses an analytic function (either ROW_NUMBER, RANK or DENSE_RANK, depending on how we want to handle ties) to assign numbers to each row (lower numbers for the "better" rows), and then uses "WHERE f <= n" to display only the n "best" ones. (A special case, though a very common one, is where N=1, that is, we're only interested in the row (or rows, if there happens to be a tie) with the "best" value. In this case, most people find it cleare to say "WHERE f = 1" ratehr than "WHERE f <= 1". Your problem is an exmple ot that special case.)
    SELECT  sourcekey
    ,     flag
    ,     acctkey
    FROM     (
             SELECT  ac.sourcekey
             ,         NVL (ac.flag, 'N')     AS flag
             ,         ac.acctkey
             ,         DENSE_RANK () OVER (ORDER BY  SIGN (sourcekey)     DESC)     AS division_num
                FROM    itemtable     i
             ,         finance     f
             ,         acct     ac
               WHERE   i.itemtableid1      = f.parentid1
               AND         i.itemtableid2      = f.parentid2
             AND         f.financekey      = ac.financekey
               AND         i.parenttableid      = 19063
    WHERE     division_num     = 1
    ;Notice I talked about "mutually exclusiive *divisions* " above, not "mutually exclusive *partitions* ".
    There is no PARTITION BY in the analytic clause above. PARTITION BY means we want a separate, independent caluclation for each partition. Here, we want one single numbering for the entire result set.
    We want all rows that tie for the "best" to be numbered 1, so we have to use DENSE_RANK (or RANK) rather than ROW_NUMBER.

  • SQL Query question : how to add data to two tables connected by third Junction-table.

    Hello specialists.
    Maybe this is the wrong formum but I've got a question for which you probably have the answer, i hope.
    Situation
    John is member of Group_A and Group_B
    Bill is member of Group_B and Group_C
    Allison is member of Group_A and Group_E
    How can I create a query to input Allisons username into table 1 and groupmembership into table 2. Also updating the relationship within junction-table3 must be done automaticaly. I want to avoid duplicate records.
    The final situation I want is given in red text.
    The relationships between the tables are as follows
    Table1 (PK)ID-Userinfo    [ONE]    <------------> [MANY] Table3 ID-Userinfo
    Table3 (PK)ID-GroupInfo  [MANY] <------------> [ONE]   Table2 (PK)ID-GroupInfo
    Table1: UserInfo
    (PK)ID-Userinfo   UserName
    1                        John
    2                        Bill
    3                        Allison
    Table2: GroupInfo
    (PK)ID-GroupInfo  GroupName
    1                          Group_A
    2                          Group_B
    3                          Group_C
    4                          Group_E
    Table3: MemberOf
    (PK)ID-MemberOf  ID-UserInfo  ID-GroupInfo
    1                           1                 1
    2                           1                 2
    3                           2                 2
    4                           2                 3
    5                           3                 1
    6                           3                 4
    I hope you can help me cracking this nut.
    Thx in advance. Greetings Fred

    Hi RSingh
    May i comment something very small?
    Anyone who writes a response, done it on the assumption that he is writing the answer. Writing response is the statement of "I think that's correct". Each one that write a response see it as the answer. It makes no sense immediately mark your own
    answer too (double actions). Marking a response as "Propose as answer" is explicitly designed to
    allow others to announce that they think this is the best right answer in the thread, after the discussion :-)
    In my personal opinion, we should not mark our own answers. There are enough users with permissions, which would be glad to mark your answers as correct / recommended.
    Give us the opportunity, I will love to mark your answers :-). Moreover, by marking "Proposed as answer" you actually close the thread, other users which see that the question got an answer, may not check
    the thread at all, and maybe they could provide a different answer.
    * Exceptions are very specific cases. For example a case where the message has already become very old and no one marked anything. in this case marking "Proposed as answer" can help others and especially the person who ask the question, to focus
    on thread and the answer (more on the subject is in the Moderators forum and the Moderators Guide by the way).
    [Personal Site] [Blog] [Facebook]

  • Campaign SQL Query Question

    I have a table that has at least 40 values that I need to use as variables in CCA. My questions is. If I write query as follows;
    SELECT VALUE1 AS $Var1, VALUE2 AS $Var2 , ETC.... I would have to do this for 40 fields. Would it work if I simply do a query where I
    SELECT * FROM DBNAME WHERE CALLER = '$ANI'
    and then just use the column names as variables?

    If you need all 40 variables in your campaign to act on and use in logic, then you need to list out and assign all 40 like your first example.

  • SQL Query Question T-SQL

    Hi  
    I am trying to figure out a SQL statement where I need to select all items if one meet a condition.  In the example below I wan the query to return all 3 Red rows because one of the rows met condition = Bad.
    Any help or pointer would be appreciated.
    Thanks
    Red Good
    Red Bad
    Red Good
    Green Good
    Green Good
    Green Good
    Blue Good
    Blue Good
    Blue Good

    Hi
    Thank you for putting in t-SQL format.
    One thing I struggling with:
    If one of the Red is Bad, them make all 3 Reds Bad.
    example
    if one of the RED is bad:
    Red Good
    Red Bad
    Red Good
    then Update the three rows to this
    Red
    Bad
    Red Bad
    Red
    Bad
    I would appreciate any help on this.  I tried converting one of the options you provided to update
    select
    Col1,Col2
    from
    test t Where
    exists
    (select
    1
    from
    test Where
    t.col1=col1
    and
    col2='Bad')
    But the query keep returning syntax error
    thanks

  • Sql query questions

    Ok I have these sql statements that I was to combine to make one sql statement can someone please help me. I tried using the nvl funtion but I was un successful with that. The whole "is null" throws me b/c nothing can ever be equal to null so I am not sure how to combine the statement b/c the operator changes depending on the type of query I am making. Here are my statements any help would be appreciated!
    federal
    select * from tableName where
    UPPER(country) = UPPER('usa')
    and UPPER(state) IS NULL
    and UPPER(county) IS NULL
    and UPPER(city) IS NULL
    State
    select * from tableName where state = 'MO'
    and UPPER(county) IS NULL
    and UPPER(city) IS NULL
    county
    select * from tableName where state = 'MO'
    and UPPER(county) = UPPER('boone')
    and UPPER(city) IS NULL
    city
    select * from tableName where state = 'MO'
    and UPPER(county) = UPPER('boone')
    and UPPER(city) = UPPER('columbia')
    TIA
    Doopsterus

    First of all, you're welcome.
    Second of all, I don't think doing it in Java code is a better approach, especially since you are not going to be able to get the results you want in either case if you can't clearly code it. Now don't take offense at that ... I am not insunuating that you don't know what you want! ... rather that SQL is an excellant way to both SEE clearly what you want because it forces you to code it succinctly, and to get results back right away without compiling and weeding through code logic.
    Thirdly, each of these would appear to give a different result set.
    Forthly, What I meant by not using upper when you don't need it ...
    1) for code like this: where upper(country) = upper('USA')
    ... well you are hard coding 'USA', so you have control, it's not a variable. 'USA' can't get any more uppercase than that!
    2) for: where upper(state) = null
    ... well again, state is null or it is not null; converting state to uppercase is not going to change that. In both of these cases this is needless process slowdown.

  • PL/SQL Query Question

    I am using Oracle 11g. Below is an example of what I am trying to achieve. I want to only see records where the event type of 55 occurred before the event type of 3100101. In the example below the only records that should return are ID 1 because it is the only one where the Event type of 55 occurred before the event type of 3100101. Any help would be appreciated. Thank you.
    Create table testtable(
    ID number,
    event_type number,
    event_time date,
    ID
    insert into testtable(ID,event_type,event_time)
    Values (1,55,to_date('19960925','YYYYMMDD'));
    insert into testtable(ID,event_type,event_time)
    Values (1,3100101,to_date('19960926','YYYYMMDD'));
    insert into testtable(ID,event_type,event_time)
    Values (2,55,to_date('19960926','YYYYMMDD'));
    insert into testtable(ID,event_type,event_time)
    Values (2,3100101,to_date('19960925','YYYYMMDD'));
    insert into testtable(ID,event_type,event_time)
    Values (3,55,to_date('19960926','YYYYMMDD'));

    What about this?
    SQL> SELECT *
      2  FROM   testtable
      3  WHERE  id IN
      4         ( SELECT id
      5           FROM   ( SELECT id
      6                         , event_type
      7                         , LAG(event_type) OVER (PARTITION BY id ORDER BY event_time) AS prev_event_type
      8                    FROM   testtable
      9                  )
    10           WHERE  event_type      = '3100101'
    11           AND    prev_event_type = '55'
    12         )
    13  ;
            ID EVENT_TYPE EVENT_TIME
             1         55 09/25/1996 00:00:00
             1    3100101 09/26/1996 00:00:00

  • PL SQL Query Question PROMPT

    I have a pl sql block in which I have the following script
    set serveroutput on
    set verify off
    PROMPT MENU
    PROMPT
    PROMPT 1) TOTAL INDEX
    PROMPT
    ACCEPT option PROMPT "Enter here the option "
    declare
    result number:=0;
    begin
    if (&&option=1) then
    select count(1) as total into result from dba_tables where owner='SYS';
    end if;
    dbms_output.put_line('Total is: '||result);
    end;
    I need to do, add an option in the menu in which says "out" and get me out of the menu,
    but if choose other options that I have to show and re-list the menu;
    can any1 help me with this please ?
    sorry for my bad english.
    Regards,

    Rafael M. wrote:
    I have a pl sql block in which I have the following scriptNot a good idea.
    PL/SQL is a server side language.
    You are now mixing that with client side language commands like prompt and accept (SQL*Plus client commands) .
    Do not confuse PL/SQL as part of the SQL*Plus tool and its (very primitive) macro language. They have nothing in common.
    All PL/SQL code in a script needs to be send by the client to the database server. There it is parsed. Compiled. Executed. And a response returned to the client.
    Given the fact that SQL*Plus does not support conditional processing, it makes it extremely difficult (and usually very much a hack) to create even a primitive console interface for an user - despite the power of PL/SQL as PL/SQL executes in an Oracle server process that is not connected to a mouse, keyboard or display device.
    IMO, what you are doing is simply not worth it. And I say that as a SQL*Plus user of many years and still using it every single day extensively (I prefer a CLI client over a GUI client like TOAD for the type of Oracle work/development I do).

Maybe you are looking for

  • PO and FI invoice Bex report

    Hi all , I need a BEx report which shows following 1)     PO with FI Invoice number 2)     Only PO (without invoice) 3)     Only Invoice (without PO) Well I have PO line item ODS and GL ODS. Anyone suggest how should I approach this report??

  • After updating to Lion 10.7.2 System now hangs on boot

    I downloaded and installed Lion 10.7.2 update last night onto my MacBook Pro. The update appeared to download ok after quite some time, but during the install the system appeared to hang transfering files. I left the system installing over night, but

  • Problem ADF Filter

    Hi, I've got a big problem when I launch my application with a WAS 6.0 on Unix. It seems that the adf filter is not installed whereas I've declared it in my web.xml file I don't have this problem when my WAS is on Windows The exception log is : [10/1

  • Refesh Block with Changing Focus to It?

    There is a common problem we all share around text fields. A lot of applications need to switch temporarily to a different block this during user input (e.g. Select "Country" then reduce list of "State/Provinces" to that country, or validate surname

  • ComboBox in a datamatrix

    Dear Sir; how can put a combobox that is linked to a table in a  matrix that is linked to another table? thank you Dear.