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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need help with DATEDIFF function

Need help with DATEDIFF function

Scheduled Pinned Locked Moved Database
questioncsshelp
3 Posts 2 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.
  • J Offline
    J Offline
    James Shao
    wrote on last edited by
    #1

    I have a stock time series data in which a numerical indicator fluctuates over time. If I want to find out for each stock that has its indicator now over 30, how many days since the last time this indicator is less than 30? In other words, how many days since the last time the stock's indicator crosses above 30? Just for your reference, currently I have the following: SELECT ticker, DATEDIFF(d,(SELECT MAX(DATE) FROM....),'12/21/2009') WHERE Indicator > 30 AND Date = '12/21/2009' I'd greatly appreciate any help! :) If possible please provide a sample code. By the way, thank you Ashfield for your help on a similar question last time, later I was able to understand the code you gave me and utilized it in different ways. This is a bit more difficult for me though.

    modified on Monday, December 21, 2009 12:06 AM

    A 1 Reply Last reply
    0
    • J James Shao

      I have a stock time series data in which a numerical indicator fluctuates over time. If I want to find out for each stock that has its indicator now over 30, how many days since the last time this indicator is less than 30? In other words, how many days since the last time the stock's indicator crosses above 30? Just for your reference, currently I have the following: SELECT ticker, DATEDIFF(d,(SELECT MAX(DATE) FROM....),'12/21/2009') WHERE Indicator > 30 AND Date = '12/21/2009' I'd greatly appreciate any help! :) If possible please provide a sample code. By the way, thank you Ashfield for your help on a similar question last time, later I was able to understand the code you gave me and utilized it in different ways. This is a bit more difficult for me though.

      modified on Monday, December 21, 2009 12:06 AM

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      How about something like

      select ticker,datediff(day,t.date,(select max(date) from table t1
      where t1.ticker = t.ticker and t1.indicator < 30))
      from table t

      You may have to play about a bit, but it should be somewhere close

      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

      J 1 Reply Last reply
      0
      • A Ashfield

        How about something like

        select ticker,datediff(day,t.date,(select max(date) from table t1
        where t1.ticker = t.ticker and t1.indicator < 30))
        from table t

        You may have to play about a bit, but it should be somewhere close

        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

        J Offline
        J Offline
        James Shao
        wrote on last edited by
        #3

        Thank you Ashfield, I'll test it out later and come back if I still have an issue.

        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