SQL query help needed to get desired output


  • Table with three column I AM stoked IN FORMING THE SQL QUERY I have tried all where clause and having clause but cant get the desired output I want output like example "select all distinct train number having station GHY AND RNY
    i.e GHY an RNY together having common train number 

    Train No. Code Station
    15609 GHY Guwahati
    15609 KYQ Kamakhya
    15609 RNY Rangiya Junction
    15609 BPRD Barpeta Road
    15609 NBQ New Bongaigaon
    15609 KOJ Kokrajhar
    15609 FKM Fakiragram Junction
    15609 NOQ New Alipurduar
    15609 NCB New Cooch Behar
    15609 DQG Dhupguri
    15696 AGTL Agartala
    15696 JGNR Jogendranagar
    15696 JRNA Jirania
    15696 TLMR Teliamura
    15696 JTG Jatinga
    15696 LFG Lower Haflong
    15696 MBG Maibang
    15696 LMG Lumding Junction
    12341 HWH Howrah Junction
    12341 BWN Barddhaman Junction
    12341 PAN Panagarh
    12341 RNY Rangiya Junction
    12341 UDL Andal Junction
    12341 RNG Raniganj
    12341 GHY Guwahati
    12342 ASN Asansol Junction
    12342 RNG Raniganj
    12342 UDL Andal Junction
    12342 DGR Durgapur
    12342 PAN Panagarh
    12342 BWN Barddhaman Junction
    12342 HWH Howrah Junction
    12320 AGC Agra Cantonment
    12320 MTJ Mathura Junction
    12320 RNY Rangiya Junction

    so my output will be like this for the above example query
    Train No.
    Thursday, June 20, 2013 1:07 PM

All replies

  • use link this, replace code as per requirement.

    select trainno 
    from tabname
    group by trainno,code
    having code = 'a' 
    select trainno 
    from tabname
    group by trainno,code
    having code = 'b' 

    Rohit Garg
    (My Blog)
    This posting is provided with no warranties and confers no rights.
    Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread.

    Tuesday, August 06, 2013 4:52 AM