Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not a database expert but I think I came up with an somewhat elegant solution that does not require restructuring the data into another table. If you use a pivot table you can get SQL to return the proper counts for each number.</p> <p>First the pivot table (don't name pivot because it causes a MS SQL Server error in the query). It is simply a table with one column of type int, primary key). It holds data that has a row from 1 to 100. You only really need as many numbers as your highest lottery number. More is OK.</p> <pre><code>PVT Structure: i(int,primary key) PVT Data: (1) (2) (3) .... (100) </code></pre> <p>I am doing this example for Florida Lottery 6 numbers, no powerball, 53 numbers.</p> <p>You have a LotteryTicket table, something like </p> <pre><code>LotteryTicket: ID, Number, N1, N2, N3, N4, N5, N6 </code></pre> <p>SampleData:</p> <blockquote> <p>(1), (1-2-3-4-5-6), (1), (2), (3), (4), (5), (6)</p> <p>(2), (1-2-3-15-18-52), (1), (2), (3), (15), (18), (52)</p> </blockquote> <p>Query/Stored procedure: [pass in a winning lottery number like: 1-2-3-20-30-33 or leave default params (this example)]</p> <pre><code>MatchFloridaLottery ( @p1 int = 1, @p2 int = 2, @p3 int = 3, @p4 int = 4, @p5 int = 5, @p6 int = 6, @minmatches int = 2 ) AS SELECT t.id, COUNT(p.i) numbermatch FROM LotteryTicket t, pvt p WHERE (n1 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n1=p.i) or (n2 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n2=p.i) or (n3 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n3=p.i) or (n4 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n4=p.i) or (n5 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n5=p.i) or (n6 IN (@p1,@p2,@p3,@P4,@p5,@p6) AND t.n6=p.i) group by n.id HAVING COUNT(p.i) &gt; @minmatches </code></pre> <p>For my example in LotteryTickets I get:</p> <pre><code>ID NumberMatch (count of numbers that matched) 1 6 2 3 </code></pre> <p>The pivot table allows the query to return a row for each column that matches a winning number which you then group together by id and count the total rows returned by the pivot table (column i) which is the total number of matches to the winning number. Yes the query is not real pretty but it works and avoids having to do all the work of a separate table and rows. Modify as needed for different games.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload