在子查询错误中选择

时间:2013-06-29 19:32:57

标签: sql sql-server subquery

我有以下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列中。因此我需要检查所有列...

2 个答案:

答案 0 :(得分:2)

中的内部选择
  CASE WHEN VAL0 IN (SELECT * FROM tblDraw) THEN 1 ELSE 0 END

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

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


更新:

您的问题是由您的桌面设计引起的。如果您有一个绘制事件的表和另一个包含每个选取的数字的行和客户端的类似布局的表,那么您将遵循第三范式并且查询变得更加简单。但是,我们假设您无法更改表架构。我还假设您使用的是SQL 2008或更高版本,因为我的解决方案将无法在2005或更早版本上运行。

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

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

我在创建对象时更改了名称。我也改变了一些数字,以使匹配发生的可能性更大。我的例子中的彩票是15个乐透中的6个。但这仅与示例数据创建有关,而与最终查询无关。

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

SQL Fiddle

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

<强> Results

|  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 |

答案 1 :(得分:0)

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