Solved

Update master record with rowcount

Posted on 2010-09-23
5
353 Views
Last Modified: 2012-06-27
(Optional Read) Business Objective:  Count the number of roommates living with the primary tenant/resident and update a field on the resident record.

(Optional Read) Background:  FYI, I’m comfortable with SQL select and update statements.   I have minimal experience with cursors, stored procedures etc.   (Of course) I use a test DB prior to applying to production environment.

(Must Read) The Primary Question for Experts:
Assuming the SQL statement provide below is on the right track, how should I modify the SQL statement to:
o      Accumulate/Count the quantity of roommates for each of the 10000 tenant records
o      Update the sfields9 field for each of the 10000 tenant records
I think I need to use a cursor (not confident).
Of course I will award points appropriately if my general direction is wrong and Expert(s) provide redirection.

SQL Statement: The statement below successfully updates the Tenant.sfields9 record with quantity of individuals living with the specified tenant “49546”.  (Note the “49546” filters the select and update to one tenant/resident.)   The successful solution should run without the where clause limiting to an individual tenant.  It should run for all tenants, subtotaling the quantity of roommates for each tenant, and updating the sfields9 field with said subtotal.

select p.Sfirstname + ' ' + p.ulastname, t.hmyperson
From person p left outer join room r on p.hmy = r.hmyperson
left outer join tenant t on t.hmyperson = r.hmytenant
where
p.hmy  in (49546) or R.hmytenant in (49546)
update tenant set sfields9 = (@@rowcount + 1)  where hmyperson = 49546

(Optional Read) Table.Field Details:
1.      Tenant.sfields9 = “Tenant” Table and  “sfields9” field holds the quantity of individuals living in apartment.
2.      “Room” table contains roommates id (xref to “Tenant” table)
3.      “Person” table contains name, address, etc for both the tenant and room tables

Experts-Exchange---Update-Primar.doc
0
Comment
Question by:LFreehauf
  • 3
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33744822
this should do;
update x
  set sfields9 = 1 + ( 
    select count(*)
      from  person p 
      left outer join room r 
       on p.hmy = r.hmyperson
      left outer join tenant t 
      on t.hmyperson = r.hmytenant
    where p.hmy =  x.hmytenant 
       or R.hmytenant = x.hmytenant 
  )  
 from tenant x

Open in new window

0
 

Author Comment

by:LFreehauf
ID: 33745095
Theory Looks correct.   However, Cut and paste created an error;  I'm happy to modify and further test after this "unexpected meeting".  Thanks for the fast response.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33746364
what error do you get?
0
 

Author Comment

by:LFreehauf
ID: 33746478
update x
It works!  Thanks.  
I had to change x.hmytenant to x.hmyperson in the where clause.  It updated (successful & accurate) on two tenant records.  Thanks again.  I appreciate it.

set sfields9 = (
    select count(*)
      from  person p
      left outer join room r
       on p.hmy = r.hmyperson
      left outer join tenant t
      on t.hmyperson = r.hmytenant
    where p.hmy =  x.hmyperson
       or R.hmytenant = x.hmyperson
  )  
 from tenant x where x.scode in ('t0036179', 't0003407')
0
 

Author Closing Comment

by:LFreehauf
ID: 33746493
Very good.  Very fast.  Expert follow up when I indicated a problem with exact syntax.  
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 48
SQL Server Designer 19 40
What is the proper way to use for criteria in left join? 7 26
SQL Syntax: How to force case sensitive query? 2 29
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question