When I run the following query:
SELECT datetime_up, logic_id,eqp_name,ack, created FROM NAS_db.main
WHERE TRUNC(datetime_up,'DD') >= TO_TIMESTAMP ('04/18/2012', 'MM/DD/YYYY') and
TRUNC(datetime_up,'DD') <= TO_TIMESTAMP ('04/21/2012', 'MM/DD/YYYY') AND eqp_name ='Router-A'
GROUP BY datetime_up,eqp_name, logic_id,ack,created
I get the following output:
datetime_up logic_id eqp_name ack created
4/19/2012 5:52:04 PM IP_1.1.1.1_ Router-A 1 0
4/19/2012 5:52:04 PM IP_1.1.1.1_ Router-A 0 0
Ok, Those are consecutive records, I need to… in the cases like this , that the first record has a 1, to take only that one, instead of 2.
I mean, if I query and I have 1000 records showing a 1 after the RouterA, I will need to take only one, and avoid the other one, if they are only 0s, the same, only take one, so I would like to have my query to show this.
4/19/2012 5:52:04 PM IP_1.1.1.1_ Router-A 1 0
Thanks!
You are grouping by all of the columns, without using any aggregate functions, which is equivalent to
select distinct.Assuming
ackcan only be zero or one, you can usemax()on that column, and exclude it from thegroup by:Not sure how
createdis expected to behave, you might want to do the same to that column too.In more complicated situations you could use analytic functions to rank the rows by some criteria and then filter in an outer
select, but I think that’s overkill here.