日期:2014-05-18 浏览次数:20626 次
--演示数据 create table lottery(ID int, F1 CHAR(1), F2 CHAR(1), F3 CHAR(1), F4 CHAR(1), F5 CHAR(1), F6 CHAR(1), F7 CHAR(1), F8 CHAR(1), F9 CHAR(1), F10 CHAR(1), F11 CHAR(1), F12 CHAR(1), F13 CHAR(1), F14 CHAR(1)) go insert lottery values('1','*','3','*','*','3','3','0','0','*','*','3','0','3','1') insert lottery values('2','*','3','*','*','3','3','0','0','*','*','3','0','3','0') go --具体语句 WITH t310 AS(SELECT Val='3' UNION ALL SELECT Val='1' UNION ALL SELECT Val='0'), t1 AS (SELECT ID,ISNULL(b.Val,'*') F1,CASE WHEN a.F1=b.Val OR a.F1='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F1 <> '*'), t2 AS (SELECT ID,ISNULL(b.Val,'*') F2,CASE WHEN a.F2=b.Val OR a.F2='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F2 <> '*'), t3 AS (SELECT ID,ISNULL(b.Val,'*') F3,CASE WHEN a.F3=b.Val OR a.F3='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F3 <> '*'), t4 AS (SELECT ID,ISNULL(b.Val,'*') F4,CASE WHEN a.F4=b.Val OR a.F4='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F4 <> '*'), t5 AS (SELECT ID,ISNULL(b.Val,'*') F5,CASE WHEN a.F5=b.Val OR a.F5='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F5 <> '*'), t6 AS (SELECT ID,ISNULL(b.Val,'*') F6,CASE WHEN a.F6=b.Val OR a.F6='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F6 <> '*'), t7 AS (SELECT ID,ISNULL(b.Val,'*') F7,CASE WHEN a.F7=b.Val OR a.F7='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F7 <> '*'), t8 AS (SELECT ID,ISNULL(b.Val,'*') F8,CASE WHEN a.F8=b.Val OR a.F8='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F8 <> '*'), t9 AS (SELECT ID,ISNULL(b.Val,'*') F9,CASE WHEN a.F9=b.Val OR a.F9='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F9 <> '*'), t10 AS (SELECT ID,ISNULL(b.Val,'*') F10,CASE WHEN a.F10=b.Val OR a.F10='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F10 <> '*'), t11 AS (SELECT ID,ISNULL(b.Val,'*') F11,CASE WHEN a.F11=b.Val OR a.F11='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F11 <> '*'), t12 AS (SELECT ID,ISNULL(b.Val,'*') F12,CASE WHEN a.F12=b.Val OR a.F12='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F12 <> '*'), t13 AS (SELECT ID,ISNULL(b.Val,'*') F13,CASE WHEN a.F13=b.Val OR a.F13='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F13 <> '*'), t14 AS (SELECT ID,ISNULL(b.Val,'*') F14,CASE WHEN a.F14=b.Val OR a.F14='*' THEN 0 ELSE 1 END ID2 FROM lottery a LEFT JOIN t310 b on a.F14 <> '*') SELECT t1.ID,t1.F1,t2.F2,t3.F3,t4.F4,t5.F5,t6.F6,t7.F7,t8.F8,t9.F9,t10.F10,t11.F11,t12.F12,t13.F13,t14.F14 FROM t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14 WHERE t1.ID=t2.ID and t1.ID=t3.ID and t1.ID=t4.ID and t1.ID=t5.ID and t1.ID=t6.ID and t1.ID=t7.ID and t1.ID=t8.ID and t1.ID=t9.ID and t1.ID=t10.ID and t1.ID=t11.ID and t1.ID=t12.ID and t1.ID=t13.ID and t1.ID=t14.ID and t1.ID2+t2.ID2+t3.ID2+t4.ID2+t5.ID2+t6.I