Solved

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

Posted on 2014-10-04
4
835 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 69

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:
Scott Pletcher 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

825 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