Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-04-01 : 08:11:34
|
| Maha writes "HiI have the following query working fine.I want OWNER_TYPE = "A" to be updated in my #rps_125_temp table. If there r three rows with owner_type = 'A' its picking up the last row. I want it to pick up the first row. I can use min(owner.owner_id) which will pick the first row waht I want.That isOwner tableowner_id owner_name owner_type400 aaa A401 bbb A403 ccc A404 ddd P My existing query update the TEMP table with 403 ccc.I want 400 and aaa in my TEMP table and I MUST WANT owner_type = 'A'How can I incorporate the same here.Kindly help update #rps_125_temp set addl_owner_first_name = owner.owner_first_name, addl_owner_last_name = owner.owner_last_name, addl_owner_initial_name = owner.owner_initial_name, addl_owner_name_suffix = owner.owner_name_suffix from #swis_125_temp,owner,parcel_to_owner,#rps_125_temp where (#swis_125_temp.swis_co = parcel_to_owner.swis_co and #swis_125_temp.swis_town = parcel_to_owner.swis_town and #swis_125_temp.swis_vg = parcel_to_owner.swis_vg and #swis_125_temp.roll_yr = parcel_to_owner.roll_yr) and (#rps_125_temp.swis_co = parcel_to_owner.swis_co and #rps_125_temp.swis_town = parcel_to_owner.swis_town and #rps_125_temp.swis_vg = parcel_to_owner.swis_vg and #rps_125_temp.parcel_id = parcel_to_owner.parcel_id) and (owner.swis_co = parcel_to_owner.swis_co and owner.swis_town = parcel_to_owner.swis_town and owner.swis_vg = parcel_to_owner.swis_vg and owner.parcel_id = parcel_to_owner.parcel_id and owner.owner_id = parcel_to_owner.owner_id and owner.owner_type = 'A') and (year(parcel_to_owner.sale_date) = #swis_125_temp.roll_yr and parcel_to_owner.book = '')" |
|
|
rfrancisco
Yak Posting Veteran
95 Posts |
Posted - 2005-04-02 : 22:53:44
|
| Try this one. I've re-written your query to make use of INNER JOINs to join your tables. To get the minimum owner.owner_id where owner_type = 'A', look at the ones in red.UPDATE A SETaddl_owner_first_name = C.owner_first_name,addl_owner_last_name = C.owner_last_name,addl_owner_initial_name = C.owner_initial_name,addl_owner_name_suffix = C.owner_name_suffix FROM #rps_125_temp A INNER JOIN parcel_to_owner BON A.swis_co = B.swis_co ANDA.swis_town = B.swis_town ANDA.swis_vg = B.swis_vg ANDA.parcel_id = B.parcel_id ANDB.book = '' INNER JOIN owner CON B.swis_co = C.swis_co ANDB.swis_town = C.swis_town ANDB.swis_vg = C.swis_vg ANDB.parcel_id = C.parcel_id ANDB.owner_id = C.owner_id ANDC.owner_type = 'A' ANDC.owner_id = (SELECT MIN(owner_id) FROM owner EWHERE E.owner_type = 'A')INNER JOIN #swis_125_temp DON B.swis_co = D.swis_co ANDB.swis_town = D.swis_town ANDB.swis_vg = D.swis_vg ANDB.roll_yr = D.roll_yr ANDyear(B.sale_date) = D.roll_yr |
 |
|
|
|
|
|