有大神在不,关于彩票查询的一个SQL脚本 为什么个别查不出来?
下面的代码 为什么不对啊 我查询兑奖级别 = 5 可是 第一条 和第二条 都对了4个号 为什么没显示出来?
脚本 sql
IF object_id('tempdb..#tb') IS NOT NULL DROP TABLE #tb
go
CREATE TABLE #tb(h1 char(2),h2 char(2),h3 char(2),h4 char(2),h5 char(2),h6 char(2),h7 CHAR(2))
INSERT #tb
SELECT '05', '06', '12', '14', '19', '23', '05' union ALL --这条查不出来
SELECT '02', '12', '15', '23', '24', '32', '09' union ALL --还有这条也查不出来
SELECT '02', '14', '15', '19', '23', '24', '12' union ALL --这条可以
SELECT '02', '12', '14', '19', '28', '33', '10' union ALL --这条也可以
SELECT '05', '06', '08', '09', '12', '13', '05' union ALL
SELECT '00', '00', '00', '00', '00', '00', '05'
GO
--sql:
DECLARE @兑奖号码 VARCHAR(100), @兑奖级别 INT
SELECT @兑奖号码 = '02,12,14,19,23,24,05', @兑奖级别 =5
SELECT h1,h2,h3,h4,h5,h6,h7
FROM
(
SELECT
兑奖级别 =
CASE
WHEN (红球数量=6 AND 蓝球数量=1) THEN 1
WHEN (红球数量=6) THEN 2
WHEN (红球数量=5 AND 蓝球数量=1) THEN 3
WHEN ((红球数量=5) OR (红球数量=4 AND 蓝球数量=1)) THEN 4
WHEN ((红球数量=4) OR (红球数量=3 AND 蓝球数量=1)) THEN 5
WHEN ((红球数量=2 AND 蓝球数量=1) OR (红球数量=1 AND 蓝球数量=1) OR (蓝球数量=1)) THEN 6 --这儿可以简化成:蓝球数量=1
ELSE 0
END,
h1,h2,h3,h4,h5,h6,h7
FROM
(
SELECT
红球数量 = (CASE WHEN h1 = SUBSTRING(@兑奖号码, 1, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h2 = SUBSTRING(@兑奖号码, 4, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h3 = SUBSTRING(@兑奖号码, 7, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h4 = SUBSTRING(@兑奖号码, 10, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h5 = SUBSTRING(@兑奖号码, 13, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h6 = SUBSTRING(@兑奖号码, 16, 2) THEN 1 ELSE 0 END),
蓝球数量 = (CASE WHEN h7 = SUBSTRING(@兑奖号码, 19, 2) THEN 1 ELSE 0 END),
h1,h2,h3,h4,h5,h6,h7
FROM #tb
) t
) m
WHERE 兑奖级别 = @兑奖级别
FROM
(
SELECT
红球数量 = (CASE WHEN h1 = SUBSTRING(@兑奖号码, 1, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h2 = SUBSTRING(@兑奖号码, 4, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h3 = SUBSTRING(@兑奖号码, 7, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h4 = SUBSTRING(@兑奖号码, 10, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h5 = SUBSTRING(@兑奖号码, 13, 2) THEN 1 ELSE 0 END)
+(CASE WHEN h6 = SUBSTRING(@兑奖号码, 16, 2) THEN 1 ELSE 0 END),
蓝球数量 = (CASE WHEN h7 = SUBSTRING(@兑奖号码, 19, 2) THEN 1 ELSE 0 END),
h1,h2,h3,h4,h5,h6,h7
FROM #tb
) t