Select result, and show only if all are equivalent

Asked

Viewed 114 times

0

I’m maintaining a database, and I’m in the next situation; in the bank I have 5 columns in a table, are:

ID(PK)|Idticket|Result|Prestadoconta|Date

I have to filter for the user to see, only the Idticket that is missing result, or that already have the results all checked.

Normally use select * from bilhetes to return everything, and comes something similar to this:

00|BFF554|0|0|21/12/2017

01|BFF554|1|0|21/12/2017

02|BFF554|2|0|21/12/2017

03|CCC154|1|0|22/12/2017

04|CCC154|3|0|22/12/2017

05|CCC154|1|0|22/12/2017

Then I want to group the repeated Idticket only if all the 'result' columns of that ID are equal to a specific value.

Ex: Return me the Idticket only if all the 'result' columns equals 0! So if BFF554 has all 3 columns result with the value 0, the query returns me this ID, if one of the result columns is not the value 0, this ID does not interest me.

  • Hi, I can help you just that your question is confused, have to explain again

  • You want to group by idticket, but in case it has many values above 0 , only appear once?

  • 1

    Hi, is that in the database I’m having to group the results based on a column that has repeated Ids, but I want to group the result only if the other 'result' column are all equal to a specific condition.

  • By chance it was not only group by Idticket,Result and bringing how many each brought?

  • 1

    You mean, making one select count(resultado) from bilhetes where resultado = 0 group by IDBilhete ? but how I would filter to return me only if all the results are equal to 0 for example?

  • select Count(result) from tickets group by Idticket, Result

  • And in case you don’t want zero to appear, select Count(result) from tickets Where Result > 0 group by Idticket, Result

  • is, but what I want to show is only if, on that specific ID, all the results are 0, if any of the results of that particular ID is not 0 I don’t want to see that ID! I think just doing a cursor right?!

Show 3 more comments

1 answer

1


select bi. * from tickets as bi Where bi.Idticket not in ( select b.Idticket from tickets as b Result > 0 ) group by bi.Idticket

You take out the idbbilhetes first, which has a result greater than zero, and then you group the rest.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.