Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an excel file with data of about 1000 records. I want to read each employees temperature of 5 consecutive days. If it is greater than 100 for 5 continuous days, I have to return this employee's data. Can anyone help me to query this is SQL? Thanks in advance!


What I have tried:

SELECT reading_date, employeeID
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Temperature DESC) AS reading_date,*
    FROM TEMP_READINGS
) as readings

WHERE readings.EmployeeID = 5
AND readings.reading_date<= 5
Posted
Updated 14-May-21 2:08am

1 solution

To be honest, I wouldn't do that in SQL: I'd either create a secondary table which monitored the last reading per employee and the count of instances - then update it every time I insert a new temperature value, or I'd do it in a presentation language (less efficient than doing it in SQL because you need to fetch every row, but a damn site easier to produce and maintain).

Think about it: SQL is really not good at spotting "5 sequential days", and even if it was, what if two were last Thursday and Friday, and the other three were Monday, Tuesday, and Wednesday of this week? Do they count? Should they count?

Summaries are a better way to handle that, and presentation language srae more flexible.
SQL is a great tool - but it's good at data storage and retrieval, not data analysis!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900