Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Multiple occurences would be the easiest. You count how many of each combination you can find and select those with a count bigger than 3.</p> <pre><code>SELECT S.Machine, S.Station, S.DefectCode, S.CreateTime FROM ( SELECT Machine, Station, DefectCode, CreateTime, COUNT(CreateTime) OVER (PARTITION BY Machine, Station, DefectCode) AS DefectCount FROM SAMPLE WHERE Machine = :machine ) S WHERE S.DefectCount &gt; 3 </code></pre> <p>Consecutive occurence is a bit harder (the following query may not be the most optimal one). Assuming consecutive is 'consecutive date', you could try the following:</p> <pre><code>WITH OrderedSample AS ( SELECT Machine, Station, DefectCode, CreateTime, ROW_NUMBER() OVER (ORDER BY CreateTime) AS RowNr FROM SAMPLE WHERE Machine = :machine ) SELECT Current.Machine, Current.Station, Current.DefectCode, Current.CreateTime FROM OrderedSample Current WHERE EXISTS ( SELECT 1 FROM OrderedSample Next WHERE Current.Machine = Next.Machine AND Current.Station = Next.Station AND Current.DefectCode = Next.DefectCode AND Current.RowNr = Next.RowNr - 1 ) OR EXISTS ( SELECT 1 FROM OrderedSample Prev WHERE Current.Machine = Prev.Machine AND Current.Station = Prev.Station AND Current.DefectCode = Prev.DefectCode AND Current.RowNr = Prev.RowNr + 1 ) </code></pre> <p><strong>EDIT</strong></p> <p>Just realized the second query becomes a bit easier because you are working with a fixed Machine:</p> <pre><code>WITH OrderedSample AS ( SELECT Machine, Station, DefectCode, CreateTime, ROW_NUMBER() OVER (ORDER BY CreateTime) AS RowNr FROM SAMPLE WHERE Machine = :machine ) SELECT Curr.Machine, Curr.Station, Curr.DefectCode, Curr.CreateTime, RowNr FROM OrderedSample Curr WHERE EXISTS ( SELECT 1 FROM OrderedSample Next WHERE Curr.Station = Next.Station AND Curr.DefectCode = Next.DefectCode AND Curr.RowNr = Next.RowNr - 1 ) OR EXISTS ( SELECT 1 FROM OrderedSample Prev WHERE Curr.Station = Prev.Station AND Curr.DefectCode = Prev.DefectCode AND Curr.RowNr = Prev.RowNr + 1 ) </code></pre> <p><strong>EDIT 2</strong>: Just noticed Machine &amp; Station are two columns, edited</p> <p><strong>EDIT 3</strong>: Some fixes based on <a href="http://www.sqlfiddle.com/#!3/50b92/31" rel="nofollow">Sql Fiddle example</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COthanks sam! this code is awesome. but is there a way not to enumerate the machine numbers? cause if i have 100 machines i have to paste the code hundred times. also how do put it in the email body arrange exactly as the above expected result? "OCAP triggered on machine 1. d1 consecutive occurence" then the resulting query for that paricular machine. is it possible using Database mail? I've read that queries result can be included in the email body but cannot quite figure out how to do it. hope you can help me out while your at it. thanks!
      singulars
    2. CO@L42 If you are happy with a list of everything in one table, then for multiple occurences you only need to remove the `WHERE` that filters on machines, the same for consecutive occurence (but take the first one of the two examples as you will now need to join on Machine as well in your `EXISTS`). If you want a seperate list for each Machine/DefectCode combination things get a lot more complicated as you'll need to loop over everything somehow (either in an application or in a stored procedure as Tolga commented). I'm not familiar with Database mail, so no idea how to handle that.
      singulars
    3. COyep a list in one table will do but i have to output the comments on what particular machine and what particular defect the OCAP was triggered which will still complicate things (this will serve as alert notice when a certain defect repeatedly occurs). btw, no worries on DB mail man. you've helped me more than enough with these codes.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload