Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Table Without Reordering Values

Status
Not open for further replies.

trenttc

Technical User
Feb 25, 2002
68
US
T1 has fields code,type1,type2,type3. T1.code is unique.
T2 has fields code,type. T2.code is not unique; there are from 1 to 3 T2.type values. T1 type1-type3 fields are updated, IN T2.type ORDER, based on a match between the T1.code and T2.code. I can update T1 from T2 using Select Count(*) (AS RECNO) but there is a problem. Count(*) REORDERS T2.type in numeric order. Is there a way to use Count(*) that doesn't reorder the values when it counts them?

T1 T2 RECNO RECNO
code type1 type2 type3 code type (actual) (desired)
1234 4 1 1234 4 2 1
2468 9 3 6 1234 1 1 2
2468 9 3 1
2468 3 1 2
2468 6 2 3
UPDATE T1 SET
TYPE1=
CASE WHEN Q.RECNO=1
THEN Q.TYPE ELSE T1.TYPE1 END,
TYPE2 =
CASE WHEN Q.RECNO=2
THEN Q.TYPE ELSE T1.TYPE2 END,
TYPE3 =
CASE WHEN Q.RECNO=3
THEN Q.TYPE ELSE T1.TYPE3 END

FROM T1 TBL1
INNER JOIN
(SELECT CODE, TYPE,
(SELECT COUNT(*) FROM T2
WHERE CODE = TBL2.CODE
AND TYPE <= TBL2.TYPE) AS RECNO
FROM T2 TBL2) AS Q

ON T1.CODE = Q.CODE

Many thanks to tlbroadbent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top