PDA

View Full Version : SQL Query - Design struggle



J.Cz
03-09-2017, 05:48 AM
Hi All,


I am fairly new to SQL Server (2012) but I was assigned the project where I have to use it.
The database consists of one table (counted in millions of rows) which looks mainly like this:



Number (float) Date (datetime) Status (nvarchar(255))
999 2016-01-01 14:00:00.000 Error
999 2016-01-02 14:00:00.000 Error
999 2016-01-03 14:00:00.000 Ok
999 2016-01-04 14:00:00.000 Error
888 2016-01-01 14:00:00.000 Error
888 2016-01-02 14:00:00.000 Ok
888 2016-01-03 14:00:00.000 Error
888 2016-01-04 14:00:00.000 Error
777 2016-01-01 14:00:00.000 Error
777 2016-01-02 14:00:00.000 Error


I have to create a query which will show me only the phone numbers (one number per row so probably Group by number?) that meet the conditions:
1. Number reappears at least 3 times
2. Last two times (that has to be based on date; originally records are not sorted by date) has to be an Error


For example, in the table above the phone number that meets the criteria is only 888, beacuse for 999 2nd newest status is Ok and number 777 reoccurs only 2 times.


I will appreciate any kind of help!


Thanks in advance!