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. Question about using conditional SQL statement

Question about using conditional SQL statement

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

    I have a time series data in which a numerical indicator fluctuates over time. If I want to find out for which given day, this indicator has crossed above 30 in the past 3 days. Is it possible to write a SQL query that retrieve this information? The logic to check for the crossing movement is: IF indicator(day x) > 30 AND indicator(3 days ago from day x) <30 THEN Return "YES WE HAVE AN UPWARD CROSSING MOVEMENT THROUGH 30 FOR PAST 3 DAYS!" END IF Can this be accomplished using a SQL CASE statement with a HAVING clause or something else? I'd greatly appreciate any help! If possible please provide a sample code ;P

    A D 2 Replies Last reply
    0
    • J James Shao

      I have a time series data in which a numerical indicator fluctuates over time. If I want to find out for which given day, this indicator has crossed above 30 in the past 3 days. Is it possible to write a SQL query that retrieve this information? The logic to check for the crossing movement is: IF indicator(day x) > 30 AND indicator(3 days ago from day x) <30 THEN Return "YES WE HAVE AN UPWARD CROSSING MOVEMENT THROUGH 30 FOR PAST 3 DAYS!" END IF Can this be accomplished using a SQL CASE statement with a HAVING clause or something else? I'd greatly appreciate any help! If possible please provide a sample code ;P

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

      An exists should do it - as far as I can tell from your description

      select column_list....
      from table1 t1
      where indicator > 30
      and exists (select 1 from table1 t2 where t2.pk = t1.pk and t2.indicator < 30 and t2.dateadded = dateadd(day,-3,t1.dateadded))

      where PK is your primary key [Edit] I had missed the date bit from the exists originally [/Edit]

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

      modified on Tuesday, December 15, 2009 11:17 AM

      J 1 Reply Last reply
      0
      • J James Shao

        I have a time series data in which a numerical indicator fluctuates over time. If I want to find out for which given day, this indicator has crossed above 30 in the past 3 days. Is it possible to write a SQL query that retrieve this information? The logic to check for the crossing movement is: IF indicator(day x) > 30 AND indicator(3 days ago from day x) <30 THEN Return "YES WE HAVE AN UPWARD CROSSING MOVEMENT THROUGH 30 FOR PAST 3 DAYS!" END IF Can this be accomplished using a SQL CASE statement with a HAVING clause or something else? I'd greatly appreciate any help! If possible please provide a sample code ;P

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Please do not cross post.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        J 1 Reply Last reply
        0
        • D dan sh

          Please do not cross post.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

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

          Thanks for your help in the other section, sorry about cross-posting. Will try to prevent that in the future. :((

          1 Reply Last reply
          0
          • A Ashfield

            An exists should do it - as far as I can tell from your description

            select column_list....
            from table1 t1
            where indicator > 30
            and exists (select 1 from table1 t2 where t2.pk = t1.pk and t2.indicator < 30 and t2.dateadded = dateadd(day,-3,t1.dateadded))

            where PK is your primary key [Edit] I had missed the date bit from the exists originally [/Edit]

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

            modified on Tuesday, December 15, 2009 11:17 AM

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

            Hi Ashfield, Your code looks exactly like what I need! I'll give it a try after getting home. :) Many thanks again.

            modified on Tuesday, December 15, 2009 10:19 PM

            A 1 Reply Last reply
            0
            • J James Shao

              Hi Ashfield, Your code looks exactly like what I need! I'll give it a try after getting home. :) Many thanks again.

              modified on Tuesday, December 15, 2009 10:19 PM

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

              no problem

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

              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