Home > database > This code execution result is wrong, how is the solution
This code execution result is wrong, how is the solution
Time:05-15
- build a data table @ t, actually has a 120 w record DECLARE @ t TABLE (mids INT IDENTITY (1, 1), INT, m1 m2 INT, m3 INT, m4 INT, INT m5) INSERT the @ t select '1', '2', '3', '4', '5' Union all select '6', '7', '8', '9', '10' Union all select '11', '12' and '13', '14' and '15' Union all select '16', '17', '18', 'the', '20'
- build a data table @ a, there are 3000 records DECLARE @ a TABLE (Nid INT IDENTITY (1, 1), n1 INT, n2 INT, n3 INT, n4 interchange INT, n5 INT) Insert @ a select '1', '12', '13', '14' and '15' Union all select '2', '12' and '13', '14' and '15' Union all select '3', '4', '13', '14' and '15' Union all select '4', '6', '8', '14' and '15' Union all select '5', '7', '9', '10', '15' Union all select '16', '17', '18', 'the', '20'
-- -- -- -- -- choose @ t to each row and @ each line in a field by comparing the results SELECT mid1 mids, m1, m2, m3, m4, m5, (R0)=SUM (CASE WHEN counts=0 THEN 1 ELSE 0 END), (R1)=SUM (CASE WHEN counts=1 THEN 1 ELSE 0 END), (R2)=SUM (CASE WHEN counts=2 THEN one ELSE 0 END), (R3)=SUM (CASE WHEN counts=3 THEN 1 ELSE 0 END), (R4)=SUM (CASE WHEN counts=4 THEN 1 ELSE 0 END), (R5)=SUM (CASE WHEN counts=5 END THEN 1 ELSE 0 ) The FROM ( B. SELECT *, arjun id mid1, m1, m2, m3, m4, m5, Case the when n1 in (Select from m1 @ a) then one else 0 end + (Case when n2 in the from (Select m2 @ a) then one else 0 end) + (Case when n3 (Select m3 in the from @ a) then one else 0 end) + (Case when n4 interchange in (Select the m4 from @ a) then one else 0 end) + (Case when n5 in (Select the m5 from @ a) then one else 0 end) Counts The FROM @ t a, @ a b ) counts GROUP BY mid1, m1, m2, m3, m4, m5 The ORDER BY mid1