Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL self join question

SQL self join question

Scheduled Pinned Locked Moved Database
databasequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asad Hussain
    wrote on last edited by
    #1

    I have a table that has currency exchange rates as below CCY EX_RATE RATE_DT ---- ------- ------- AED 1.1 7/31/06 AED 1.0 6/30/06 AED 1.5 5/31/06 What that means is that the exchange rate was 1.5 from 5/31 to 6/30 and is at 7/31 till eternity. What I want is the output to look like this CCY EX_RATE START_DT END_DT ---- ------- -------- ------ AED 1.1 7/31/06 (NULL) AED 1.0 6/30/06 7/31/06 AED 1.5 5/31/06 6/30/06 I think an self-join will give me the correct data but i am getting excessive rows when i do a join.

    F E 2 Replies Last reply
    0
    • A Asad Hussain

      I have a table that has currency exchange rates as below CCY EX_RATE RATE_DT ---- ------- ------- AED 1.1 7/31/06 AED 1.0 6/30/06 AED 1.5 5/31/06 What that means is that the exchange rate was 1.5 from 5/31 to 6/30 and is at 7/31 till eternity. What I want is the output to look like this CCY EX_RATE START_DT END_DT ---- ------- -------- ------ AED 1.1 7/31/06 (NULL) AED 1.0 6/30/06 7/31/06 AED 1.5 5/31/06 6/30/06 I think an self-join will give me the correct data but i am getting excessive rows when i do a join.

      F Offline
      F Offline
      Farhan Noor Qureshi
      wrote on last edited by
      #2

      Asad, This should work.

      Select CCY, EX_RATE, MIN(RATE_DT) as START_DT, MAX(RATE_DT) as END_DT
      From your_currency_table
      Group by CCY, EX_RATE
      

      Farhan Noor Qureshi

      A 1 Reply Last reply
      0
      • F Farhan Noor Qureshi

        Asad, This should work.

        Select CCY, EX_RATE, MIN(RATE_DT) as START_DT, MAX(RATE_DT) as END_DT
        From your_currency_table
        Group by CCY, EX_RATE
        

        Farhan Noor Qureshi

        A Offline
        A Offline
        Asad Hussain
        wrote on last edited by
        #3

        Thanks for your reply but that doesnt work. If I have this data

        CCY EX_RATE RATE_DT


        AED 1.1 7/31/06
        AED 1.0 6/30/06
        AED 1.5 5/31/06

        It will return something like

        CCY EX_RATE START_DT END_DT


        AED 1.1 7/31/06 7/31/06
        AED 1.0 6/30/06 6/30/06
        AED 1.5 5/31/06 5/31/06

        I want it to give me ranges like

        CCY EX_RATE START_DT END_DT


        AED 1.1 7/31/06 (NULL)
        AED 1.0 6/30/06 7/31/06
        AED 1.5 5/31/06 6/30/06

        1 Reply Last reply
        0
        • A Asad Hussain

          I have a table that has currency exchange rates as below CCY EX_RATE RATE_DT ---- ------- ------- AED 1.1 7/31/06 AED 1.0 6/30/06 AED 1.5 5/31/06 What that means is that the exchange rate was 1.5 from 5/31 to 6/30 and is at 7/31 till eternity. What I want is the output to look like this CCY EX_RATE START_DT END_DT ---- ------- -------- ------ AED 1.1 7/31/06 (NULL) AED 1.0 6/30/06 7/31/06 AED 1.5 5/31/06 6/30/06 I think an self-join will give me the correct data but i am getting excessive rows when i do a join.

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          select ccy,ex_rate,rate_dt as start_dt,(select top 1 cur2.rate_dt from tblcurrencyrate cur2 where cur.ccy = cur2.ccy and cur2.rate_dt > cur.rate_dt order by cur2.rate_dt) as end_dt from tblcurrencyrate cur

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups