Need help with Update Join Query

Hello, I am trying to update PID of #child table with PID of #parent table if "lastname & firstname are matches in both table" but my update query is giving some error. Please help and correct the update query. I am also trying to remove any
blank space from starting and ending.
drop table #parent,#child
create table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan  ','  Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali   ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Update #child
set PID = p.PID
from #child C Join
#parent p ON c.LTRIM(RTRIM(lastname) = p.LTRIM(RTRIM(lastname)
AND c.LTRIM(RTRIM(firstname) = p.LTRIM(RTRIM(firstname)
/* Requested Output */
PID        lastname      firstname
100        Josheph       Sumali
600        Mursan        Terry

create table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan ',' Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Merge #child as t
Using #parent as p ON (LTRIM(RTRIM(t.lastname)) = LTRIM(RTRIM(p.lastname))
AND LTRIM(RTRIM(t.firstname)) = LTRIM(RTRIM(p.firstname)) )
When Matched Then
Update
set PID = p.PID;
update #child
Set lastname=LTRIM(RTRIM(lastname)), firstname= LTRIM(RTRIM(firstname));
update #parent
Set lastname=LTRIM(RTRIM(lastname)), firstname = LTRIM(RTRIM(firstname));
select * from #child
select * from #parent
drop table #parent,#child

Similar Messages

  • Need help with self join query

    Hello,
    I have table A with the following data
    oid parent_oid
    10 4
    4 2
    2 2
    12 6
    6 6
    parent_oid is the parent of oid. I'd like a query that shows the final parent of the oid. The result should show the following
    oid final parent
    10 2
    4 2
    2 2
    12 6
    6 6
    I'm using Oracle 10g. I'm familiar with self joins, but that alone will not do the job. Thanks!

    Hi,
    arizona9952 wrote:
    ... I'm familiar with self joins, but that alone will not do the job.You're absolutely right!
    A 2-way self join would work for rows have no parent, or rows that are directly connected to their final ancestor (such as oid=4), but not for anything farther away.
    A 3-way self-join would work for one more level away from the final row, but no more. That would be enough for the small set of sample data that you posted, but it would not work if you added a new row with parent_id=10.
    An N-way self-join would work for up to N+1 levels, but no more.
    You need something that can go any number of levels, such as CONNECT BY:
    SELECT     CONNECT_BY_ROOT oid     AS oid
    ,     parent_oid          AS final_parent
    FROM     a
    WHERE     CONNECT_BY_ISLEAF     = 1
    CONNECT BY     oid     = PRIOR parent_oid
         AND     oid     != parent_oid
    ;Edited by: Frank Kulash on Feb 22, 2010 7:09 PM
    Upon sober reflection, I think that a Top-Down query, like the one below, would be more efficient than a Bottom-Up query, like the one above:
    SELECT     oid
    ,     CONNECT_BY_ROOT     parent_oid     AS final_parent
    FROM     a
    START WITH     parent_oid     = oid
    CONNECT BY     parent_oid     = PRIOR oid
         AND     oid          != PRIOR oid
    ;

  • Need help with a JOIN query

    Hi,
    I am looking for the following result:
    PROVIDER_ID     SPECIALTY     BUCKET     CODE     RATING     FEE
         1     FP                 EM     100     9     20
         1     FP                 SP     300     15     0
         1     INFUS                 EM     100     3     20
         1     INFUS                 EM     200     6     15The base tables are provided below in the with clause. What I am trying to do is, where "code" matches show the fee from t1 else show 0.
    I tried a few variotions but just can't seem to get it right.
    with t1 as
    select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
    select 1 as provider_id, 200 as code, 15 as fee, 30 as default_multiplier from dual
    , t2 as
    select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
    select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
    select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
    select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
    SELECT   t1.provider_id
           , t2.specialty
           , t2.bucket
           , t2.code
           , t2.rating
           , t1.fee
    FROM     t1, t2
    ORDER BY 1, 2, 3Any help will be appreciated.
    Thanks.

    Could I possibly add one more twist to this.
    The current result:
    PROVIDER_ID SPECI BU       CODE     RATING        FEE
              1 FP    EM        100          9         20
              1 FP    SP        300         15          0
              1 INFUS EM        100          3         20
              1 INFUS EM        200          6         75
              2 FP    EM        100          9         40
              2 FP    SP        300         15          0
              2 INFUS EM        100          3         40
              2 INFUS EM        200          6          0
              3 FP    EM        100          9          0
              3 FP    SP        300         15          0
              3 INFUS EM        100          3          0
              3 INFUS EM        200          6         60The current code:
    with t1 as
    select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
    select 1 as provider_id, 200 as code, 75 as fee, 10 as default_multiplier from dual union all
    select 1 as provider_id, 500 as code, 75 as fee, 10 as default_multiplier from dual union all
    select 2 as provider_id, 100 as code, 40 as fee, 20 as default_multiplier from dual union all
    select 3 as provider_id, 200 as code, 60 as fee, 30 as default_multiplier from dual
    , t2 as
    select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
    select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
    select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
    select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
    , t3 as
    SELECT   t1.provider_id
           , t2.specialty
           , t2.bucket
           , t2.code
           , t2.rating
           , CASE t1.code
                WHEN t2.code
                   THEN t1.fee
                ELSE 0
             END AS fee
           , RANK () OVER (PARTITION BY t1.provider_id, t2.specialty, t2.bucket, t2.code ORDER BY CASE t1.code
                 WHEN t2.code
                    THEN t1.fee
                 ELSE 0
              END DESC) AS the_rank
        FROM t1
           , t2
    SELECT DISTINCT provider_id
                  , specialty
                  , bucket
                  , code
                  , rating
                  , fee
               FROM t3
              WHERE the_rank = 1
           ORDER BY 1
                  , 2
                  , 3
                  , 4I added the code 500 to t1. Howevere, I don't want this code to show up in the result becuase it is does not exist in t2.
    I also added code 200 to t1, to see if that shows properly.
    I had to do the rank, becuase I need the proper count of rows.
    In reality the row counts are:
    t1: 20 Million
    t2: 10 Thousand.
    So, I would like to avoid doing DISTINCT and ANALYTIC functions on this large set, so any better way of achiving the same result, with a better statement.
    Thanks.

  • HT201541 I need help with updating my browser.

    I need help with updating my browser.  Its telling me that Safari is outdated.

    That is a very old version belonging to Snow Leopard. I take it you are not actually running Yosemite.
    Upgrading to Yosemite
    You can upgrade to Yosemite from Lion or directly from Snow Leopard. Yosemite can be downloaded from the Mac App Store for FREE.
    Upgrading to Yosemite
    To upgrade to Yosemite you must have Snow Leopard 10.6.8 or Lion installed. Download Yosemite from the App Store. Sign in using your Apple ID. Yosemite is free. The file is quite large, over 5 GBs, so allow some time to download. It would be preferable to use Ethernet because it is nearly four times faster than wireless.
        OS X Mavericks/Yosemite- System Requirements
          Macs that can be upgraded to OS X Yosemite
             1. iMac (Mid 2007 or newer) - Model Identifier 7,1 or later
             2. MacBook (Late 2008 Aluminum, or Early 2009 or newer) - Model Identifier 5,1 or later
             3. MacBook Pro (Mid/Late 2007 or newer) - Model Identifier 3,1 or later
             4. MacBook Air (Late 2008 or newer) - Model Identifier 2,1 or later
             5. Mac mini (Early 2009 or newer) - Model Identifier 3,1 or later
             6. Mac Pro (Early 2008 or newer) - Model Identifier 3,1 or later
             7. Xserve (Early 2009) - Model Identifier 3,1 or later
    To find the model identifier open System Profiler in the Utilities folder. It's displayed in the panel on the right.
         Are my applications compatible?
             See App Compatibility Table - RoaringApps.
    Upgrading to Lion
    If your computer does not meet the requirements to install Mavericks, it may still meet the requirements to install Lion.
    You can purchase Lion at the Online Apple Store. The cost is $19.99 (as it was before) plus tax.  It's a download. You will get an email containing a redemption code that you then use at the Mac App Store to download Lion. Save a copy of that installer to your Downloads folder because the installer deletes itself at the end of the installation.
         Lion System Requirements
           1. Mac computer with an Intel Core 2 Duo, Core i3, Core i5, Core i7,
               or Xeon processor
           2. 2GB of memory
           3. OS X v10.6.6 or later (v10.6.8 recommended)
           4. 7GB of available space
           5. Some features require an Apple ID; terms apply.

  • I need help with a SELECT query - help!

    Hello, I need help with a select statement.
    I have a table with 2 fields as shown below
    Name | Type
    John | 1
    John | 2
    John | 3
    Paul | 1
    Paul | 2
    Paul | 3
    Mark | 1
    Mark | 2
    I need a query that returns everything where the name has type 1 or 2 but not type 3. So in the example above the qery should bring back all the "Mark" records.
    Thanks,
    Ian

    Or, if the types are sequential from 1 upwards you could simply do:-
    SQL> create table t as
      2  select 'John' as name, 1 as type from dual union
      3  select 'John',2 from dual union
      4  select 'John',3 from dual union
      5  select 'Paul',1 from dual union
      6  select 'Paul',2 from dual union
      7  select 'Paul',3 from dual union
      8  select 'Paul',4 from dual union
      9  select 'Mark',1 from dual union
    10  select 'Mark',2 from dual;
    Table created.
    SQL> select name
      2  from t
      3  group by name
      4  having count(*) <= 2;
    NAME
    Mark
    SQL>Or another alternative if they aren't sequential:
    SQL> ed
    Wrote file afiedt.buf
      1  select name from (
      2    select name, max(type) t
      3    from t
      4    group by name
      5    )
      6* where t < 3
    SQL> /
    NAME
    Mark
    SQL>Message was edited by:
    blushadow

  • Need help with writing a query with dynamic FROM clause

    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'Simply sticking the later query in the first query does not work.
    Any pointers will be appreciated.
    Thanks
    rogers42

    Hi,
    rogers42 wrote:
    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'
    Remember that anything inside quotes is case-sensitive. Is the owner really "Schema_Name" with a capital S and a capital N, and 8 lower-case letters?
    Simply sticking the later query in the first query does not work.Right; the table name must be given when you compile the query. It's not an expression that you can generate in the query itself.
    Any pointers will be appreciated.In SQL*Plus, you can do something like the query bleow.
    Say you want to count the rows in scott.emp, but you're not certain that the name is emp; it could be emp_2011 or emp_august, or anything else that starts with e. (And the name could change every day, so you can't just look it up now and hard-code it in a query that you want to run in the future.)
    Typically, how dynamic SQL works is that some code (such as a preliminary query) gets some of the information you need to write the query first, and you use that information in a SQL statement that is compiled and run after that. For example:
    -- Preliminary Query:
    COLUMN     my_table_name_col     NEW_VALUE my_table_name
    SELECT     table_name     AS my_table_name_col
    FROM     all_tables
    WHERE     owner          = 'SCOTT'
    AND     table_name     LIKE 'E%';
    -- Main Query:
    SELECT     COUNT (*)     AS cnt
    FROM     scott.&my_table_name
    ;This assumes that the preliminary query will find exactly one row; that is, it assumes that SCOTT has exactly one table whose name starts with E. Could you have 0 tables in the schema, or more than 1? If so, what results would you want? Give a concrete example, preferably suing commonly available tables (like those in the SCOTT schema) so that the poepl who want to help you can re-create the problem and test their ideas.
    Edited by: Frank Kulash on Aug 11, 2011 2:30 PM

  • MAC OS 10.6.8 Using Safari 5.1 Needing Help With Updating Adobe Flash

    Hello-
    Recently Adobe Flash installed an icon in my System Preferences and when I click the icon there are several tabs.  One allows updates to check automatically.  It says it will install the latest version of Flash without having to remove the previous version, but I have never been told that a newer version is available unless I clcik "check now."  although "Check for updates automatically" is checked, I do not know where it tells me a newer version is available.  Also, there has been 5 "newer versions" available since this icon was installed in System Prefeences, I have only found out about the newer versions when I clicked "Check Now."  If I read that with this new way to install the latest Mac version of Flash does not require me to uninstall the previous version of Flash how do I install the most recent version of Flash?  Does it come in an email?
    Since it is not telling me that there is a more recent version of Flash unless I click "Check Now" should I still do it the way I used to install the latest version of flash?  (The way before this icon was added to System Preferences)?
    I would appreciate help with updating Flash to the most recent version.  I used to uninstall the older version, restart the browser and download the most recent version and install it.  It always worked find but this was before there was an Adobe icon in my system Preferences.

    Hello,
    The way you used to do this (uninstall, restart the browser, etc.) is still definitely valid and probably the "safest" way to get a new version installed.  Uninstalling shouldn't be required, but it definitely doesn't hurt.  If you feel comfortable doing it the old way, please feel free to continue using that process.
    As for notifications, this is a bit trickier, but in general you should be notified within 30 days (or so) after a new player is released.  This usually occurs when the browser loads swf content.  Clicking the button will, like you mention, immediately check.  Another alternative to finding out when a new player is released is to subscribe to our Flash Player Releases feed.
    Is there a way to be automatically notified when a new Flash Runtime release is made?
    Thanks,
    Chris

  • Help with Inner Join query in SQL

    Hope someone can help with this, as this is quite an involved project for me, and I'm just about there with it.
    My table structure is :
    table_packages
    packageID
    package
    packageDetails
    etc
    table_destinations
    destinationID
    destination
    destinationDetails
    etc
    table_packagedestinations
    packageID
    destinationID
    ..so nothing that complicated.
    So the idea is that I can have a package details page which shows the details of the package, along any destinations linked to that package via the packagedestinations table.
    So this is the last part really - to get the page to display the destinations, but I'm missing something along the way....
    This is the PHP from the header, including my INNER JOIN query....
    PHP Code:
    <?php
    $ParampackageID_WADApackages = "-1";
    if (isset($_GET['packageID'])) {
      $ParampackageID_WADApackages = (get_magic_quotes_gpc()) ? $_GET['packageID'] : addslashes($_GET['packageID']);
    $ParamSessionpackageID_WADApackages = "-1";
    if (isset($_SESSION['WADA_Insert_packages'])) {
      $ParamSessionpackageID_WADApackages = (get_magic_quotes_gpc()) ? $_SESSION['WADA_Insert_packages'] : addslashes($_SESSION['WADA_Insert_packages']);
    $ParampackageID2_WADApackages = "-1";
    if (isset($_GET['packageID'])) {
      $ParampackageID2_WADApackages = (get_magic_quotes_gpc()) ? $_GET['packageID'] : addslashes($_GET['packageID']);
    mysql_select_db($database_connPackages, $connPackages);
    $query_WADApackages = sprintf("SELECT packageID, package, costperpax, duration, baselocation, category, dateadded, agerange, hotel, educational_tours, field_trips, corporate_outings, plant_visits, budget_package, rollingtours, teambuilding, description, offer FROM packages WHERE packageID = %s OR ( -1= %s AND packageID= %s)", GetSQLValueString($ParampackageID_WADApackages, "int"),GetSQLValueString($ParampackageID2_WADApackages, "int"),GetSQLValueString($ParamSessionpackageID_WADApackages, "int"));
    $WADApackages = mysql_query($query_WADApackages, $connPackages) or die(mysql_error());
    $row_WADApackages = mysql_fetch_assoc($WADApackages);
    $totalRows_WADApackages = mysql_num_rows($WADApackages);
    $colname_educationalDestinations = "1";
    if (isset($_GET['PackageID'])) {
      $colname_educationalDestinations = (get_magic_quotes_gpc()) ? packageID : addslashes(packageID);
    mysql_select_db($database_connPackages, $connPackages);
    $query_educationalDestinations = sprintf("SELECT * FROM destinations INNER JOIN (packages INNER JOIN packagedestinations ON packages.packageID = packagedestinations.packageID) ON destinations.destinationID = packagedestinations.destinationID WHERE packages.packageID = %s ORDER BY destination ASC", GetSQLValueString($colname_educationalDestinations, "int"));
    $educationalDestinations = mysql_query($query_educationalDestinations, $connPackages) or die(mysql_error());
    $row_educationalDestinations = mysql_fetch_assoc($educationalDestinations);
    $totalRows_educationalDestinations = mysql_num_rows($educationalDestinations);
    ?>
    And where I'm trying to display the destinations themselves, I have : 
    <table>
            <tr>
                     <td>Destinations :</td>
                </tr>
               <?php do { ?>
            <tr>
                 <td><?php echo $row_educationalDestinations['destination']; ?></td>
            </tr>
            <?php } while ($row_educationalDestinations = mysql_fetch_assoc($educationalDestinations)); ?>
    </table>
    If anyone could have a quick look and help me out that would be much appreciated - not sure if its my SQL at the top, or the PHP in the page, but either way it would be good to get it working. 
    Thanks.

    First off, you need to get the database tables so that there is a relationship between them.
    In fact, if there is a one to one relationship, then it may be better to store all of your information in one table such as
    table_packages
    packageID
    package
    packageDetails
    destination
    destinationDetails
    etc
    If there is a one to many relationship, then the following would be true
    packages
    packageID
    package
    packageDetails
    etc
    destinations
    destinationID
    packageID
    destination
    destinationDetails
    etc
    The above assumes that there are many destinations to one package with the relationship coloured orange.
    Once you have the above correct you can apply your query as follows
    SELECT *
    FROM packages
    INNER JOIN destinations
    ON packages.packageID = destinations.packageID
    WHERE packages.packageID = %s
    ORDER BY destination ASC
    The above query will show all packages with relevant destinations

  • Need help with LikeFilter for querying the keyset instead of value

    Hi,
    I'm looking for help with the LikeFilter.
    I need to query the cache to get all entries with key starting with a particular string.
    I could see samples using LikeFilter for querying the values in the cache but not the keyset.
    Can someone help?
    E.g:
    Cache Entries:
    abc123 - value1
    abc234 - value2
    bcd123 - value3
    I want to get all entries with key starting with 'abc'.
    thanks,
    rama.

    NJ, thanks for the quick reply.
    I tried something similar (as below) but this code gives me 'java.lang.NoClassDefFoundError: com/tangosol/util/ValueExtractor'.
    KeyExtractor extractor = new KeyExtractor("getKey");
    Filter filter = new LikeFilter(extractor, id+":%",'-',false);
    -rama.
    Edited by: 911950 on Feb 2, 2012 1:18 PM

  • I need help with updating my Ipod 5

    So i am updating my ipid 5 to IOS 7.0.3. I am doing it through Itunes on my computor. I have sucsessfully updated the ipod, and am now in the process of setting up the ipod after the update. I am connected to wif-fi, but for the past 20 hours all the ipod has said was "restoring ICloud settings". I need help! i dont know what to do
    Thanks!

    Try:
    - Reset the iOS device. Nothing will be lost
    Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - The try again.

  • Need help with the following query

    I have the following data
    INVOICE_ID PAID_AMT PYMNT_GROSS_AMT DISCOUNT
    10151 1375 1375 55
    10151DC -44.81 -44.81 0
    20017 25 25 7
    Ok the data I want to grab the discount field is paid_amt = pymnt_gross_amt and discount <> 0. If however there is another invoice_id out there (same invoice ID except with a DC appended to it like 10151DC) then you want to select the differece between the discount field without the DC and the Invoice ID with the DC the paid_amt field.
    So the results would be.
    Invoice_ID Discount
    10151 10.19
    20017 7
    Thanks for all your help!

    Why scan same table twice when it can be achived using single scan :
    Hope this helps :
    with inv_data as
    (select '10151' invoice_id,
    1375 paid_amt,
              1375 pymnt_gross_amt,
              55 discount from dual
    union all
    select '10151DC' invoice_id,
    -44.81 paid_amt,
              -44.81 pymnt_gross_amt,
              0 discount from dual
    union all
    select '20017' invoice_id,
    25 paid_amt,
              25 pymnt_gross_amt,
              7 discount from dual)
    select replace(invoice_id,'DC') invoice_id,
    sum(case when invoice_id like '%DC' then pymnt_gross_amt
         else discount end) discount
    from inv_data
    group by replace(invoice_id,'DC')
    INVOICE DISCOUNT
    10151 10.19
    20017 7
    2 rows selected.

  • Need help with update statement with multiple joins

    I've got the following select statement that is pulling 29 records:
    SELECT
    PPA.PROJECT_ID,
    PPA.SEGMENT1,
    peia.expenditure_item_id,
    peia.expenditure_type,
    pec.expenditure_comment
    FROM PA.PA_PROJECTS_ALL PPA,
    pa.pa_expenditure_items_all peia,
    pa.pa_expenditure_comments pec
    where PPA.segment1 < '2008' and
    PPA.project_id = 52 and -- just run for project # 20077119 for testing
    peia.expenditure_type = 'PAYROLL' and
    peia.project_id = ppa.project_id and
    PEC.EXPENDITURE_ITEM_ID = PEIA.EXPENDITURE_ITEM_ID;
    I need to update the pec.expenditure_comments to a static field for those 29 records. I assume I should start with the following, but not sure how to complete the where:
    update
    pa.pa_expenditure_comments pec
    set pec.expenditure_comment = 'REFERENCE HD#728'
    where
    First time that we've ever needed to update, so any and all help appreciated.

    Try using exists:
    update pa.pa_expenditure_comments pec
    set    pec.expenditure_comment = 'REFERENCE HD#728'
    where exists ( select null
                   from   pa.pa_projects_all ppa
                   ,      pa.pa_expenditure_items_all peia
                   ,      pa.pa_expenditure_comments pec2
                   where  ppa.segment1 < ''    -- not sure what you posted here, so for next time:
                                               -- please put your examples between the code tags.
                   and    ppa.project_id = 52  -- just run for project # 20077119 for testing
                   and    peia.expenditure_type = 'PAYROLL'
                   and    peia.project_id = ppa.project_id
                   and    pec2.expenditure_item_id = peia.expenditure_item_id
                   and    pec2.expenditure_item_id = pec.expenditure_item_id
                 );

  • Need help with update query

    I am having a strange problem with an update query I am running. Here are the specifics:
    1. I run a script that extracts qualifying rows form a source table (S1), performs some calculation,s and stores the results in a temporary table (T1).
    2. The calculations stored in the temporary table (T1) are only for a subset of rows in the source table (S1).
    3. The temporary table (T1) uses the same primary key values as the source table (T1).
    4. Once the calculations are completed, I want to update a single column on the source table (S1.CalcValue) with the calculated value from the temporary table (T1.CalcValue).
    The problem is that I am doing monthly updates so I run month 1, do some verification of the data and then update the source table. Then repeat the process for months 2 through n. When I run the update for the month 2 data, the prior month 1 data for the column is lost. Below is the update script which looks like it should work and only update on the matching keys between the temporary table (T1) and the source table (S1). I was wondering if anyone could let me know what is wrong with this script and why.
    I am new to Oracle having worked extensively in SQL Server, so the syntax differences are killing me right now so any help would be appreciated.
    Update script:
    procedure update_rvu AS
    BEGIN
    --update the test.RVU table
    update test.RVU S1
    set S1.CalcRVU = ( select
    T1.CalcRVU
    from
    TMP_RVU T1
    where
    S1.GroupId = T1.GroupId
    and
    S1.PatientId = T1.PatientId
    and
    S1.InvoiceId = T1.InvoiceId
    and
    S1.TransId = T1.TransId
    commit;
    END update_rvu;
    Edited by: user9009311 on Apr 14, 2010 4:47 PM

    Most likely you want a WHERE clause in your update portion ... something like
    update test.RVU S1
    set S1.CalcRVU = ( select
    T1.CalcRVU
    from
    TMP_RVU T1
    where
    S1.GroupId = T1.GroupId
    and
    S1.PatientId = T1.PatientId
    and
    S1.InvoiceId = T1.InvoiceId
    and
    S1.TransId = T1.TransId
    where exists
       select null
       from tmp_rvu t11
       where S1.GroupId = T11.GroupId
       and    S1.PatientId = T11.PatientId
       and    S1.InvoiceId = T11.InvoiceId
       and    S1.TransId = T11.TransId
    )You can also look into using the MERGE command (if you're on version 10 or better you can perform update only operations with it). I personally find it more 'friendly' than correlated updates a lot of the time.

  • Need help with outer joins

    I have the following table structure,
    Table - 1_
    ID | Information
    1 | abcadskasasa
    2 | asdasdasdasd
    3 | saeqdfdvsfcsc
    Table - 2_
    ID | PID
    1 | 12
    1 | 13
    2 | 14
    1 | 15
    1 | 16
    2 | 12
    Table - 3_
    ID | PARID
    1 | 12
    2 | 14
    1 | 15
    Now I want to select for each ID in table 1, the count of number of PID from table 2 and count of number of PARID from table 3.
    Desired output:_
    ID | COUNT_PID | COUNT_PARID
    1 | 4 | 2
    2 | 2 | 1
    3 | 0 | 0
    Could anyone please help me out with this. I am trying to make use of outer joins, but as I work mostly on the front end so, not able to come up with a proper solution for the above.
    Thanks in advance,
    Tejas

    Hi, Tejas,
    You might have been doing the outer join correctly.
    There's another problem here: joining table_1 to two other tables with which it has a one-to-many relationship.
    If you were joining table_1 to just one other table, you could say:
    SELECT       t1.id
    ,       COUNT (t2.pid)     AS count_pid
    FROM              table_1     t1
    LEFT OUTER JOIN  table_2     t2     ON     t1.id     = t2.id
    GROUP BY  t1.id
    ORDER BY  t1.id;You could have done the exact same thing with table_3 instead of table_2.
    But you can't do the same thing with both table_2 and table_3 at the same time: that would be like cross-joining table_2 and table_3. Instead of showing id=1 having count_pid=4 and count_parid=2, you would get cout_pid=8 and count_parid=8 (since 8 = 4 * 2).
    You can do a separate GROUP BY on (at least) one of the tables.
    This gets the right results. In the main query, there is only one one-to-many relationship.
    WITH  t3_summary     AS
         SELECT    id
         ,       COUNT (parid)     AS count_parid
         FROM       table_3
         GROUP BY  id
    SELECT       t1.id
    ,       COUNT (t2.pid)     AS count_pid
    ,       MAX (t3.count_parid)     AS count_parid
    FROM              table_1     t1
    LEFT OUTER JOIN  table_2     t2     ON     t1.id     = t2.id
    LEFT OUTER JOIN      t3_summary     t3     ON     t1.id     = t3.id
    GROUP BY  t1.id
    ORDER BY  t1.id;

  • Need help with inner join and distinct rows

    Hey Guys,
    i have
    1) BaseEnv Table 
    2) Link Table
    3) BaseData Table
    Link table has three columns Id,BaseEnvId,BaseDataId 
    the BaseEnvID is unique in the table where as BaseDataId can be repeated i.e multile rows of BaseEnv Table can point to same BaseData  table row
    Now i want to do  BaseEnvTable inner join Link Table inner join BaseData Table and select 5 columsn ; Name,SyncName,Version,PPO,DOM  from the BaseData table.. the problem is that after i do the inner join I get duplciate records..
    i want to eliminate the duplicate records , can any one help me here

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. Now we have to guess and type, guess and type, etc. because of your bad manners. 
    CREATE TABLE Base_Env
    (base_env_id CHAR(10) NOT NULL PRIMARY KEY,
    Think about the name Base_Data; do you have lots of tables without data? Silly, unh? 
    CREATE TABLE Base_Data
    (base_data_id CHAR(10) NOT NULL PRIMARY KEY,
    Your Links table is wrong in concept and implementation. The term “link” refers to a pointer chain structure used in network databases and makes no sense in RDBMS. There is no generic, magic, universal “id” in RDBMS! People that do this are called “id-iots”
    in SQL slang. 
    We can model a particular relationship in a table by referencing the keys in other tables. But we need to know if the relationship is 1:1, 1:m, or n:m. This is the membership of the relationship. Your narrative implies this: 
    CREATE TABLE Links
    (base_env_id CHAR(10) NOT NULL UNIQUE
       REFERENCES Base_Env (base_env_id),
     base_data_id CHAR(10) NOT NULL
       REFERENCES Base_Data (base_data_id));
    >> The base_env_id is unique in the table where as base_data_id can be repeated I.e multiple rows of Base_Env Table can point [sic] to same Base_Data table row. <<
    Again, RDBMS has no pointers! We have referenced an referencing tables. This is a fundamental concept. 
    That narrative you posted has no ON clauses! And the narrative is also wrong. There is no generic “name”, etc. What tables were used in your non-query? Replace the ?? in this skeleton: 
    SELECT ??.something_name, ??.sync_name, ??.something_version, 
           ??.ppo, ??.dom
    FROM Base_Env AS E, Links AS L, Base_Data AS D
    WHERE ?????????;
    >> I want to eliminate the duplicate records [sic], can any one help me here?<<
    Where is the sample data? Where is the results? Please read a book on RDBMS so you can post correct SQL and try again. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

Maybe you are looking for

  • Is there a good anti virus/diagnostic for iMac

    My iMac is doing many weird things.   With out my clicking any thing, the stupid mackeeper web page opens and freezes my computer. Also, my homepage will not stay as I change it. I NEVER changed it to start with but now when I start up Safari, this c

  • Mass reallocation of payment methods in APP

    How to make a mass reallocation of payment methods in APP?

  • New Jersey Unemployment (SUI) Form - Family Leave Insurance

    Hi guys - New Jersey Implemented the Family Leave Insurance tax last year (tax types 87 and 88). We are currently withholding this tax from employees. However, our New Jersey Unemployment Form (HR_F_UNEMP_NJ) does not have a line item for this tax. O

  • WD Smartware is mounting unknown disks

    I have a WD Passport. It used to do this annoying thing, where in addition to the actual drive, it would load a virtual vcd drive containing the smartware software too. (This was common to a lot of WD products). To get rid of this, I installed the WD

  • Fault in invoke webservice method

    Hi! I've created WS (J2EE 1.4) for PL\SQL package using JDEV 10g. It seem run ok I have accessed in browser to http://XXX.XXX.XXX.XXX:8988/Application1-Project1-context-root/Test The methods are run well in web form. Then i've, created simple soap cl