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 Query

SQL Query

Scheduled Pinned Locked Moved Database
database
8 Posts 6 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.
  • H Offline
    H Offline
    heinthuwin
    wrote on last edited by
    #1

    I have one column table with negative and positive values and want to display positive and negative values in different columns using SQL Query. Column -10000 -17000 16000 25000 output should be like A B ----------------- -10000 16000 -17000 25000

    B N P 3 Replies Last reply
    0
    • H heinthuwin

      I have one column table with negative and positive values and want to display positive and negative values in different columns using SQL Query. Column -10000 -17000 16000 25000 output should be like A B ----------------- -10000 16000 -17000 25000

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      And how do you determine that 16000 and -10000 belong into the same column, but not 16000 and -25000? What's the rule behind that?

      1 Reply Last reply
      0
      • H heinthuwin

        I have one column table with negative and positive values and want to display positive and negative values in different columns using SQL Query. Column -10000 -17000 16000 25000 output should be like A B ----------------- -10000 16000 -17000 25000

        N Offline
        N Offline
        NickPace
        wrote on last edited by
        #3

        One of the problems you will have with creating a query to do this is that the column of numbers will most likely not have an equal number of positive and negative values. Even if it does, I don't know of a way to create a single query to create the output you illustrate. The best I could do in this situation is the following which will output the same number of rows as in the original table but simply put a NULL value in the column that does not apply:

        SELECT
        CASE
        WHEN NumberField > 0 THEN NumberField
        ELSE NULL
        END AS PositiveNumbers,
        CASE
        WHEN NumberField < 0 THEN NumberField
        ELSE NULL
        END AS NegativeNumbers
        FROM NumbersTable

        Of course, you also have to decide what exactly to do with values of zero.

        -NP Never underestimate the creativity of the end-user.

        H 1 Reply Last reply
        0
        • H heinthuwin

          I have one column table with negative and positive values and want to display positive and negative values in different columns using SQL Query. Column -10000 -17000 16000 25000 output should be like A B ----------------- -10000 16000 -17000 25000

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          With SQL Server 2012?

          SELECT A.[Column] A
          , B.[Column] B
          FROM (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column] DESC) RN , [Column] FROM [Table] WHERE [Column]<0)A
          FULL OUTER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column]) RN , [Column] FROM [Table] WHERE [Column]>0)B
          ON A.RN=B.RN

          T M 2 Replies Last reply
          0
          • P PIEBALDconsult

            With SQL Server 2012?

            SELECT A.[Column] A
            , B.[Column] B
            FROM (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column] DESC) RN , [Column] FROM [Table] WHERE [Column]<0)A
            FULL OUTER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column]) RN , [Column] FROM [Table] WHERE [Column]>0)B
            ON A.RN=B.RN

            T Offline
            T Offline
            Tim Carmichael
            wrote on last edited by
            #5

            A good example of CAN be done versus SHOULD be done... But, well done...

            P 1 Reply Last reply
            0
            • P PIEBALDconsult

              With SQL Server 2012?

              SELECT A.[Column] A
              , B.[Column] B
              FROM (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column] DESC) RN , [Column] FROM [Table] WHERE [Column]<0)A
              FULL OUTER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY [Column]) RN , [Column] FROM [Table] WHERE [Column]>0)B
              ON A.RN=B.RN

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Erk - I mean I like ROW_NUMBER but that is just... I agree with Tim!

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • T Tim Carmichael

                A good example of CAN be done versus SHOULD be done... But, well done...

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                What? I didn't use CTEs. :-D

                1 Reply Last reply
                0
                • N NickPace

                  One of the problems you will have with creating a query to do this is that the column of numbers will most likely not have an equal number of positive and negative values. Even if it does, I don't know of a way to create a single query to create the output you illustrate. The best I could do in this situation is the following which will output the same number of rows as in the original table but simply put a NULL value in the column that does not apply:

                  SELECT
                  CASE
                  WHEN NumberField > 0 THEN NumberField
                  ELSE NULL
                  END AS PositiveNumbers,
                  CASE
                  WHEN NumberField < 0 THEN NumberField
                  ELSE NULL
                  END AS NegativeNumbers
                  FROM NumbersTable

                  Of course, you also have to decide what exactly to do with values of zero.

                  -NP Never underestimate the creativity of the end-user.

                  H Offline
                  H Offline
                  heinthuwin
                  wrote on last edited by
                  #8

                  thanks! that is useful for me.

                  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