Simple SQL Question: How to reference an alias identifier in a subquery?

SELECT (subquery, function, or expression) alias,
(SELECT ... FROM tab2 WHERE col=alias)
FROM tab1 WHERE ...
How to do this in Oracle (referencing alias in a subquery)? Thanks.

drop table tab1;
create table tab1(col varchar2(30));
insert into tab1 values('a');
insert into tab1 values('b');
insert into tab1 values('c');
insert into tab1 values('a');
commit;
drop table tab2;
create table tab2 (col varchar2(30));
insert into tab2 values('a');
insert into tab2 values('b');
commit;
SELECT t1.*,
       (SELECT col
          FROM tab2 t2
         WHERE t2.col = t1.alias) AS scalar_t2_col
  FROM (SELECT col,
               LOWER (UPPER (col)) AS alias
          FROM tab1) t1;Or analogously...
WITH  t1 AS (
SELECT col,
       lower(upper(col)) AS alias
  FROM tab1)
SELECT t1.*,
       (SELECT col
          FROM tab2  t2
         WHERE t2.col = t1.alias) AS scalar_t2_col
  FROM t1;

Similar Messages

  • Simple/silly question: how do I set/change default font/color for outgoing mail messages?

    Simple/silly question: how do I set/change default font/color for outgoing mail messages?

    Just a suggestion..........
    Download Thunderbird.  Easier to use when it comes to what you want to do w/your emails. 

  • Urgent SQL question : how to flip vertical row values to horizontal ?

    Hello, Oracle people !
    I have an urgent SQL question : (simple for you)
    using SELECT statement, how to convert vertical row values to horizontal ?
    For example :
    (Given result-set)
    MANAGER COLUMN1 COLUMN2 COLUMN3
    K. Smith ......1
    K. Smith ...............1
    K. Smith ........................1
    (Needed result-set)
    MANAGER COLUMN1 COLUMN2 COLUMN3
    K. Smith ......1 .......1 .......1
    I know you can, just don't remeber how and can't find exactly answer I'm looking for. Probably using some analytic SQL function (CAST OVER, PARTITION BY, etc.)
    Please Help !!!
    Thanx !
    Steve.

    scott@ORA92> column vice_president format a30
    scott@ORA92> SELECT f.VICE_PRESIDENT, A.DAYS_5, B.DAYS_10, C.DAYS_20, D.DAYS_30, E.DAYS_40
      2  FROM   (select t2.*,
      3                row_number () over
      4                  (partition by vice_president
      5                   order by days_5, days_10, days_20, days_30, days_40) rn
      6            from   t2) f,
      7           (SELECT T2.*,
      8                row_number () over (partition by vice_president order by days_5) RN
      9            FROM   T2 WHERE DAYS_5 IS NOT NULL) A,
    10           (SELECT T2.*,
    11                row_number () over (partition by vice_president order by days_10) RN
    12            FROM   T2 WHERE DAYS_10 IS NOT NULL) B,
    13           (SELECT T2.*,
    14                row_number () over (partition by vice_president order by days_20) RN
    15            FROM   T2 WHERE DAYS_20 IS NOT NULL) C,
    16           (SELECT T2.*,
    17                row_number () over (partition by vice_president order by days_30) RN
    18            FROM   T2 WHERE DAYS_30 IS NOT NULL) D,
    19           (SELECT T2.*,
    20                row_number () over (partition by vice_president order by days_40) RN
    21            FROM   T2 WHERE DAYS_40 IS NOT NULL) E
    22  WHERE  f.VICE_PRESIDENT = A.VICE_PRESIDENT (+)
    23  AND    f.VICE_PRESIDENT = B.VICE_PRESIDENT (+)
    24  AND    f.VICE_PRESIDENT = C.VICE_PRESIDENT (+)
    25  AND    f.VICE_PRESIDENT = D.VICE_PRESIDENT (+)
    26  AND    f.VICE_PRESIDENT = E.VICE_PRESIDENT (+)
    27  AND    f.RN = A.RN (+)
    28  AND    f.RN = B.RN (+)
    29  AND    f.RN = C.RN (+)
    30  AND    f.RN = D.RN (+)
    31  AND    f.RN = E.RN (+)
    32  and    (a.days_5 is not null
    33            or b.days_10 is not null
    34            or c.days_20 is not null
    35            or d.days_30 is not null
    36            or e.days_40 is not null)
    37  /
    VICE_PRESIDENT                     DAYS_5    DAYS_10    DAYS_20    DAYS_30    DAYS_40
    Fedele Mark                                                          35473      35209
    Fedele Mark                                                          35479      35258
    Schultz Christine                              35700
    South John                                                                      35253
    Stack Kevin                                    35701      35604      35402      35115
    Stack Kevin                                    35705      35635      35415      35156
    Stack Kevin                                    35706      35642      35472      35295
    Stack Kevin                                    35707      35666      35477
    Stack Kevin                                               35667      35480
    Stack Kevin                                               35686
    Unknown                             35817      35698      35596      35363      35006
    Unknown                                        35702      35597      35365      35149
    Unknown                                        35724      35599      35370      35155
    Unknown                                                   35600      35413      35344
    Unknown                                                   35601      35451      35345
    Unknown                                                   35602      35467
    Unknown                                                   35603      35468
    Unknown                                                   35607      35475
    Unknown                                                   35643      35508
    Unknown                                                   35644
    Unknown                                                   35669
    Unknown                                                   35684
    Walmsley Brian                                 35725      35598
    23 rows selected.

  • Simple SQL question

    I am a former FoxPro programer who has just started working with the JDBC. I have a quick question about table focus. (I am picking up the JDBC Pocket Reference guide tomorrow so this will hopefully be my only silly question)
    How can I retrieve the entire contents of a table to the result set without saying
    SELECT * FROM TABLE
    This seems this is a very slow alternative to just returning the entire table without going through the whole select process and returning a cursor filled with the data.
    In FoxPro (ugh) you can say SELECT TABLE and that would bring the table into focus. Can you do this in Java or are you limited to an entire select statement by the JDBC driver?

    I'm not a FoxPro programmer, so I don't know what the difference is between SELECT * FROM TABLE and bringing the table into focus. If it's a remote call to a database, that data's gotta come back someway. Those bytes have to cross the network sometime. I don't see the difference, but them I'm ignorant.
    Are you saying that "bringing the table into focus" allows you to grab say the first ten records and then ask for the next ten when you're ready for them?
    I'm not sure, but you might be talking about a scrollable ResultSet or maybe javax.sql.RowSet.
    But I don't know of any way to avoid that SELECT.
    Are you 100% certain that it's a problem? Have you timed the two and compared?
    I have a funny feeling that FoxPro might be doing that SELECT * FROM TABLE behind the scenes and perhaps doing some nice caching to make it look fast.
    Sorry I haven't been much help. Maybe someone who knows both JDBC and FoxPro will be able to answer with authority. - MOD

  • Simple Query Question - How do I return the Last 3 records of a Table?

    Question.
    For example, I have a table that has 50 records.
    How do I, specify in SQL to only return the last 3 records of the table.
    Select a.* from table a where ????

    I was just trying to show an example to a friend on
    how something like this would work and if it was even possible. But it won't work. Here's a simple example:
    SQL> create table emp
      2  (id)
      3  as
      4  select object_id
      5  from   all_objects
      6  order  by object_id;
    Table created.
    SQL> select *
      2  from  (select rownum rn
      3               ,b.*
      4         from   emp b)
      5  where  rn > ( select (max(rownum) - 3)
      6                from    emp)
      7  ;
            RN         ID
         40830      55891
         40831      55892
         40832      55893So far, so good. These are the "last 3" rows inserted. Now delete a bunch of rows and insert 3 new ones:
    SQL> delete emp where id < 40000;
    33423 rows deleted.
    SQL> commit;
    Commit complete.
    SQL> insert into emp values (60000);
    1 row created.
    SQL> insert into emp values (60001);
    1 row created.
    SQL> insert into emp values (60002);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select *
      2  from  (select rownum rn
      3               ,b.*
      4         from   emp b)
      5  where  rn > ( select (max(rownum) - 3)
      6                from    emp)
      7  ;
            RN         ID
          7410      55891
          7411      55892
          7412      55893Here's the problem. Even though the "last 3 rows" are 60000 - 60002, I still get the same ones as the first query.

  • Simple IPhoto Questions-How to reduce picture file size?

    Hello,
    I know how to reduce the size of photos using the Mail program, but is there a simple way to reduce the size of photo files and keep them for sending through the internet at later dates. I would like to keep the photos at their original size for viewing, but transfer photos to a file where they will be used to send over the net in much smaller format size.
    thanks

    As Larry says, you can resize the pics on Export using the File -> Export command.
    and keep them for sending through the internet at later dates.
    The intention in iPhoto is that you would only reduce the size on a case-by-case basis, rather than resize the whole library. As iPhoto always maintains the Originals this require quite a lot of disk space
    Regards
    TD

  • Simple SQL question (I think)

    I have an SQL newbie question. I have a select query into a table (T1) that contains 2 id numbers (sender and receiver), in addition to other information. The id numbers are mapped, in a separate table (T2), to names. So, we have table T1 with data columns transactionTime (timestamp), sender id (varchar(10)), receiver id (varchar(10), and additional info in each row about transactions. Table T2 has two columns ... name (varchar(60)) and id (varchar(10)), where the id is the same as senders and receivers in the transaction table. I'd to find all transactions between a start timestamp and an end timestamp, and I would like to also display the name associated with each id.
    So, as a newbie, I tried (simplified)
    select T1.transactionTime, T1.sender, T2alias1.name, T1.receiver, T2alias2.name
    from T1, T2 as T2alias1, T2 as T2alias2
    where T1.transactionTime > timestamp('aStartTime') and T1.transactionTime < timestamp('anEndTime')
    and T1.sender = T2alias1.id and T1.receiver = T2alias2.id
    order by T1.transactionTime
    This returns some of the rows between the start and end time, but only seems to get records for a single sender, and not all of those.
    I've also tried
    where T1.transactionTime > timestamp('aStartTime') and T1.transactionTime < timestamp('anEndTime')
    and (T1.sender = T2alias1.id or T1.receiver = T2alias2.id)
    but that takes a long time to return, and returns more results then there are records in the transaction table.
    I obviously am not do this correctly. I just want to get the name associated with the ids. Any help?
    None of these look quite right to me, and none return the the data I am looking for
    ¦{Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Hi,
    This query should give you correct results if, there are all assciated id's and names for both senders and recievers in table2.
    SELECT t1.transactiontime, t1.sender,t21.NAME,t1.reciever, t22.NAME
      FROM t1, t2 t21, t2 t22
    WHERE     t1.transactiontime BETWEEN timestamp(startdate) and timestamp(enddate)
           AND t1.sender = t21.ID
           AND t1.reciever = t22.ID;if either sender's ID or Recievers ID are missing in table2, the whole record will get ignored.
    You can try an outer Join get records even if the ID's are missing in table2.
    SELECT t1.transactiontime, t1.sender,t21.NAME,t1.reciever, t22.NAME
      FROM t1, t2 t21, t2 t22
    WHERE     t1.transactiontime BETWEEN timestamp(startdate) and timestamp(enddate)
           AND t1.sender = t21.ID(+)
           AND t1.reciever = t22.ID(+);if you want to know the missing ID's the add a IS NULL caluse.
    SELECT * FROM(
    SELECT t1.transactiontime, t1.sender,t21.NAME name1,t1.reciever, t22.NAME name2
      FROM t1, t2 t21, t2 t22
    WHERE     t1.transactiontime BETWEEN timestamp(startdate) and timestamp(enddate)
           AND t1.sender = t21.ID(+)
           AND t1.reciever = t22.ID(+))
      WHERE  name1 IS NULL or name2 IS NULL;G.

  • Another simple SQL question?

    Can someone help me (an SQL novice) optimize this query?
    Assume a table TRANS with four columns ACCOUNT, PURCHASE_DATE,
    ITEM_NUMBER, and QUANTITY. (Fairly self evident names, I hope.)
    I want to create a view that returns the most recent purchase
    date for each account, and the sum of all items purchase on that
    date.
    The following works, but involves a sub-query, which I think
    must be inefficient (the real table I'm working with has about
    20 million rows, returning about 1 million rows and takes
    hours!):
    select unique a.account,
    purchase_date,
    sum(quantity)
    from TRANS a,
    (select account,
    max(purchase_date) as cdate
    from TRANS
    group by account) b
    where a.account = b.account and
    a.purchase_date = b.cdate
    group by a.account;
    A second, similar query is to find the most recent purchase
    date, by account, for each item, and the total quantity of that
    item purchased on that date. But, hopefully, an answer to the
    first question will lead me in the right direction.
    Thanks!
    Bob

    Here is another batch of queries to test. The one on top is the
    only one that is drastically different from your query or
    anything that David already suggested.
    SELECT  account,
            purchase_date,
            total
    FROM    (SELECT   account,
                      purchase_date,
                      SUM (quantity) total,
                      RANK () OVER
                           (PARTITION BY account
                            ORDER BY purchase_date DESC)
                      AS rk
             FROM     trans
             GROUP BY account, purchase_date)
    WHERE    rk = 1
    ORDER BY account
    SELECT   a.account,
             a.purchase_date,
             SUM (quantity) total
    FROM     trans a,
             (SELECT   account,
                       MAX (purchase_date) cdate
              FROM     trans
              GROUP BY account) b
    WHERE    a.account = b.account
    AND      a.purchase_date = b.cdate
    GROUP BY a.account, a.purchase_date
    ORDER BY a.account
    SELECT    account,
              purchase_date,
              SUM (quantity) total
    FROM      trans a
    WHERE NOT EXISTS
              (SELECT 0
               FROM   trans b
               WHERE  b.account = a.account
               AND    b.purchase_date > a.purchase_date)
    GROUP BY  account, purchase_date
    ORDER BY  a.account
    SELECT   a.account,
             a.purchase_date,
             SUM (quantity) total
    FROM     trans a
    WHERE    a.purchase_date =
             (SELECT MAX (b.purchase_date)
              FROM   trans b
              WHERE  b.account=a.account)
    GROUP BY a.account, a.purchase_date
    ORDER BY a.account
    SELECT   a.account,
             a.purchase_date,
             SUM (quantity) total
    FROM     trans a
    WHERE    (a.account, a.purchase_date) IN
             (SELECT   b.account,
                       MAX (b.purchase_date)
              FROM     trans b
              GROUP BY b.account)
    GROUP BY a.account, a.purchase_date
    ORDER BY a.account
    SELECT   a.account,
             a.purchase_date,
             SUM (quantity) total
    FROM     trans a
    WHERE    a.purchase_date =
             (SELECT /*+ INDEX_DESC (b my_index) */
                     b.purchase_date
              FROM   trans b
              WHERE  b.account = a.account
              AND    ROWNUM = 1)
    GROUP BY a.account, a.purchase_date
    ORDER BY a.account

  • Simple Java Question - How to Overwrite Set

    After working so much in Java, wondering how do i overwrite java.util.Set
    My pojo is Set of associated object. For example
    class Parent
    private Set child = new HashSet(0);
    public Set getChild() {
    return this.child;
    I wanna overwrite set of child objects in pojo with the set passed from UI tier
    Any pointers/suggestions will be highly appreciated
    Regards
    Bansi

    public void setChild(Set child) { this.child = child; }or if you need more control over the Set instance:
    public void setChild(Set child)
        this.child.clear();
        this.child.addAll(child);
    }

  • Simple audio question - how to mute a track?

    Have a timeline with one video and two audio tracks - both tracks are stereo.
    I need to mute one of the audio tracks to make edits to the other.
    How can I mute one of the audio tracks?

    Awesome - that's what I was looking for! Ton of clips so hitting shift-Z and then selecting the entire length of the track seems to work.
    Where is that "V" feature in the menus?

  • Simple "Find" question -- How to find values in cells with formulas?

    Sorry if this is a stupid question but I don't find the answer. I have a table with values that are all computed via complex formulas. I want to FIND, say, when the value is 19. I can't find a way to do it: If I use the "find" command and type 19, the program finds when 19 is written INSIDE the formulas, e. g., it finds the cell where a formula is thus defined:
    =Table Correct Responses :: A19
    but does NOT find the cell with VALUE 19.
    Tried different syntax combinations without success. Any idea?
    Cheers,
    l.

    My two cents:
    Select the entire table.
    Cells Inspector.
    Define a rule like the one I uses here.
    It will highlight the cells whose value matches the condition.
    Yvan KOENIG (from FRANCE lundi 26 mai 2008 17:53:33)

  • Simple Java Question - How to test if a given string is numeric or not

    Hi Experts,
    I have written one Java program. It fetches user ID from UME. The code is as below:
    Iterator itr = role.getUserMembers(true);
    while (itr.hasNext()) {
    String uniqId = (String) itr.next();
    IUser thisUser = myUserFactory.getUser(uniqId);
    wdComponentAPI.getMessageManager().reportSuccess("here "+thisUser.getUid() );
    Used ID is thisUser.getUid() . I have to find out all user IDs which are numeric.
    Do we have any standard Java program (API) to find out whether thisUser.getUid()  is numeric or not.
    Regards,
    Gary

    Hi
    Just try to parse this string to integer or long number and catch the NumberFormatException.
    Somthing like:
    try
       Integer.parseInt(userIdString);
    catch(NumberFormatException e)
      //do sume thing when user id is not number
    good luck

  • SQL question - how to get only the leafs

    i have a table of departments
    create table departments (
      dept   number ,
      father number
    )this is the data
    1
    1/2
    1/2/4
    1/2/4/6
    1/2/4/7
    1/2/4/8
    1/3
    1/3/5
    1/3/9given a department number i need only the leafs (the lowest member in the hierarchy).
    for dept 2 i need 6, 7, 8
    for dept 3 i need 5 , 9
    for dept 5 i need 5
    this is my query
    select dept
    from departments
    where connect_by_isleaf = 1
    start with father = :X
    connect by prior dept = father;this query will work only if :X is not a leaf. for 2 and 3 this query will work but for 5 it will return nothing (since connect_by_isleaf is 0 if the department apears in the start with clause).
    thanks !

    try
    select dept
    from departments
    where connect_by_isleaf = 1
    start with dept = :X
    connect by prior dept = father;Dang very late
    Message was edited by:
    Versatile

  • Sql question how to write this query

    This is my query:
    SELECT msi.segment1 item, bsd.operation_code, bd.department_code,
    bsd.operation_description
    FROM mtl_system_items msi,
    bom_operational_routings bort,
    bom_operation_sequences bos,
    bom_departments bd,
    bom_operation_resources br,
    bom_resources bor,
    bom_standard_operations bsd
    WHERE msi.inventory_item_id = bort.assembly_item_id
    AND msi.organization_id = bort.organization_id
    AND bort.routing_sequence_id = bos.routing_sequence_id
    AND bos.department_id = bd.department_id
    AND bd.department_id = bsd.department_id
    AND bos.standard_operation_id = bsd.standard_operation_id
    AND bos.operation_sequence_id = br.operation_sequence_id
    AND bor.resource_id = br.resource_id
    AND bos.reference_flag = 1
    AND msi.organization_id = '82'
    AND bos.disable_date IS NULL
    GROUP BY msi.segment1,
    bsd.operation_code,
    bd.department_code,
    bos.operation_description,
    bsd.operation_description,
    bor.resource_code
    Which essentially produces this output:
    Item Op code Dept Description
    123 10 Warehouse Move parts
    123 20 Assembly Finish Parts
    123 30 Inspection Complete
    I need to capture when the part goes into Inspection and from where it came, so in this case, From Assembly to Inspection.
    I don't even know where to start
    Thanks for any direction

    Not sure I've got the columns names right from your example, but does this get you started at all?
    It sounds like you might need an analytic function like LAG or LEAD.
    with t as
    (select 123 item, 10 op, 'Warehouse' code, 'Move' dept, 'parts' description
    from dual
    union
    select 123, 20, 'Assembly', 'Finish', 'Parts'
    from dual
    union
    select 123, 30, 'Inspection', 'Complete',null
    from dual)
    select *
    from (
    select item, op, code, dept, description, lag(code) over (partition by item order by op) previous_code
    from t
    where code = 'Inspection';
          ITEM         OP CODE       DEPT     DESCR PREVIOUS_C
           123         30 Inspection Complete       AssemblyMessage was edited by:
    dombrooks

  • Simple bash question: how to script into new shell?

    I want to have a script that opens a new terminal and calls some commands. I am to stupid. :oops:

    Pink Chick wrote:
    *Smack*
    This should be ok. I was tired of typing "makepkg foobar", and so I wrote a tiny dirty nautilus script to do that for me. Unfortunately, it worked in the background. Not bad, if you KNOW your package is ok, but in case of errors.
    Thank you all. 
    hmm sounds like an interesting idea... i don't use nautilus myself... but i could see it being cool to double click a PKGBUILD file and have it run makepkg...

Maybe you are looking for