Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to identify or recoginize a pattern in data with SQL

I'm wondering if this is possible. I am trying to identify records where there is a pattern in the data.

Eg a table with fields ID, DATA_DTE (only daily records), SPEED.

I would like to identify when the field SPEED drops by at least 4 and holds for at least 3 consecutive days in a row

**ID**..**DATA_DTE**...**SPEED**
1........Jan-1............8
1........Jan-2............9
1........Jan-3............4
1........Jan-4............4
1........Jan-5............4
1........Jan-6............7
1........Jan-7............8
1........Jan-8............9

From the above, I basically want to have a SQL query return the ID. Eg "1" in the above eg.

Does anyone know how I can setup my SQL query to return these IDs which match a pattern like this? I'll be running it in ms access 2003.

like image 856
user1243468 Avatar asked Mar 01 '12 20:03

user1243468


People also ask

How do I find a specific pattern in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.

Which command is used for pattern matching in SQL?

LIKE clause is used to perform the pattern matching task in SQL. A WHERE clause is generally preceded by a LIKE clause in an SQL query.

Which command do we use to check patterns in?

Using Regular Expressions With grep You can also use the grep command to search for targets that are defined as patterns by using regular expressions.


1 Answers

I don't know ms access, but it might do the job:

SELECT DISTINCT id
FROM       data AS d1
INNER JOIN data AS d2 ON d1.id = d2.id AND DateAdd("d",+1,d1.data_dte) = d2.data_dte
INNER JOIN data AS d3 ON d1.id = d3.id AND DateAdd("d",+2,d1.data_dte) = d3.data_dte
INNER JOIN data AS d4 ON d1.id = d4.id AND DateAdd("d",+3,d1.date_dte) = d4.data_dte
WHERE d1.speed - d2.speed >= 4
  AND d1.speed - d3.speed >= 4
  AND d1.speed - d4.speed >= 4
like image 135
biziclop Avatar answered Oct 12 '22 21:10

biziclop