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. query needed

query needed

Scheduled Pinned Locked Moved Database
databasequestionsalesworkspace
9 Posts 4 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
    Ashok Nalam
    wrote on last edited by
    #1

    I have table data like this Id(PrimaryKey) Time Value Time Order 1 06:00AM 1 2 07:00AM 2 3 08:00AM 3 4 09:00AM 4 5 10:00AM 5 6 11:00AM 6 If I get query like select * from table order by Time Order then I will above data My requirement is if I have 2 customers • Customer1 configure start time as 08:00AM then I have to bind drop down first value as 08:00AM , 09:00AM, 10:00AM, 11:00AM, 06:00AM, 07:00AM • Customer1 configure start time as 10:00AM then I have to bind drop down first value as 10:00AM , 11:00AM, 06:00AM, 07:00AM, 08:00AM, 09:00AM So it has basically start from ( based on configuration by customer specific) to end time then start time onwards. Can you please how can we write SQL query above and how can I achive above requirement? Thank, Ashok

    Ashok Nalam

    I N 2 Replies Last reply
    0
    • A Ashok Nalam

      I have table data like this Id(PrimaryKey) Time Value Time Order 1 06:00AM 1 2 07:00AM 2 3 08:00AM 3 4 09:00AM 4 5 10:00AM 5 6 11:00AM 6 If I get query like select * from table order by Time Order then I will above data My requirement is if I have 2 customers • Customer1 configure start time as 08:00AM then I have to bind drop down first value as 08:00AM , 09:00AM, 10:00AM, 11:00AM, 06:00AM, 07:00AM • Customer1 configure start time as 10:00AM then I have to bind drop down first value as 10:00AM , 11:00AM, 06:00AM, 07:00AM, 08:00AM, 09:00AM So it has basically start from ( based on configuration by customer specific) to end time then start time onwards. Can you please how can we write SQL query above and how can I achive above requirement? Thank, Ashok

      Ashok Nalam

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      I wouldn't write this as SQL. I would populate the list that you use in the dropdown in c#/vb.net using data from the query that you already have.

      A N 2 Replies Last reply
      0
      • A Ashok Nalam

        I have table data like this Id(PrimaryKey) Time Value Time Order 1 06:00AM 1 2 07:00AM 2 3 08:00AM 3 4 09:00AM 4 5 10:00AM 5 6 11:00AM 6 If I get query like select * from table order by Time Order then I will above data My requirement is if I have 2 customers • Customer1 configure start time as 08:00AM then I have to bind drop down first value as 08:00AM , 09:00AM, 10:00AM, 11:00AM, 06:00AM, 07:00AM • Customer1 configure start time as 10:00AM then I have to bind drop down first value as 10:00AM , 11:00AM, 06:00AM, 07:00AM, 08:00AM, 09:00AM So it has basically start from ( based on configuration by customer specific) to end time then start time onwards. Can you please how can we write SQL query above and how can I achive above requirement? Thank, Ashok

        Ashok Nalam

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        SELECT * FROM Table
        WHERE TimeOrder >= @StartOrder

        UNION ALL

        SELECT * FROM Table
        WHERE TimeOrder < @StartOrder


        only two letters away from being an asset

        I 1 Reply Last reply
        0
        • I i j russell

          I wouldn't write this as SQL. I would populate the list that you use in the dropdown in c#/vb.net using data from the query that you already have.

          A Offline
          A Offline
          Ashok Nalam
          wrote on last edited by
          #4

          I need sql statement for reporting purpose also please provide the sql

          Ashok Nalam

          1 Reply Last reply
          0
          • I i j russell

            I wouldn't write this as SQL. I would populate the list that you use in the dropdown in c#/vb.net using data from the query that you already have.

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            And what they are not using a dropdown list? What if it is needed for a report or a view?


            only two letters away from being an asset

            I 1 Reply Last reply
            0
            • N Not Active

              And what they are not using a dropdown list? What if it is needed for a report or a view?


              only two letters away from being an asset

              I Offline
              I Offline
              i j russell
              wrote on last edited by
              #6

              The initial request said "bind drop down" so I assumed that they wanted to populate a list to fill a dropdown.

              1 Reply Last reply
              0
              • N Not Active

                SELECT * FROM Table
                WHERE TimeOrder >= @StartOrder

                UNION ALL

                SELECT * FROM Table
                WHERE TimeOrder < @StartOrder


                only two letters away from being an asset

                I Offline
                I Offline
                i j russell
                wrote on last edited by
                #7

                The only way to guarantee the order of a resultset is to use an ORDER BY clause;

                SELECT Id, TimeValue, TimeOrder
                FROM
                (
                SELECT Id, TimeValue, TimeOrder, 0 as result
                FROM #data
                WHERE TimeOrder >= @StartOrder
                UNION ALL
                SELECT Id, TimeValue, TimeOrder, 1
                FROM #data
                WHERE TimeOrder < @StartOrder
                ) as t
                ORDER BY result ASC, TimeOrder asc

                N 1 Reply Last reply
                0
                • I i j russell

                  The only way to guarantee the order of a resultset is to use an ORDER BY clause;

                  SELECT Id, TimeValue, TimeOrder
                  FROM
                  (
                  SELECT Id, TimeValue, TimeOrder, 0 as result
                  FROM #data
                  WHERE TimeOrder >= @StartOrder
                  UNION ALL
                  SELECT Id, TimeValue, TimeOrder, 1
                  FROM #data
                  WHERE TimeOrder < @StartOrder
                  ) as t
                  ORDER BY result ASC, TimeOrder asc

                  N Offline
                  N Offline
                  Nisha Agrawal
                  wrote on last edited by
                  #8

                  yes, you are right. If records are already not in order in what we need then we need to sort it as per the requirement.

                  I 1 Reply Last reply
                  0
                  • N Nisha Agrawal

                    yes, you are right. If records are already not in order in what we need then we need to sort it as per the requirement.

                    I Offline
                    I Offline
                    i j russell
                    wrote on last edited by
                    #9

                    That's not what I said. I said that the only way to guarantee the order of the results is to use an ORDER BY clause. Without the sort, the results may or may not be returned in the order that you want depending upon how the query optimizer decides to handle your request.

                    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