Sql Server - Fastest way to get number of times value changed for each ID

Shri

I'm writing sql queries to display different types of results as per following requirments :

  1. Display record who's value changed from 1 to 0, maximum times in given timestamp
  2. Display record who's value changed from 1 to 0, minimum times in given timestamp
  3. Display top 10 records who's value changed from 1 to 0, maximum times in given timestamp

Sample Data :

+----------+-------------+-------------+
| DeviceId | CaptureTime | SensorValue |
+----------+-------------+-------------+
|  DC001   | 02/01/2017  |      0      |
|  DC001   | 02/02/2017  |      1      |
|  DC001   | 02/03/2017  |      0      |
|  DC001   | 02/04/2017  |      1      |
|  DC001   | 02/05/2017  |      0      |
|  DC001   | 02/07/2017  |      1      |
|  DC001   | 02/08/2017  |      0      |
|  DC001   | 02/10/2017  |      1      |
|  DC001   | 02/01/2017  |      0      |
|  DC001   | 02/01/2017  |      0      |
|  DC002   | 02/02/2017  |      1      |
|  DC002   | 02/02/2017  |      0      |
|  DC002   | 02/02/2017  |      1      |
|  DC002   | 02/02/2017  |      1      |
|  DC002   | 02/02/2017  |      1      |
|  DC002   | 02/03/2017  |      1      |
|  DC002   | 02/03/2017  |      0      |
|  DC002   | 02/03/2017  |      0      |
|  DC002   | 02/03/2017  |      1      |
|  DC002   | 02/03/2017  |      1      |
|  DC003   | 02/03/2017  |      1      |
|  DC003   | 02/03/2017  |      1      |
|  DC003   | 02/03/2017  |      0      |
|  DC003   | 02/03/2017  |      1      |
|  DC003   | 02/03/2017  |      1      |
|  DC003   | 02/04/2017  |      1      |
|  DC003   | 02/05/2017  |      1      |
|  DC003   | 02/06/2017  |      1      |
|  DC003   | 02/07/2017  |      1      |
|  DC003   | 02/08/2017  |      1      |
|  DC004   | 02/09/2017  |      0      |
|  DC004   | 02/10/2017  |      0      |
|  DC004   | 02/11/2017  |      1      |
|  DC004   | 02/12/2017  |      0      |
|  DC004   | 02/12/2017  |      1      |
|  DC004   | 02/12/2017  |      1      |
|  DC004   | 02/12/2017  |      1      |
|  DC004   | 02/12/2017  |      1      |
|  DC004   | 02/12/2017  |      1      |
|  DC004   | 02/12/2017  |      1      |
|  DC005   | 02/12/2017  |      0      |
|  DC005   | 02/12/2017  |      0      |
|  DC005   | 02/12/2017  |      0      |
|  DC005   | 02/12/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
|  DC005   | 02/14/2017  |      0      |
+----------+-------------+-------------+

I have created bellow common query for all three requirements :

DECLARE @HoursBack  INT
        , @MinMax   VARCHAR(3)
        , @TopRows  INT

SELECT  TOP (@TopRows) COUNT(TD1.DeviceId) PickedNoOfTimes, ItemName -- I have removed table to get ItemName to simplify this query
FROM    tTrayDetails AS TD1
WHERE   TD1.SensorValue = 0
AND     TD1.CaptureTime > DATEADD(HOUR, -@HoursBack, GETDATE())
AND     TD1.SensorValue <> (
                                SELECT  TOP 1 SensorValue
                                FROM    tTrayDetails TD2
                                WHERE   TD2.CaptureTime < TD1.CaptureTime
                                ORDER BY TD2.CaptureTime DESC
                            )
GROUP BY    TD1.DeviceId
ORDER BY    CASE WHEN @MinMax = 'Max' THEN COUNT(TD1.DeviceId) END DESC
            , CASE WHEN @MinMax = 'Min' THEN COUNT(TD1.DeviceId) END ASC

This query works for all three requirements, by just setting different values of @HoursBack, @MinMax and @TopRows variables.

Here is the values to set for my three requirements :

  1. @HoursBack = 24, @MinMax='Max', @TopRows=1
  2. @HoursBack = 24, @MinMax='Min', @TopRows=1
  3. @HoursBack = 24, @MinMax='Max', @TopRows=10

Now Problem is : This query takes around 40 Sec to execute, only for 14K records on testing environment.

On production environment daily 2-4K records will get added, so this query execution time will increase.

