1

テーブルのさまざまなフィールドで 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
...

私は何が欠けていますか?

4

2 に答える 2

0

PIVOT と UNPIVOT を使用できます。

SELECT Number, Winner, Second, Third
FROM (SELECT LottoID, Ranking, Number
        FROM Lotto UNPIVOT (Number FOR Ranking 
        IN ([Winner], [Second], [Third])) AS unpvt) flat
     PIVOT (COUNT(LottoId) FOR Ranking
     IN ([Winner], [Second], [Third])) crosstab
于 2010-04-15T09:08:42.900 に答える
0

SQL Server 2005 以降を使用している場合

With 
    WinnerCounts As
    (
        Select #Numbers.Number, Count(Results.Winner) As Results
        FROM #Numbers
            JOIN Results 
                On #Numbers.Number = Results.Winner 
    )
    , SecondCounts As
    (
        Select #Numbers.Number, Count(Results.Second) As Results
        FROM #Numbers
            JOIN Results 
                On #Numbers.Number = Results.Second
    )
    , ThirdCounts As
    (
        Select #Numbers.Number, Count(Results.Third) As Results
        FROM #Numbers
            JOIN Results 
                On #Numbers.Number = Results.Third
    )
Select Numbers.Number, Coalesce(WinnerCounts.Results,0) As Winner, Coalesce(SecondCounts.Result,0) As Second, Coalesce(ThirdCounts.Result,0) As Third
From #Numbers
    Left Join WinnerCounts
        On WinnerCounts.Results = #Numbers.Number
    Left Join SecondCounts
        On SecondCounts.Results = #Numbers.Number
    Left Join ThirdCounts
        On ThirdCounts.Results = #Numbers.Number

古いバージョンの SQL Server で機能する別の解決策:

Select #Numbers.Number
    , SUM( Case When Winners.Winner Is Not Null Then 1 Else 0 End ) As WinnerCount
    , SUM( Case When Seconds.Second Is Not Null Then 1 Else 0 End ) As SecondCount
    , SUM( Case When Thirds.Third Is Not Null Then 1 Else 0 End ) As ThirdCount
From #Numbers
    Left Join Results As Winners
        On Winners.Winner = #Numbers.Number
    Left Join Results As Seconds
        On Seconds.Second = #Numbers.Number
    Left Join Results As Thirds
        On Thirds.Third = #Numbers.Number
Group By #Numbers.Number
于 2010-04-14T22:09:22.227 に答える