1

我有以下 SQL:

SELECT LOTTOID,
      CASE WHEN VAL0 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    + CASE WHEN VAL1 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    + CASE WHEN VAL2 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    + CASE WHEN VAL3 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    + CASE WHEN VAL4 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    + CASE WHEN VAL5 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END
    AS totalValues
INTO #tempWinners
FROM tblLotto 

但我在第 2 行收到以下错误:

不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式

tblDraw表如下所示:

LOTTOID VAL0    VAL1    VAL2    VAL3    VAL4    VAL5    DRAWDATE
    3    7       13      15      17      21      43     2013-06-23 10:56:08.927

基本上我想做的是使用 SQL 来确定是否有人有匹配的乐透号码。

tblDraw 表是平局(显然)。我知道我可以做这样的检查:

SELECT VAL0 FROM tblLotto WHERE IN (SELECT VAL0 FROM tblDraw)

但是 tblLotto 中的 val0 并不总是与抽奖中的 val0 列匹配,因为列中可能有“7”val4因此我需要检查所有列...

4

2 回答 2

2

内部选择

  CASE WHEN VAL0 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END

需要只返回一列。目前它正在返回所有列dbo.tblDraw

唯一可以在子查询中使用 aSELECT * FROM的时候是子查询是EXISTS(SELECT * FROM...)查询时。


更新:

您的问题是由您的表格设计引起的。如果您有一个用于抽奖事件的表和另一个表,其中每个选择的号码包含一行,并且在客户端有类似的布局,那么您将遵循第 3 范式并且查询变得更加简单。但是,让我们假设您无法更改表架构。我还假设您使用的是 SQL 2008 或更高版本,因为我的解决方案不适用于 2005 或更早版本。

要获得您正在寻找的结果,您基本上需要取消旋转绘制值以及客户选择值。比您只需加入结果集并计算您返回的行数。假设所有值都是唯一的(通常是彩票中的情况),这将为您提供客户针对彩票抽奖所做的正确选择的数量。

我在下面发布了一个示例解决方案。不要对所有代码感到震惊。其中大部分是创建表并将一些随机值放入其中。只有查询 1标题之后的查询才是您要查找的内容。

我在创建对象时冒昧地更改了名称。我还稍微改变了一些数字,以使比赛更有可能发生。我的示例中的彩票是 15 次彩票中的 6 次。但这仅与示例数据创建有关,与最终查询无关。

该查询首先将 dbo.LottoDraws 表与 dbo.CustomerSelections 表连接起来。然后 id 交叉应用于子查询。该子查询使用VALUE运算符首先对客户选择的值进行反透视,然后对 LotteryDraws 进行反透视。然后它将这两个结果集连接在一起并计算匹配项。

SQL小提琴

MS SQL Server 2008 架构设置

CREATE TABLE dbo.LottoDraws(
  draw_date DATE PRIMARY KEY CLUSTERED,
  draw1 INT,
  draw2 INT,
  draw3 INT,
  draw4 INT,
  draw5 INT,
  draw6 INT
);
GO
CREATE TABLE dbo.CustomerSelections(
  customer_id INT,-- REFERENCES Customer(customer_id)
  draw_date DATE,
  val1 INT,
  val2 INT,
  val3 INT,
  val4 INT,
  val5 INT,
  val6 INT,
  CONSTRAINT PK_CustomerPicks PRIMARY KEY (customer_id,draw_date)
);

GO
DELETE FROM dbo.LottoDraws;
DELETE FROM dbo.CustomerSelections;


INSERT INTO dbo.LottoDraws(draw_date,draw1,draw2,draw3,draw4,draw5,draw6)
SELECT CAST(dates.d AS DATE) AS draw_date,X4.*
FROM
(VALUES('2013-01-01'),('2013-02-02'),('2013-03-03'),('2013-04-04'),('2013-05-05'))dates(d)
CROSS APPLY
(
SELECT * FROM
(
SELECT 'val'+LTRIM(STR(ROW_NUMBER()OVER(ORDER BY (SELECT val)))) pick, val
FROM(
SELECT TOP(6) val
FROM(SELECT TOP(15) ROW_NUMBER()OVER(ORDER BY (SELECT NULL))*SIGN(LEN(dates.d)) val 
     FROM sys.columns a,sys.columns b,sys.columns c,sys.columns d)X1
ORDER BY NEWID()
)X2
)X3
PIVOT(MAX(val) FOR pick IN([val1],[val2],[val3],[val4],[val5],[val6]))P
)X4
;

INSERT INTO dbo.CustomerSelections(customer_id,draw_date,val1,val2,val3,val4,val5,val6)
SELECT customers.customer_id,CAST(dates.d AS DATE) AS draw_date,X4.*
FROM
(VALUES(111),(222),(333),(444))customers(customer_id)
CROSS JOIN
(VALUES('2013-01-01'),('2013-02-02'),('2013-03-03'),('2013-04-04'),('2013-05-05'))dates(d)
CROSS APPLY
(
SELECT * FROM
(
SELECT 'val'+LTRIM(STR(ROW_NUMBER()OVER(ORDER BY (SELECT val)))) pick, val
FROM(
SELECT TOP(6) val
FROM(SELECT TOP(15) ROW_NUMBER()OVER(ORDER BY (SELECT NULL))*SIGN(customers.customer_id+LEN(dates.d)) val 
     FROM sys.columns a,sys.columns b,sys.columns c,sys.columns d)X1
ORDER BY NEWID()
)X2
)X3
PIVOT(MAX(val) FOR pick IN([val1],[val2],[val3],[val4],[val5],[val6]))P
)X4
;

