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. Delta between records in one column

Delta between records in one column

Scheduled Pinned Locked Moved Database
helpdatabasetutorialquestion
8 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.
  • U Offline
    U Offline
    User 3770393
    wrote on last edited by
    #1

    Hi I have a column where are values regarding to time. And In my query I want to add additional column where I will have delta between each record. So e.g. between one record and another remain 34 seconds. For this one I was trying to use DateDiff function, and compare my column with times to the same column but offset by one record. And there is the problem. It looks like date DateDIFF(seconds, column, [subquery]) and I have error that subquery return more than 1 value and it its not appropriate where are following; =, != or something like that. Do you have any ideas? how to manage this problem? Dnorus

    W J 2 Replies Last reply
    0
    • U User 3770393

      Hi I have a column where are values regarding to time. And In my query I want to add additional column where I will have delta between each record. So e.g. between one record and another remain 34 seconds. For this one I was trying to use DateDiff function, and compare my column with times to the same column but offset by one record. And there is the problem. It looks like date DateDIFF(seconds, column, [subquery]) and I have error that subquery return more than 1 value and it its not appropriate where are following; =, != or something like that. Do you have any ideas? how to manage this problem? Dnorus

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      If you use a subquery to fetch the previous (or next) record you must ensure that only 1 record is fetched. So this means that you have to be able to order the rows based on some column and take only the first, for example using TOP 1. So your query could look something like:

      SELECT ...
      DATEDIFF(seconds,
      datecolumn1,
      (SELECT TOP 1 datecolumn2
      FROM YourTable a1
      WHERE ...
      ORDER BY datecolumn2 DESC))
      FROM YourTable
      WHERE ...

      The need to optimize rises from a bad design.My articles[^]

      U 1 Reply Last reply
      0
      • W Wendelius

        If you use a subquery to fetch the previous (or next) record you must ensure that only 1 record is fetched. So this means that you have to be able to order the rows based on some column and take only the first, for example using TOP 1. So your query could look something like:

        SELECT ...
        DATEDIFF(seconds,
        datecolumn1,
        (SELECT TOP 1 datecolumn2
        FROM YourTable a1
        WHERE ...
        ORDER BY datecolumn2 DESC))
        FROM YourTable
        WHERE ...

        The need to optimize rises from a bad design.My articles[^]

        U Offline
        U Offline
        User 3770393
        wrote on last edited by
        #3

        It isnt good solution. subquery returns one record for example first one. and Each record in datecolumn1 is compared to only one record from datecolumn2. I want to create some query that I will have data from datecolumn from ID 1 to 10, then create subquery to have data from the same datecolumn from ID 2 to 11, and then use datediff to obtain time delta between following records.

        W 1 Reply Last reply
        0
        • U User 3770393

          It isnt good solution. subquery returns one record for example first one. and Each record in datecolumn1 is compared to only one record from datecolumn2. I want to create some query that I will have data from datecolumn from ID 1 to 10, then create subquery to have data from the same datecolumn from ID 2 to 11, and then use datediff to obtain time delta between following records.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          This is why I wrote WHERE ... inside the subquery. What you must do is to add necessary conditions into the subquery using correlation to the outer query. Since you didn't provide the query and the columns I'm just quessing the columns but try adding something like the following to the subquery

          ...
          WHERE a1.datecolumn1 < a.datecolumn1
          ...

          A1 is the alias for you table in the subquery and A is alias for your table in the main query.

          The need to optimize rises from a bad design.My articles[^]

          U 1 Reply Last reply
          0
          • W Wendelius

            This is why I wrote WHERE ... inside the subquery. What you must do is to add necessary conditions into the subquery using correlation to the outer query. Since you didn't provide the query and the columns I'm just quessing the columns but try adding something like the following to the subquery

            ...
            WHERE a1.datecolumn1 < a.datecolumn1
            ...

            A1 is the alias for you table in the subquery and A is alias for your table in the main query.

            The need to optimize rises from a bad design.My articles[^]

            U Offline
            U Offline
            User 3770393
            wrote on last edited by
            #5

            Oh it is working;) thx.. but it is working for to long.... maybe another way? something like:

            ....
            Datediff(seconds, datecolumn, (Delete first row from datecolumn and add at the end null))

            Is there any possibility to do that? Because now I have almost the same searching in main query and subquery, and I think this main issue for this long working.

            W 1 Reply Last reply
            0
            • U User 3770393

              Oh it is working;) thx.. but it is working for to long.... maybe another way? something like:

              ....
              Datediff(seconds, datecolumn, (Delete first row from datecolumn and add at the end null))

              Is there any possibility to do that? Because now I have almost the same searching in main query and subquery, and I think this main issue for this long working.

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              If it's taking a long time, it's probably because for each row in the main query a scan is done to the table to fetch the previous/next row. If possible, can you add an index to the table for the column you're using in the subquery. If several columns are used in the subquery condition consider having more than one column in the index.

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              • U User 3770393

                Hi I have a column where are values regarding to time. And In my query I want to add additional column where I will have delta between each record. So e.g. between one record and another remain 34 seconds. For this one I was trying to use DateDiff function, and compare my column with times to the same column but offset by one record. And there is the problem. It looks like date DateDIFF(seconds, column, [subquery]) and I have error that subquery return more than 1 value and it its not appropriate where are following; =, != or something like that. Do you have any ideas? how to manage this problem? Dnorus

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                You can build it on this principle.

                WITH Ordered AS (
                Select IDColumn,DateColumn,ROW_NUMBER() OVER(ORDER BY DateColumn) as rn
                FROM YourTable
                )
                SELECT o2.IDColumn,DateDiff(seconds,o2.DateColumn - o1.DateColumn)
                FROM ordered o1
                join ordered o2
                on o1.rn = o2.rn - 1

                List of common misconceptions

                U 1 Reply Last reply
                0
                • J Jorgen Andersson

                  You can build it on this principle.

                  WITH Ordered AS (
                  Select IDColumn,DateColumn,ROW_NUMBER() OVER(ORDER BY DateColumn) as rn
                  FROM YourTable
                  )
                  SELECT o2.IDColumn,DateDiff(seconds,o2.DateColumn - o1.DateColumn)
                  FROM ordered o1
                  join ordered o2
                  on o1.rn = o2.rn - 1

                  List of common misconceptions

                  U Offline
                  U Offline
                  User 3770393
                  wrote on last edited by
                  #8

                  Okey it seems working and also in a very fast way. But I have a another question. In section With ordered as( ) I have some values declared... and now I dont know where can I put Declare and Set variable.. Ah Ok I didnt have ";".. before With or after declarations So now it is perfect;)

                  modified on Monday, March 28, 2011 4:59 AM

                  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