我想数字从1频数到100了桌子上的不同领域。
比方说,我有表“结果”数据如下:
LottoId Winner Second Third
--------- --------- --------- ---------
1 1 2 3
2 1 2 3
我希望能够得到每个号码的频率。 对于我使用下面的代码:
--Creating numbers temp table
CREATE TABLE #Numbers(
Number int)
--Inserting the numbers into the temp table
declare @counter int
set @counter = 0
while @counter < 100
begin
set @counter = @counter + 1
INSERT INTO #Numbers(Number) VALUES(@counter)
end
--
SELECT #Numbers.Number, Count(Results.Winner) as Winner,Count(Results.Second) as Second, Count(Results.Third) as Third FROM #Numbers
LEFT JOIN Results ON
#Numbers.Number = Results.Winner OR #Numbers.Number = Results.Second OR #Numbers.Number = Results.Third
GROUP BY #Numbers.Number
的问题是,计数每个数量的重复相同的值。 在这种特殊情况下,我发现了以下结果:
Number Winner Second Third
--------- --------- --------- ---------
1 2 2 2
2 2 2 2
3 2 2 2
...
当我应该得到这样的:
Number Winner Second Third
--------- --------- --------- ---------
1 2 0 0
2 0 2 0
3 0 0 2
...
我在想什么?