查询 1

SELECT *
FROM dbo.LottoDraws ld
JOIN dbo.CustomerSelections cs
ON ld.draw_date = cs.draw_date
CROSS APPLY(
  SELECT COUNT(1) correct_count
  FROM (VALUES(cs.val1),(cs.val2),(cs.val3),(cs.val4),(cs.val5),(cs.val6))csv(val)
  JOIN (VALUES(ld.draw1),(ld.draw2),(ld.draw3),(ld.draw4),(ld.draw5),(ld.draw6))ldd(draw)
  ON csv.val = ldd.draw
)CC

结果

|  DRAW_DATE | DRAW1 | DRAW2 | DRAW3 | DRAW4 | DRAW5 | DRAW6 | CUSTOMER_ID | VAL1 | VAL2 | VAL3 | VAL4 | VAL5 | VAL6 | CORRECT_COUNT |
--------------------------------------------------------------------------------------------------------------------------------------
| 2013-01-01 |     2 |     3 |     4 |     5 |     8 |    12 |         111 |    3 |    6 |    9 |   11 |   14 |   15 |             1 |
| 2013-02-02 |     2 |     3 |     5 |     6 |    12 |    14 |         111 |    1 |    2 |    3 |   10 |   11 |   14 |             3 |
| 2013-03-03 |     5 |     6 |     8 |    10 |    11 |    14 |         111 |    2 |    3 |    6 |   13 |   14 |   15 |             2 |
| 2013-04-04 |     2 |     3 |     5 |     6 |    10 |    12 |         111 |    3 |    5 |    6 |    7 |   12 |   15 |             4 |
| 2013-05-05 |     3 |     6 |     7 |    10 |    11 |    12 |         111 |    1 |    4 |    6 |    7 |    9 |   11 |             3 |
| 2013-01-01 |     2 |     3 |     4 |     5 |     8 |    12 |         222 |    3 |    6 |    8 |    9 |   11 |   14 |             2 |
| 2013-02-02 |     2 |     3 |     5 |     6 |    12 |    14 |         222 |    5 |    7 |   11 |   12 |   13 |   14 |             3 |
| 2013-03-03 |     5 |     6 |     8 |    10 |    11 |    14 |         222 |    2 |    6 |    8 |   10 |   13 |   14 |             4 |
| 2013-04-04 |     2 |     3 |     5 |     6 |    10 |    12 |         222 |    4 |    7 |    8 |   11 |   12 |   15 |             1 |
| 2013-05-05 |     3 |     6 |     7 |    10 |    11 |    12 |         222 |    1 |    2 |    4 |    5 |   11 |   14 |             1 |
| 2013-01-01 |     2 |     3 |     4 |     5 |     8 |    12 |         333 |    3 |    7 |    8 |   10 |   14 |   15 |             2 |
| 2013-02-02 |     2 |     3 |     5 |     6 |    12 |    14 |         333 |    2 |    3 |    4 |    7 |    8 |   10 |             2 |
| 2013-03-03 |     5 |     6 |     8 |    10 |    11 |    14 |         333 |    5 |    6 |    7 |   11 |   12 |   14 |             4 |
| 2013-04-04 |     2 |     3 |     5 |     6 |    10 |    12 |         333 |    1 |    8 |   11 |   12 |   14 |   15 |             1 |
| 2013-05-05 |     3 |     6 |     7 |    10 |    11 |    12 |         333 |    3 |    4 |    5 |    8 |   10 |   13 |             2 |
| 2013-01-01 |     2 |     3 |     4 |     5 |     8 |    12 |         444 |    2 |    8 |   10 |   11 |   13 |   14 |             2 |
| 2013-02-02 |     2 |     3 |     5 |     6 |    12 |    14 |         444 |    4 |    6 |    7 |    9 |   10 |   13 |             1 |
| 2013-03-03 |     5 |     6 |     8 |    10 |    11 |    14 |         444 |    2 |    3 |    4 |    6 |   11 |   15 |             2 |
| 2013-04-04 |     2 |     3 |     5 |     6 |    10 |    12 |         444 |    2 |    4 |    7 |    8 |   12 |   14 |             2 |
| 2013-05-05 |     3 |     6 |     7 |    10 |    11 |    12 |         444 |    4 |    7 |   11 |   13 |   14 |   15 |             2 |
于 2013-06-29T20:25:36.807 回答
0

VAL0、VAL1.... 正在寻找子查询中的单个列。而不是select * from tblDraw,尝试返回一个唯一的列。

于 2013-06-29T21:00:17.603 回答