我想计算我表中每个数字的所有可能组合。
我希望我的查询返回如下内容:
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
我得到这样的东西: