2
I have a table with 100,000 records and the fields:
id | dia | chuva_manha | chuva_tarde | chuva_noite
I would like to create an instruction that consults the table and counts how many times a given sequence of events happens. For example I want to take how many times it rained one day in the morning and the other day it rained in the afternoon.
But for that I have to create 2 instructions select
in C# and create a loop for
to validate the result (the.EXE_READER manager is a CRUD that I have implemented that makes all the connection and transaction with sql server, just sending the SQL statement)
int valor = 0;
for(int id = 0; id <= 100000; id+)
{
string query1 = "SELECT * FROM dbchuva WHERE [id] = id AND chuva_manha != 0";
string query2 = "SELECT * FROM dbchuva WHERE [id] = (id + 1) AND chuva_tarde != 0";
DataTable dados1 = gestor.EXE_READER(query1);
int Qtde1 = dados1.Rows.Count;
DataTable dados2 = gestor.EXE_READER(query2;
int Qtde2 = dados2.Rows.Count;
if(Qtde1 !=0 && Qtde2 !=0)
{
int valor = 1;
}
}
Or
int valor = 0;
for(int id = 0; id <= 100000; id+)
{
int id2 = id + 1;
string query = "SELECT * FROM dbchuva";
DataTable dados = gestor.EXE_READER(query)
int val1= Convert.ToInt16(dados.Rows[id]["chuva_manha"]);
int val2 = Convert.ToInt16(dados.Rows[id2]["chuva_tarde"]);
if valor(val1 != 0 && val2 != 0)
{
valor = 1;
}
}
Both forms require a lot of time to process due to the large amount of table values that ends up locking the whole system.
How could I implement in a simpler way?
How to verify that line X occurred event A and line X+1 event B occurred?
Or better how to count how many times when happening event A, in the subsequent record occurred event B?
I read your answer, and saw that you can create several LEAD in this same instruction, carammmba, solved the question in the blink of an eye.
– i_melo
yes, very useful function, already helped me a few times too, not to mention that there is no need to do the direct verification by code, just pick up the result
– mateusalxd
Can give a light in https://answall.com/q/386299/93308
– i_melo