简体   繁体   中英

Method to find hits in comma-separated number string on SQL Server

I have a Windows forms (c#) application and a table in SQL Server that has two columns like this:

ticket (int) | numbers (string)
12345        | '01, 02, 04, 05, 09, 10, 23'

This table may have like 100.000 rows or more.

Where I have to do is to found the amount of hits giving an array of numbers like a lottery. I have 12 hits, 11 hits and 9 hits for example and for each raffled number I have to perform the search of what win the 12 hits, 11 hits or 9 hits.

So, how is the best way to get this approach? I need the best performance.

For now I have this code:

string sentSQL = " SELECT ticket, numbers FROM tableA";

/* CODE TO PERFORM THE CONNECTION */
/*...*/

DbDataReader reader = connection.ExecuteReader();

int hits12, hits11, hits9 = 0;
int count;

while (reader.Read())
{
                count = 0;
                string numbers = reader["numbers"].ToString();
                string ticketNumber = reader["ticket"].ToString();
                int maxJ = balls.Count; //balls is the ArrayList with the numbers currently extracted in the raffle
                for (int j = 0; j < maxJ; j++)
                {
                    if (numbers.Contains(balls[j].ToString()))
                    {
                        count++;
                    }
                }
                switch (count)
                        {
                            case 12:
                                hits12++;
                                break;
                            case 11:
                                hits11++;
                                break;
                            case 9:
                                hits9++;
                                break;
                        }
}

This is working but maybe there is a better method to make it possible.

I'm using SQL Server 2012, maybe is there a function that help me?

Edit: Can i perform in the sql query a SUM of the CHARINDEX of each number to get the amount of hits inside the sql query?

You currently have a totally tacky solution.

create table ticket (
    ticketId int not null -- PK
)
create table TicketNumbers *
    ticketId int not null,
    numberSelected int not null
)

TicketNumbers has an FK to Ticket, and a PK of TicketNumber + numberSelected.

select t.ticketId, count(*) CorrectNumbers
from ticket t
inner join TicketNumbers tn on tn.ticketId = t.TicketId
where tn.numberSelected in (9, 11, 12, 15) -- list all winning numbers
group by t.ticketId
order by count(*) desc

Cheers -

One simple way to improve this is to update your select statement to get only records with numbers greater than your first ball number and less that your last ball number + 1 ...

Example (probably not correct SQL):

SELECT ticket, numbers FROM tableA where '10' < numbers and '43' > numbers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM