Solved

sql query to count records where field changed to specific value from another

Posted on 2014-10-04
4
824 Views
Last Modified: 2014-10-19
Hi experts,

I created a mock table below to simulate the problem I'm trying to solve. I'm trying to write a query which would count all the DefectIDs where the status changed from Open to closed such as ID 125 in the table below. The count shouldn't included any other defects where the status changed to closed but the previous status wasn't Open. Using the example below the Defect 245 wouldn't be included in the results. I will also need a query to work in both oracle & SQL server databases in case the queries are slightly differebt.

Any assistance or ideas would be appreciated

DefectID    Column2    Column 3   Status   DateStamp
-------------------------------------------------------------------
245               aaaaa    bbbbbb        New          2/10/2014 13:15:58
125               dgsdg     dsgsgg         New          2/10/2014 13:16:33
333               sdsdg    gsdggs           New         2/10/2014  13:16:49
125               fsafaf     safafff           Open        2/10/2014  15:20:44
245               fafasf      fhfdhd          Assigned  2/10/2014   15:25:12
125              asfasf     safasff           Closed       3/10/2014   09:30:22
245              gdgdsg    gdsgg           Closed       4/10/2014    11:14:11

Thanks
0
Comment
Question by:victoriaharry
4 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40361228
Use the LEAD() function to access the next status value (based on datestamp for the same defectid)
Then filter accordingly
SELECT
      d.*
FROM (
      SELECT
            defects.*
          , LEAD(status) OVER (PARTITION BY defectid ORDER BY datestamp ASC) AS next_state
      FROM defects
      ) d
WHERE status = 'Open'
      AND next_state = 'Closed'
ORDER BY
      d.defectid
;

Open in new window

You cannot filter directly on the LEAD() calculation so a nested query is used so the column alias (next_state) may be used.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40361266
Note: Lead() requires MS SQL 2012 or higher (might be an issue), and Oracle 10.2 (which shouldn't be an issue).
0
 
LVL 41

Expert Comment

by:pcelba
ID: 40361344
What about the simple code
SELECT COUNT(*)
 FROM YourTable
WHERE Status = 'Closed'
   AND DefectID IN SELECT(DefectID FROM YourTable WHERE Status = 'Open')

Open in new window

Or do you have some defects which can be reopen after closing or another defects order "incompatibility"?
Of course, you may use INNER JOIN instead of IN.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40364213
;WITH data_with_row_nums AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY DefectID ORDER BY DateStamp) AS row_num
    FROM tablename
)
SELECT d1.DefectId, d1.Column2, d1.Column3, d1.Status, d1.DateStamp, d0.DateStamp AS DateStamp_Opened
FROM data_with_row_nums d1
INNER JOIN data_with_row_nums d0 ON
    d0.DefectID = d1.DefectID AND
    d0.row_num = d1.row_num - 1 AND
    d0.status = 'Open'
WHERE
    d1.Status = 'Closed'
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now