Multiple joins on the same table

I'm new to SQL 2005 & C# - I'm a MySQL/PHP crossover.
I'm using s Stored Procedure and I'm trying to do multiple joins onto
one table.  I have 6 fields in one table that are foreign keys of
another table:
Table1
id
PrimaryCode
SecondaryCode1
SecondaryCode2
SecondaryCode3
SecondaryCode4
SecondaryCode5
Table 2
id
Title
CommCode
The fields in table 1 (except is obviously) hold the id of a row in
Table 2.  When displaying data I want to display "Title" -
"CommCode" for each item in Table 1.  I got myself started by
searchig on the net and I have a stored procedure.  The obvious
problem is that as it goes through the Query only the last value
remains in place - since each value before it is cleared in the
UNION.   How can I do this??  Here's my Stored Procedure:
=====================================
ALTER PROCEDURE GetRegistersSpecific
@SearchTxt int
AS
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
PrimaryCode AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON PrimaryCode = CommodityCodes.id
UNION
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
SecondaryCode1 AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON SecondaryCode1 = CommodityCodes.id
UNION
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
SecondaryCode2 AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON SecondaryCode2 = CommodityCodes.id
UNION
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
SecondaryCode3 AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON SecondaryCode3 = CommodityCodes.id
UNION
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
SecondaryCode4 AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON SecondaryCode4 = CommodityCodes.id
UNION
SELECT
registrations.Company,registrations.Address1,registrations.Address2,registrations.City,registrations.State,registrations.Zip,registrations.ContactName,registrations.Phone,registrations.Fax,registrations.Email,registrations.Website,registrations.Feid,registrations.BusinessType,registrations.BackupWitholding,registrations.SignedName,registrations.SignedDate,
SecondaryCode5 AS MyID, Title, CommodityCode
FROM registrations
JOIN CommodityCodes ON SecondaryCode5 = CommodityCodes.id
WHERE registrations.ID = @SearchTxt
=====================================
Thanks

Well, I tried using UNION ALL and got the same response.  I also tried doing :
Title AS SecTitle1
Title AS SecTitle2
Title AS SecTitle3
etc...
I get different values in the Output Window when I execute the query,
but as expected it only returns the first value since the script
executes all the way through before it outputs values.
Here's how I'm executing:
while (rdr.Read())
                // get the results of each column
string company = (string)rdr["Company"].ToString();
string address1 = (string)rdr["Address1"].ToString();
string address2 = (string)rdr["Address2"].ToString();
string city = (string)rdr["City"].ToString();
string state = (string)rdr["State"].ToString();
string zip = (string)rdr["Zip"].ToString();
string phone = (string)rdr["Phone"].ToString();
string fax = (string)rdr["Fax"].ToString();
string email = (string)rdr["Email"].ToString();
string website = (string)rdr["Website"].ToString();
string feid = (string)rdr["Feid"].ToString();
string businessType = (string)rdr["BusinessType"].ToString();
string contactName = (string)rdr["ContactName"].ToString();
string primaryCode = (string)rdr["PrimTitle"].ToString();
string secondaryCode1 = (string)rdr["SecTitle1"].ToString();
string secondaryCode2 = (string)rdr["SecTitle2"].ToString();
string secondaryCode3 = (string)rdr["SecTitle3"].ToString();
string secondaryCode4 = (string)rdr["SecTitle4"].ToString();
string secondaryCode5 = (string)rdr["SecTitle5"].ToString();
string backUp = (string)rdr["BackupWitholding"].ToString();
string signedName = (string)rdr["SignedName"].ToString();
string signedDate = (string)rdr["SignedDate"].ToString();

