select ID,Item1,Item2,Item3,Item4
into #Temp
from (
select ID='200910',Item1='A',Item2='B',Item3='C',Item4='T1'
union all
select ID='200910',Item1='',Item2='B',Item3='C' ,Item4='G2'
union all
select ID='200910',Item1='A',Item2='D',Item3='C' ,Item4='T3'
union all
select ID='200910',Item1='A',Item2='F',Item3='C' ,Item4='H4'
union all
select ID='200911',Item1='A',Item2='D',Item3='C' ,Item4='T5'
union all
select ID='200911',Item1='A',Item2='',Item3='E' ,Item4='T6'
union all
select ID='200911',Item1='',Item2='',Item3='C' ,Item4='T7'
) Tab
SELECT A.*
into #T1
from #Temp AS A
LEFT JOIN #Temp AS B
ON A.ID = B.ID
AND CASE WHEN ISNULL(NULLIF(A.Item1,''),'') = '' THEN 1 ELSE 0 END
+ CASE WHEN ISNULL(NULLIF(A.Item2,''),'') = '' THEN 1 ELSE 0 END
+ CASE WHEN ISNULL(NULLIF(A.Item3,''),'') = '' THEN 1 ELSE 0 END
< CASE WHEN ISNULL(NULLIF(B.Item1,''),'') = '' THEN 1 ELSE 0 END
+ CASE WHEN ISNULL(NULLIF(B.Item2,''),'') = '' THEN 1 ELSE 0 END
+ CASE WHEN ISNULL(NULLIF(B.Item3,''),'') = '' THEN 1 ELSE 0 END
AND ISNULL(NULLIF(B.Item1,''),A.Item1) = A.Item1
AND ISNULL(NULLIF(B.Item2,''),A.Item2) = A.Item2
AND ISNULL(NULLIF(B.Item3,''),A.Item3) = A.Item3
WHERE B.ID IS NULL
select ID,Item1,Item2,Item3
into #T2
from (
select ID='200910',Item1='A',Item2='',Item3='C'
union all
select ID='200910',Item1='A',Item2='D',Item3='C'
union all
select ID='200911',Item1='A',Item2='H',Item3='C'
union all
select ID='200911',Item1='',Item2='',Item3='C'
) Tab
select * from #Temp
select * from #T1
select * from #T2
select A.*,B.* from #T2 A
left join #T1 B
ON A.ID=B.ID
AND ISNULL(NULLIF(B.Item1,''),A.Item1) = A.Item1
AND ISNULL(NULLIF(B.Item2,''),A.Item2) = A.Item2
AND
1¡¢ SQL×¢Èë¹¥»÷µÄ±¾ÖÊ£ºÈÿͻ§¶Ë´«µÝ¹ýÈ¥µÄ×Ö·û´®±ä³ÉSQLÓï¾ä£¬¶øÇÒÄܹ»±»Ö´ÐС£
2¡¢ ÿ¸ö³ÌÐòÔ±¶¼±ØÐë¼ç¸ºÆð·ÀÖ¹SQL×¢Èë¹¥»÷µÄÔðÈΡ£
¡¡¡¡ËµÆð·ÀÖ¹SQL×¢Èë¹¥»÷£¬¸Ð¾õºÜÓôÃÆ£¬Õâô¶àÄêÁË´ó¼ÒÒ»Ö±ÔÚÌÖÂÛ£¬Ò²Ò»Ö±ÔÚÕùÂÛ£¬¿ÉÊǵ½ÁËÏÖÔÚËƺõ»¹ÊÇûÓж¨ÂÛ¡£µ±²»ÖªµÀ×¢ÈëÔÀíµÄʱºò»á¾õµÃºÜÉñÆ棬Ôõô¾Í±»×¢ÈëÁËÄØ£¿ ......
1. SQLÓÅ»¯µÄÔÔòÊÇ£º½«Ò»´Î²Ù×÷ÐèÒª¶ÁÈ¡µÄBLOCKÊý¼õµ½×îµÍ,¼´ÔÚ×î¶ÌµÄʱ¼ä´ïµ½×î´óµÄÊý¾ÝÍÌÍÂÁ¿¡£
µ÷Õû²»Á¼SQLͨ³£¿ÉÒÔ´ÓÒÔϼ¸µãÇÐÈ룺
¼ì²é²»Á¼µÄSQL£¬¿¼ÂÇÆäд·¨ÊÇ·ñ»¹ÓпÉÓÅ»¯ÄÚÈÝ
¼ì²é×Ó²éѯ ¿¼ÂÇSQL×Ó²éѯÊÇ·ñ¿ÉÒÔÓüòµ¥Á¬½ÓµÄ·½Ê½½øÐÐÖØÐÂÊéд
¼ì²éÓÅ»¯ ......