简体   繁体   中英

SQL query extraction: same row with same id

I am bit stuck on the following. The data of the table as follows:

  sid   eid  description   returncode    responsemessage   State
    1   T-1                200            OK               Sent 
    1   T-1  Helloworld                                    Processed

I cannot use stored procedure, the application only supports a SQL query.

select * 
from table 
where eid='T-1' 
  and returncode='200' 
  and returnmessage='OK' 
  and state='Sent' 

May be something needs to be added here??

Any tips or ideas on how I can achieve this with SQL query?

Update: Oracle Database, I want to retrieve "HelloWorld" from the description column but it should be only retrieved when State=Sent has returncode=200 and responsemessage=ok

I'm not entirely clear on what you are trying to accomplish, but maybe you meant something like this:

select T1.* 
from table  T1
INNER JOIN table T2
        ON  T2.SID = T1.sid
where T1.eid='T-1' 
  and T1.returncode='200' 
  and T1.returnmessage='OK' 
  and T1.state='SENT'
  AND T2.description IS NOT NULL

I think you can do this using exists :

select t.*
from t
where t.description is not null and t.eid = 'T-1' and
      exists (select 1
              from t t2
              where t2.eid = t.eid and t2.returncode = '200' and
                    t2.returnmessage = 'OK' and t2.state = 'SENT' 
             );

You might want to include equality on sid as well, but that is not clear from the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM