1

我想计算我表中每个数字的所有可能组合。

我希望我的查询返回如下内容:

Number (Value)     Count
       1            39
       2            450
       3            41

我的表如下所示:

在此处输入图像描述

当我运行以下查询时:

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 WHERE ld.draw_date = '2013-07-05'
)CC
ORDER BY correct_count desc

我得到这样的东西:

在此处输入图像描述

4

3 回答 3

5

我提供这个解决方案是因为unpivot通常比一系列union alls 表现更好。原因是每个都union all可以导致全表扫描,而它只unpivot需要一次扫描。

所以,你可以写你想要的:

select val, count(*)
from (select pk, val
      from test
      unpivot (val for col in (val1, val2, val3, val4, val5, val6)
              ) as unpvt
     ) t
group by val
order by val;
于 2013-07-06T12:25:11.497 回答
4

假设我确实正确理解了您的需求,我通过以下方式解决了问题。我的假设是,您需要无差别地计算单个值出现在任一 val 列(val1、val2、val3 等)上的出现次数。

这是我的测试数据:

CREATE TABLE Test(
pk int PRIMARY KEY IDENTITY(1,1) NOT NULL,
val1 int, val2 int, val3 int, val4 int, val5 int, val6 int
)

INSERT INTO Test
SELECT 1,2,3,4,5,6 UNION ALL
SELECT 1,2,3,4,5,6 UNION ALL
SELECT 1,2,3,4,5,6 UNION ALL
SELECT 1,2,3,4,5,6 UNION ALL
SELECT 3,3,3,3,3,3 UNION ALL
SELECT 1,2,3,4,5,7

这是返回单个 val 发生次数的查询:

SELECT v, SUM(c) FROM (
    SELECT val1 v, COUNT(*) c FROM Test GROUP BY val1 UNION ALL 
    SELECT val2 v, COUNT(*) FROM Test GROUP BY val2 UNION ALL 
    SELECT val3 v, COUNT(*) FROM Test GROUP BY val3 UNION ALL 
    SELECT val4 v, COUNT(*) FROM Test GROUP BY val4 UNION ALL 
    SELECT val5 v, COUNT(*) FROM Test GROUP BY val5 UNION ALL 
    SELECT val6 v, COUNT(*) FROM Test GROUP BY val6
 ) T
GROUP BY v

我的测试用例中的结果是:

val occurrencies
1   5
2   5
3   11
4   5
5   5
6   4
7   1
于 2013-07-06T09:44:24.400 回答
3

也许我在要求中遗漏了一些东西,但我认为不需要使表值构造函数和CROSS APPLY. 简单UNION ALLGROUP BY可以完成这项工作。

SELECT V, COUNT(*) FROM 
(
    SELECT val1 v FROM dbo.CustomerSelections
    UNION ALL
    SELECT val2 FROM dbo.CustomerSelections
    UNION ALL
    SELECT val3 FROM dbo.CustomerSelections
    UNION ALL
    SELECT val4 FROM dbo.CustomerSelections
    UNION ALL
    SELECT val5 FROM dbo.CustomerSelections
    UNION ALL
    SELECT val6 FROM dbo.CustomerSelections
) x
GROUP BY v

PS:这与其他答案非常相似,只是在性能方面先进行联合,然后按一次分组。

于 2013-07-06T09:51:34.060 回答