How I can change the query to run faster with large amount of data.

dnoeth

This will count only those rows where SensorValue changed from 1 to 0:

WITH cte AS
 (
   SELECT DeviceId,
      -- previous row = 1 and current row = 0
      CASE WHEN LAG(SensorValue)
                Over (PARTITION BY DeviceId
                      ORDER BY CaptureTime) = 1
                AND SensorValue = 0
           THEN 1
           ELSE 0
      END AS ChangeFlag
   FROM tTrayDetails AS t
   WHERE ....
 )
SELECT DeviceId, Count(*)
FROM cte
WHERE ChangeFlag = 1
GROUP BY DeviceId

Now apply your TOP/ORDER BY ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fastest way to check if value is in array many times

From Dev

Fastest way to check if value is in array many times

From Dev

fastest way to get max value of each masked np.array for many masks?

From Dev

Fastest way to insert 100000 records into SQL Server

From Dev

Fastest Way to Update table in SQL Server

From Dev

Python dict get value fastest way

From Dev

What is the fastest way to get the value of e?

From Dev

Fastest way to get creation and last modification times of a lot of files

From Dev

SQL Server increment the row number for each new unique value

From Dev

SQL Server: assign value for character and get average value for each user

From Dev

SQL Server How to get the number of news under each category

From Dev

SQL Server How to get the number of news under each category

From Dev

SQL Server stored procedure : create increment ID in each occurrence of value

From Dev

Variable from a function to get each changed value

From Dev

Counting the number of times a value has changed within a cell

From Dev

Fastest way to get consecutive index of each set bit?

From Dev

What is the fastest way to find the exact value of the factorial of a large number in python?

From Dev

get a count of each value from every column in a table SQL Server

From Dev

SQL Server query to get count value each day

From Dev

What is the fastest way of finding EAN Number doublettes in a SQL table?

From Dev

SQL: How to display data with row values as column names and then count number of times ID per row value?

From Dev

select same records multiple times with one column value changed in SQL

From Dev

XSLT to get count of number of times value appears

From Dev

What is the fastest way to get the last elements of a list by ID with Datetime?

From Dev

What is the fastest way to insert the current date in sql server

From Dev

Fastest way to insert 1 million rows in SQL Server

From Dev

What is the fastest way to look for duplicate uniqueidentifier in Sql Server?

From Dev

Fastest way to get the index of the first sublist that contains value

From Dev

Fastest way for Get Value of a property (Reflection) in C#

Related Related

  1. 1

    Fastest way to check if value is in array many times

  2. 2

    Fastest way to check if value is in array many times

  3. 3

    fastest way to get max value of each masked np.array for many masks?

  4. 4

    Fastest way to insert 100000 records into SQL Server

  5. 5

    Fastest Way to Update table in SQL Server

  6. 6

    Python dict get value fastest way

  7. 7

    What is the fastest way to get the value of e?

  8. 8

    Fastest way to get creation and last modification times of a lot of files

  9. 9

    SQL Server increment the row number for each new unique value

  10. 10

    SQL Server: assign value for character and get average value for each user

  11. 11

    SQL Server How to get the number of news under each category

  12. 12

    SQL Server How to get the number of news under each category

  13. 13

    SQL Server stored procedure : create increment ID in each occurrence of value

  14. 14

    Variable from a function to get each changed value

  15. 15

    Counting the number of times a value has changed within a cell

  16. 16

    Fastest way to get consecutive index of each set bit?

  17. 17

    What is the fastest way to find the exact value of the factorial of a large number in python?

  18. 18

    get a count of each value from every column in a table SQL Server

  19. 19

    SQL Server query to get count value each day

  20. 20

    What is the fastest way of finding EAN Number doublettes in a SQL table?

  21. 21

    SQL: How to display data with row values as column names and then count number of times ID per row value?

  22. 22

    select same records multiple times with one column value changed in SQL

  23. 23

    XSLT to get count of number of times value appears

  24. 24

    What is the fastest way to get the last elements of a list by ID with Datetime?

  25. 25

    What is the fastest way to insert the current date in sql server

  26. 26

    Fastest way to insert 1 million rows in SQL Server

  27. 27

    What is the fastest way to look for duplicate uniqueidentifier in Sql Server?

  28. 28

    Fastest way to get the index of the first sublist that contains value

  29. 29

    Fastest way for Get Value of a property (Reflection) in C#

HotTag

Archive