Similar Messages

  • Multiple Join over the same table

    Hi all,
    I have a question concerning joins...
    We are working with Oracle 8i.
    Consider I have the following tables t_emp_team and t_emp_names
    t_emp_team
    emp_id1, emp_id2, emp_id3
    2, 7, 9
    3, 8, 6
    4, 11, 3
    5, 10, 9
    t_emp_names
    emp_id, name
    2, Peter
    3, Mark
    4, Liz
    5, Will
    Now I want a join selecting all the names to the IDs.
    My idea was to use a join query like this:
    select
    a.emp_id1,
    a.emp_id2,
    a.emp_id3,
    b1.name,
    b2.name,
    b3.name
    from
    t_emp_team a,
    t_emp_names b1,
    t_emp_names b2,
    t_emp_names b3
    where
    a.emp_id1=b1.emp_id(+) and
    a.emp_id2=b2.emp_id(+) and
    a.emp_id3=b3.emp_id(+)
    Obviously this is not possible in Oracle as the results are not correct.
    Do you have a different idea other than Subselects for each name? (The real table has 16 IDs meaning we would end up with 16 subselects)
    Thanks for your help!
    -Peter

    Hi,
    Obviously this is not possible in Oracle as the results
    are not correct.Why do you think so ?
    SQL> select
      2  a.emp_id1,
      3  a.emp_id2,
      4  a.emp_id3,
      5  b1.name,
      6  b2.name,
      7  b3.name
      8  from
      9  t_emp_team a,
    10  t_emp_names b1,
    11  t_emp_names b2,
    12  t_emp_names b3
    13  where
    14  a.emp_id1=b1.emp_id(+) and
    15  a.emp_id2=b2.emp_id(+) and
    16  a.emp_id3=b3.emp_id(+)
    17  /
    &nbsp
       EMP_ID1    EMP_ID2    EMP_ID3 NAME       NAME       NAME
             4         11          3 Liz                   Mark
             3          8          6 Mark
             5         10          9 Will
             2          7          9 Peter
    &nbspBut it would be correct to use normzlized form
    of t_emp_team:
    SQL> desc t_emp_team_n
    Name                                      Null?    Type
    TEAM_ID                                            NUMBER
    EMP_ID                                             NUMBER
    &nbsp
    SQL> select * from t_emp_team_n;
    &nbsp
       TEAM_ID     EMP_ID
             1          4
             1         11
             1          3
             2          3
             2          8
             2          6
             3          5
             3         10
             3          9
             4          2
             4          7
             4          9
    &nbsp
    12 rows selected.
    &nbsp
    Elapsed: 00:00:00.04
    SQL> select team_id, a.emp_id, name
      2  from t_emp_team_n a, t_emp_names b
      3  where a.emp_id = b.emp_id (+)
      4  order by 1,3
      5  /
    &nbsp
       TEAM_ID     EMP_ID NAME
             1          4 Liz
             1          3 Mark
             1         11
             2          3 Mark
             2          8
             2          6
             3          5 Will
             3         10
             3          9
             4          2 Peter
             4          7
             4          9
    &nbsp
    12 rows selected.and use the advise of William Robertson:
    http://www.williamrobertson.pwp.blueyonder.co.uk/documents/one_row.html
    SQL> select team_id, substr(epath,2) "ids",
      2  rtrim(substr(npath,2),',') "members"
      3  from (
      4  select team_id, max(sys_connect_by_path(emp_id,',')) epath,
      5  max(sys_connect_by_path(name,',')) npath from (
      6  select team_id, a.emp_id, name, row_number()
      7  over(partition by team_id order by name nulls last) rn
      8  from t_emp_team_n a, t_emp_names b
      9  where a.emp_id = b.emp_id (+)
    10  )
    11  start with rn = 1 connect by prior team_id = team_id
    12  and prior rn = rn -1
    13  group by team_id
    14  )
    15  /
    &nbsp
       TEAM_ID ids                            members
             1 4,3,11                         Liz,Mark
             2 3,8,6                          Mark
             3 5,10,9                         Will
             4 2,7,9                          PeterRgds.

  • Two diffferent type of joins on the same table

    I have a query that, it seems, requires me to two two different types of joins to the same table.
    from table t1
    LEFT OUTER JOIN table2 t2
    ON t1.col1= t2.col1
    and t1.col2 = t2.col2
    and t1.col5 = t2.col5
    LEFT OUTER JOIN table2 t3
    ON t1.col1= t3.col1
    and t1.col2 = t3.col2
    and t1.col4 = t3.col4
    would I run into a problem if I just did:
    LEFT OUTER JOIN table2 t2
    ON t1.col1= t2.col1
    and t1.col2 = t2.col2
    and t1.col3 = t2.col3
    and t1.col4 = t2.col4
    and t1.col5 = t2.col5
    And if I go with option 1 and I also create two different types of indexes to accommodate each join, will 10g know which ones to use?
    Message was edited by:
    user623359
    Message was edited by:
    user623359

    No version number and no statement of what it is you are trying to do ... but one solutions is to use the traditional Oracle syntax and the first part, then wraps it in parentheses turning it into an inline view, and then join that to the next part.

  • Can Numbers Display Multiple Views of the Same Table

    Hi,
    Excel and Appleworks both have a pull down tab on the vertical bar allowing multiple views into the same spreadsheet (table). Can Numbers do this?
    I have a set of calculations at the top of a spreadsheet that are based on years and years worth of data under the calculations (same column). I add data for each new event (the rows) and watch the calculations at the top of the data. Easy to do in Excel or Appleworks, but, I can't figure out how to do this in Numbers.
    Example:
    Spot1 Spot2
    Total 15 36
    Avg 5 12
    Jan 09 5 10
    Feb 09 6 20
    Mar 09 4 6
    Apr 09
    So... does Numbers allow the view "split" or multiple views that Excel and Appleworks allow?
    Thanks!
    Tom

    Question asked and responded several times:
    feature unavailable.
    For multiple views of a table there is an easy workaround as we may build a table whose every cells grab their contents from the 'master' one.
    _Go to "Provide Numbers Feedback" in the "Numbers" menu_, describe what you wish.
    Then, cross your fingers, and wait _at least_ for iWork'10
    Yvan KOENIG (VALLAURIS, France) mardi 1 septembre 2009 21:56:42

  • Opinion needed on best way to map multiple table joins (of the same table)

    Hi all
    I have a query of the format:
    select A.col1, B.col1,C.col1
    FROM
    MASTER_TABLE A, ATTRIBUTE_TABLE B, ATTRIBUTE_TABLE C
    WHERE
    A.key1 = B.key1 (+)
    AND
    A.key1 = C.key1(+)
    AND
    B.key2(+) = 100001
    AND
    C.key2(+) = 100002
    As you can see, I am joining the master table to the attribute table MANY times over, (over 30 attributes in my actual query) and I am struggling to find the best way to map this efficiently as the comparison for script vs. mapping is 1:10 in execution time.
    I would appreciate the opinion of experienced OWB users as to how they would tackle this in a mapping and to see if they use the same approach as I have done.
    Many thanks
    Adi

    SELECT external_reference, b.attribute_value AS req_date,
    c.attribute_value AS network, d.attribute_value AS spid,
    e.attribute_value AS username, f.attribute_value AS ctype,
    g.attribute_value AS airtimecredit, h.attribute_value AS simnum,
    i.attribute_value AS lrcredit, j.attribute_value AS airlimitbar,
    k.attribute_value AS simtype, l.attribute_value AS vt,
    m.attribute_value AS gt, n.attribute_value AS dt,
    o.attribute_value AS datanum, p.attribute_value AS srtype,
    q.attribute_value AS faxnum,
    R.ATTRIBUTE_VALUE AS FAXSRTYPE,
    s.attribute_value AS extno,
    t.attribute_value AS tb, u.attribute_value AS gb
    v.attribute_value AS mb, w.attribute_value AS stolenbar,
    x.attribute_value AS hcredit, y.attribute_value AS adminbar,
    z.attribute_value AS portdate
    FROM csi_item_instances a,
    csi_iea_values b,
    csi_iea_values c,
    csi_iea_values d,
    csi_iea_values e,
    csi_iea_values f,
    csi_iea_values g,
    csi_iea_values h,
    csi_iea_values i,
    csi_iea_values j,
    csi_iea_values k,
    csi_iea_values l,
    csi_iea_values m,
    csi_iea_values n,
    csi_iea_values o,
    csi_iea_values p,
    csi_iea_values q,
    CSI_IEA_VALUES R,
    csi_iea_values s,
    csi_iea_values t,
    csi_iea_values u,
    csi_iea_values v,
    csi_iea_values w,
    csi_iea_values x,
    csi_iea_values y,
    csi_iea_values z
    WHERE a.instance_id = b.instance_id(+)
    AND a.instance_id = c.instance_id(+)
    AND a.instance_id = d.instance_id(+)
    AND a.instance_id = e.instance_id(+)
    AND a.instance_id = f.instance_id(+)
    AND A.INSTANCE_ID = G.INSTANCE_ID(+)
    AND a.instance_id = h.instance_id(+)
    AND a.instance_id = i.instance_id(+)
    AND a.instance_id = j.instance_id(+)
    AND a.instance_id = k.instance_id(+)
    AND a.instance_id = l.instance_id(+)
    AND a.instance_id = m.instance_id(+)
    AND a.instance_id = n.instance_id(+)
    AND a.instance_id = o.instance_id(+)
    AND a.instance_id = p.instance_id(+)
    AND a.instance_id = q.instance_id(+)
    AND A.INSTANCE_ID = R.INSTANCE_ID(+)
    AND a.instance_id = s.instance_id(+)
    AND a.instance_id = t.instance_id(+)
    AND a.instance_id = u.instance_id(+)
    AND a.instance_id = v.instance_id(+)
    AND a.instance_id = w.instance_id(+)
    AND a.instance_id = x.instance_id(+)
    AND a.instance_id = y.instance_id(+)
    AND a.instance_id = z.instance_id(+)
    AND b.attribute_id(+) = 10000
    AND c.attribute_id(+) = 10214
    AND d.attribute_id(+) = 10132
    AND e.attribute_id(+) = 10148
    AND f.attribute_id(+) = 10019
    AND g.attribute_id(+) = 10010
    AND h.attribute_id(+) = 10129
    AND i.attribute_id(+) = 10198
    AND j.attribute_id(+) = 10009
    AND k.attribute_id(+) = 10267
    AND l.attribute_id(+) = 10171
    AND m.attribute_id(+) = 10184
    AND n.attribute_id(+) = 10060
    AND o.attribute_id(+) = 10027
    AND p.attribute_id(+) = 10049
    AND q.attribute_id(+) = 10066
    AND R.ATTRIBUTE_ID(+) = 10068
    AND s.attribute_id(+) = 10065
    AND t.attribute_id(+) = 10141
    AND u.attribute_id(+) = 10072
    AND v.attribute_id(+) = 10207
    AND w.attribute_id(+) = 10135
    AND x.attribute_id(+) = 10107
    AND y.attribute_id(+) = 10008
    AND z.attribute_id(+) = 10103
    AND external_reference ='07920490103'
    If I run this it takes less than a second in TOAD, when mapped in OWB it takes ages. 10:1 is a conservative estimate. In reality it takes 15-20 minutes. CSI_IEA_VALUES has 30 million rows CSI_ITEM_INSTANCES has 500,000 rows.
    Hope that helps. I would love to know how others would tackle this query.

  • Sql Joins in the same table

    Hi, I have a table (DFO_TRACE) that has two columns CURRENT_FID and SOURCE_FID and I want to find out what are the values that are present in the CURRENT_FID that are not present in the SOURCE_FID for example:
    SOURCE_FID | CURRENT_FID
    1 | 2
    2 | 3
    2 | 4
    4 | 5
    So in the example above I would like to do a select that would return CURRENT_FID = 3 and 5.
    I've tried to write the following code to do this, but when I execute it, it doesn't return anything to me:
    select A.CURRENT_FID from DFO_TRACE A
    left outer join DFO_TRACE B
    on A.CURRENT_FID = B.SOURCE_FID
    where A.ID_GRUPO_TRACE = 177 and B.ID_GRUPO_TRACE = 177 and B.SOURCE_FID is null;Can anyone help me?
    PS: I am using Oracle 10g.
    Thanks,
    Komyg

    The reason you posted query does not work is that the predicate B.ID_GRUPO_TRACE = 177 in the where clause removes all od the "made_up" records from the b version of the table since their id_grupo_trace would be null. You would need to modify your query to something more like:
    SELECT a.current_fid
    FROM dfo_trace a
       LEFT JOIN dfo_trace b
          ON a.current_fid = b.source_fid and
             a.id_grupo_trace = b.id_grupo_trace
    WHERE a.id_grupo_trace = 177 and
          b.source_fid IS NULL;John

  • Retriving multiple rows from the same table

    Hi all,
    I have a table consisting of an id column as well as a start and end date.
    What I want to do is select the difference between start and end time for different id's.
    For example select (endtime where id = 1 - starttime where id = 2).
    Is this possible to do using a single select statement?
    Thanks ppl

    OK, so you want every combination of dropped_id 500 with dropped_id 1000? For example:
    WITH test_data as (SELECT 500 dropped_id, TO_DATE('13/05/2009', 'DD/MM/YYYY') insert_date, TO_DATE('12/05/2009 12:00:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('12/05/2009 12:30:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL UNION ALL
    SELECT 500, TO_DATE('12/05/2009', 'DD/MM/YYYY'),  TO_DATE('11/05/2009 12:04:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('11/05/2009 12:41:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL UNION ALL
    SELECT 500,TO_DATE('11/05/2009', 'DD/MM/YYYY'), TO_DATE('10/05/2009 12:18:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('10/05/2009 12:59:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL UNION ALL
    SELECT 1000,TO_DATE('13/05/2009', 'DD/MM/YYYY'), TO_DATE('12/05/2009 12:30:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('12/05/2009 13:30:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL UNION ALL
    SELECT 1000,TO_DATE('12/05/2009', 'DD/MM/YYYY'), TO_DATE('11/05/2009 12:41:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('11/05/2009 14:30:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL UNION ALL
    SELECT 1000,TO_DATE('11/05/2009', 'DD/MM/YYYY'), TO_DATE('10/05/2009 12:19:00', 'DD/MM/YYYY HH24:MI:SS') starttime, TO_DATE('10/05/2009 13:30:00', 'DD/MM/YYYY HH24:MI:SS') endtime FROM DUAL)
    -- end test data
    SELECT tab1.insert_date insert_date1, tab2.insert_date insert_date2, tab1.dropped_id dropped_id1,   tab2.dropped_id dropped_id2, ROUND(tab2.endtime -  tab1.starttime,2) time_diff
       FROM test_data tab1
    CROSS JOIN test_data tab2
    WHERE tab1.dropped_id = 500
        AND tab2.dropped_id = 1000;
    INSERT_DA INSERT_DA DROPPED_ID1 DROPPED_ID2  TIME_DIFF
    13-MAY-09 13-MAY-09         500        1000        .06
    12-MAY-09 13-MAY-09         500        1000       1.06
    11-MAY-09 13-MAY-09         500        1000       2.05
    13-MAY-09 12-MAY-09         500        1000        -.9
    12-MAY-09 12-MAY-09         500        1000         .1
    11-MAY-09 12-MAY-09         500        1000       1.09
    13-MAY-09 11-MAY-09         500        1000      -1.94
    12-MAY-09 11-MAY-09         500        1000       -.94
    11-MAY-09 11-MAY-09         500        1000        .05
    9 rows selected.

  • Optimization of an update that uses multiple selects on the same table

    Oracle version : 10g
    Hello,
    I have created an UPDATE statement which uses two selects on a single table to do an update according to date criteria.
    Due to the fact that the update has different criteria for updating according to whether the date falls within one of two ranges
    (future or past) two selects were used together with a union between them to unite the results.
    The end result is of course two full table scans and a poorly performing execution.
    The goal is to update a field XDEL (type number) based on a date column value (column XDATE) where the dates of interest
    fall both in the past (less than sysdate minus 45 days) and in the future (greater than sysdate +90 days). For records with XDATE
    values within these ranges, I wish to set field XDEL=1
    For records in the future (>=sysdate+90):
    for a unique combination of XNAME+XCODE, where XDATE is within the date range for update, set XDEL=1
    but for a non-unique combination of XNAME+XCODE+XDATE, update XDEL=1 only for the 'oldest' future record, ie update XDEL for MIN(XDATE) only
    For records in the future (< sysdate+45):
    for a unique combination of XNAME+XCODE, where XDATE is within the date range for update, set XDEL=1
    for a non-unique combination of XNAME+XCODE, update XDEL=1 only for the 'newest' past record, ie update XDEL for MAX(XDATE) only
    The combination of XNAME+XCODE XDATE is unique. As an example of the 'in the past' scenario:
    XNAME | XCODE | XDATE
    ================
    1. AAAA ~ 002 ~ 01/01/2006
    2. AAAA ~ 002 ~ 02/01/2006
    3. AAAA ~ 002 ~ 03/01/2006
    4. XXXX ~ 123 ~ 02/01/2006
    Here, we would update XDEL in record 3. as it corresponds to MAX(XDATE) for the GROUP AAA002 (and we would also update record 4. as a unique record that meets the criteria)
    As an example of the 'in the future' scenario:
    XNAME | XCODE | XDATE
    ================
    1. HHHH ~ 002 ~ 01/01/2011
    2. HHHH ~ 002 ~ 02/01/2011
    3. HHHH ~ 002 ~ 03/01/2011
    4. XXXX ~ 123 ~ 02/01/2011
    Here, we would update XDEL in record 1. as it corresponds to MIN(XDATE) for the GROUP HHHH002 (and of course 4. as a unique record that meets the criteria)
    Here is a query that works, but is slow:
    UPDATE TAB1 SET XDEL=1 WHERE (XNAME,XCODE,XDATE) IN ((SELECT XNAME,XCODE,max(XDATE) FROM TAB1 WHERE XDATE < sysdate-45 GROUP BY XNAME,XCODE) UNION (SELECT XNAME,XCODE,min(XDATE) from TAB1 WHERE XDATE >= sysdate+90 GROUP BY XNAME,XADDR));
    XDATE is a DATE type
    XCODE and XADDR are VARCHAR2
    XDEL is number type
    Any ideas would be greatly appreciated.

    Here is a suggestion. The code is not tested.
    update tab1 t1
       set xdel = 1
    where exists (select null
                     from (
                            select xname,
                                   xcode,
                                   max(case when xdate < sysdate-45 then xdate else null end) xdate1,
                                   min(case when xdate >= sysdate+90 then xdate else null end) xdate2
                              from tab1
                             group by xname, xcode
                          ) t
                    where t1.xname = t.xname
                      and t1.xcode = t.xcode
                      and (t1.xdate = t.xdate1 or t1.xdate = t.xdate2))

  • Multiple and conditions in the same table

    Ok I am going to kick myself for this, but I can't figure it out. I am trying to figure out how to find employees which match multiple criteria in the same table.
    create table emp
    (empno number,
    name  varchar2(10))
    create table skills
    (skill_id   number,
    skill_code varchar2(20))
    create table emp_skills
    (empno    number,
    skill_id number,
    rating   number)
    insert into emp values(1, 'SMITH');
    insert into emp values(2, 'JONES');
    insert into skills values (1, 'SQL');
    insert into skills values (2, 'PLSQL');
    insert into skills values (3, 'JAVA');
    insert into emp_skills values(1,1, 8);
    insert into emp_skills values(1,2, 9);
    insert into emp_skills values(1,3, 10);
    insert into emp_skills values(2,1,9);
    insert into emp_skills values(2,2,2);
    insert into emp_skills values(2,3,7);Now I need to come up with a query finding all employees who match all 3 of the following criteria:
    1) Have at least a 5 rating in SQL
    2) Have at least a 6 rating in PLSQL
    3) Have at least a 7 rating in JAVA
    So using this I would expect to return only SMITH since his/her skills meet all 3 criteria. I dont want to use OR in my query since I want all 3 to match not just one of them.
    I have a feeling I will need to self join the table - but this is going to be part of a dynamic query for APEX where the users can choose the skills and ratings they want employees to adhere to, so I dont know the number of criteria or the exact criteria in advance. But I figure if I can get a proof of concept SQL I can make it work dynamically.
    Any ideas are appreciated - sorry for the long post but I figure more detail is better

    with es1 as(
    select s.skill_code, es.empno, es.rating
    from skills s, emp_skills es
    where s.skill_id = es.skill_id
    /* main */
    select e.*
    from emp e
    where
    exists
    (select 'x' from es1
    where es1.rating >= 5
    and es1.skill_code = 'SQL'
    and es1.empno = e.empno)
    and
    exists
    (select 'x' from es1
    where es1.rating >= 6
    and es1.skill_code = 'PLSQL'
    and es1.empno = e.empno)
    and
    exists
    (select 'x' from es1
    where es1.rating >= 7
    and es1.skill_code = 'JAVA'
    and es1.empno = e.empno)
    -- Addition (Another example)
    with es1 as(
    select s.skill_code, es.empno, es.rating
    from skills s, emp_skills es
    where s.skill_id = es.skill_id
    /* main */
    select e.*
    from emp e
    where
    (select count(distinct es1.skill_code) from es1
    where
    ((es1.rating >= 5 and es1.skill_code = 'SQL')
    or
    (es1.rating >= 6 and es1.skill_code = 'PLSQL')
    or
    (es1.rating >= 7 and es1.skill_code = 'JAVA')
    and es1.empno = e.empno)
    =3;

  • Mapping to the same table through multiple references

    Hello,
    I'm trying to map aggregate fields (from my target) to mutlitple tables (back on my source), but am hung up because I need multiple references to the same table (basically a lookup values table) in my target. I believe that the problem lies in not being able to reference (in the mapping workbench at least) the same table more than once (such as through an alias, or otherwise defining multiple references) in the multi-tables tab. Is there a way around this, or some other approach to mapping it that I can take?
    Thank you,
    John

    Not exactly sure what you are trying to do, perhaps listing the object model and data model would help.
    If you are having trouble mapping the aggregate in the Mapping Workbench you may be able to workaround the problem through using a descriptor or project amendment method and defining the mapping through the code API.

  • LookUp to the same table with multiple conditions

    Hi,
    I nead to do a lookup to the same table in the flow but with diffrent quieres, each query contains it's own 'where'.
    Can I do it somehow in one look up or do I have to use a few ?
    select a from table where a=1
    select b from table where c=3
    Thanks

    Hi,
      Using multiple lookups will be a cleaner approach. If you are using multiple lookups on the same table consider using Cache transform. Refer the below link for details on Cache transform
    Lookup and Cache Transforms in SQL Server Integration Services
    Alternatively if you want to go ahead with single look up , you may have to modify the SQL statement in the Lookup accordingly to return the proper value. In you case it may be
    select a,b from table where a=1 or c=3
    Note : Consider the above as a pseudo code. This needs to be tested and applied based on your requirement.
    Best Regards Sorna

  • Multiple DEVL_PROJECT_ID for the same model in CZ_DEVL_PROJECTS Table.

    Hi,
    Did anyone came across this issue!! I am triggering the config engine from back and and trying to pick up a model but it has multiple devl_project_id's. :(
    I could see multiple DEVL_PROJECT_ID for the same model in CZ_DEVL_PROJECTS Table. Apart from DEVL_PROJECT_ID and created_by_date every thing looks the same.
    Below are the 2 records the data from the table
    select * from CZ.CZ_DEVL_PROJECTS where product_key = '122:17056' and DEVL_PROJECT_ID in (12943,15321)
    DEVL_PROJECT_ID     NAME     ORIG_SYS_REF     CREATION_DATE     LAST_UPDATE_DATE     PERSISTENT_PROJECT_ID     MODEL_TYPE
    12943     ASI XXX MODEL(122 17056)     OPTIONAL:122:17056     12/18/2012 10:01     12/18/2012 10:01     11100     A
    PRODUCT_KEY     ORGANIZATION_ID     INVENTORY_ITEM_ID     BOM_CAPTION_RULE_ID     NONBOM_CAPTION_RULE_ID
    122:17056     122     17056     802     801
    15321     ASI XXX MODEL(122 17056)     OPTIONAL:122:17056     12/26/2012 23:10     12/26/2012 23:10     11100     A     
    122:17056     122     17056     802     801
    Thanks in advance,
    -vijay

    It can be possible to have multiple devl_project_id for the same model in cz_devl_projects Table.
    Case -1 : When you delete the existing model and re-import again.
    case 2:
    When you publish the model locally, it will create the same copy of the record with different devl_project_id, creation_date, last update_date etc..\
    You can see that for published records the last_updat_login, checkout_user will be null and for the original model, last_update_login,checkout_user (if model is locked) will not be null.
    -Murali
    Edited by: 907569 on Jan 8, 2013 10:55 AM

  • Load Data from a table on one server's database, to the same table structure in multiple server databases

    Hi,
    I have a situation where i have to load data from one server/database table to multiple servers/databases.
    Example:
    I need to load data from dbo.TABLE_A  (on Server: Server_A & Database: Database_A)  to the same table on the list of server databases like
    Server: Server_B , Database: Database_B
    Server: Server_C , Database: Database_C
    Server: Server_D , Database: Database_D
    Server: Server_E , Database: Database_E
    Server: Server_F , Database: Database_F
    Server: Server_G , Database: Database_G
    Server: Server_H , Database: Database_H
    so on and so forth on 250 such server database combinations.
    The table structure is the same on all the servers.
    If i make the source or destination dynamic, it throws an error while mapping ?
    I cannot get Linked server permissions and SQL Server Config thing doesn't work as well.
    Please suggest on how to load data from one source to multiple server/databases.
    Thank you.

    I just need to transfer one table's data. its like i have to use a query to pick data for
    the most recent data. So i use something like, select A, B, C, D from dbo.table where ETL_TIMESTAMP > (the max(etltimestamp) in the destination on different server). There are no foreign key relationships and the data should not be truncated. it just had
    to append the new records.

  • MULTIPLE UPDATES/INSERTIONS TO THE SAME TABLE

    How can I update/insert mutiple rows into the same table from one form ?

    Hi,
    Using the portal form on table you can insert only a single row. You can use master-detail form to insert multiple rows.
    Thanks,
    Sharmila

  • I want to create a mail merge for address labels into a table, but when I fill a table with merge fields, it ends up creating multiple entries for the same address, rather than one table full of each address.

    ...but when I fill a table with merge fields, it ends up creating multiple entries for the same address, rather than one table full of each address. Please help.
    Thanks!

    That is a quirk of Pagesthat  it applies only one record per page.
    There is a way around this:
    http://www.freeforum101.com/iworktipsntrick/viewtopic.php?t=245&highlight=labels &mforum=iworktipsntrick
    Peter

Maybe you are looking for