#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Angry 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.
    15609
    12341
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,146
    Rep Power
    4274
    Code:
    SELECT train_no
      FROM timetable
     WHERE code IN ('GHY','RNY')
    GROUP
        BY train_no
    HAVING COUNT(*) = 2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo