sql server 2012 - sql select row if another doesn't exist -




i have table around 1 million entries this:

timestamp                   tagname     alarm status 2017-08-02 10:53:10.000     xs-101      alarm 2017-08-02 18:49:45.000     xs-201      alarm 2017-08-03 01:08:16.000     xs-101      normal 2017-08-05 09:16:42.000     xs-301      alarm 2017-08-12 12:33:39.000     xs-101      alarm 

i need figure out tagname has been in alarm longest, don't care if it's not in alarm. can with program code, of program's other sql queries return need. possible sql?

i've searched around examples people returning rows based on contents of other rows, haven't had luck.

which tag in alarm longest asking tag has oldest current alarm code.

you can conditional aggregation:

select tagname, max(timestamp) t group tagname having max(timestamp) = max(case when status = 'alarm' timestamp end) order max(timestamp) asc; 

this assumes 2 alarms not in sequence same tag -- consistent described data.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -