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. OVER (PARTITION BY ORDER BY )

OVER (PARTITION BY ORDER BY )

Scheduled Pinned Locked Moved Database
databasesql-serverhelptutorial
13 Posts 7 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 Ambertje

    Hello everyone, I'm trying to understand this function. Someone wrote this piece of code and I would like to use to do something simular. On a daily basis we read .txt files into our SQL database through SSIS. The data that's going through is growing fast and takes a long time to process. (for example a year of data is read every day into the db) I want to make a dayset so that we only read one single day into the DB on top of the existing data and not the whole year. Someone wrote this peace of code:

    Select * , ROW_NUMBER() OVER (PARTITION BY PK_Assignment ORDER BY DATE, TIME) as LineNbr
    from ProductivityColliDagSet

    I was wondering what the Partition By statement means. My guess was that you have to place the fields there that makes the record unique. Can someone help me out with this because I read so many things on the internet and still I don't understand. Also I don't have much experience with SQL, just the basics. Ambertje

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    It determines the grouping. Play around with below script;

    BEGIN TRANSACTION

    CREATE TABLE SomeTest(
    	Field1 INTEGER,
    	Field2 CHAR(1),
    	Data VARCHAR(50))
    	
    INSERT INTO SomeTest VALUES (1, 'C', 'Test')
    INSERT INTO SomeTest VALUES (1, 'A', 'Test')
    INSERT INTO SomeTest VALUES (7, 'C', 'Test')
    INSERT INTO SomeTest VALUES (7, 'A', 'Test')
    INSERT INTO SomeTest VALUES (4, 'C', 'Test')
    
    SELECT \*
         , ROW\_NUMBER() OVER ( PARTITION BY Field2 ORDER BY Field1 ) 
      FROM SomeTest
    

    ROLLBACK

    If you change the field that's being partitioned by to "Field1", you'll see a different grouping in the result-set (with each group receiving a unique numbering). It's often used with a primary key because some idiot forgot to add an identity-field :)

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    P A 2 Replies Last reply
    0
    • L Lost User

      It determines the grouping. Play around with below script;

      BEGIN TRANSACTION

      CREATE TABLE SomeTest(
      	Field1 INTEGER,
      	Field2 CHAR(1),
      	Data VARCHAR(50))
      	
      INSERT INTO SomeTest VALUES (1, 'C', 'Test')
      INSERT INTO SomeTest VALUES (1, 'A', 'Test')
      INSERT INTO SomeTest VALUES (7, 'C', 'Test')
      INSERT INTO SomeTest VALUES (7, 'A', 'Test')
      INSERT INTO SomeTest VALUES (4, 'C', 'Test')
      
      SELECT \*
           , ROW\_NUMBER() OVER ( PARTITION BY Field2 ORDER BY Field1 ) 
        FROM SomeTest
      

      ROLLBACK

      If you change the field that's being partitioned by to "Field1", you'll see a different grouping in the result-set (with each group receiving a unique numbering). It's often used with a primary key because some idiot forgot to add an identity-field :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #5

      Eddy Vluggen wrote:

      some idiot forgot to added an identity-field

      FTFY

      You'll never get very far if all you do is follow instructions.

      L 1 Reply Last reply
      0
      • P PIEBALDconsult

        Eddy Vluggen wrote:

        some idiot forgot to added an identity-field

        FTFY

        You'll never get very far if all you do is follow instructions.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        I was more expecting at someone pointing out the option to count items based on particular groupings. Sounds like you really dislike the identity-field? :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        J 1 Reply Last reply
        0
        • L Lost User

          I was more expecting at someone pointing out the option to count items based on particular groupings. Sounds like you really dislike the identity-field? :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

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

          Real databases use a sequence. ;-)

          Wrong is evil and must be defeated. - Jeff Ello[^]

          M 1 Reply Last reply
          0
          • A Ambertje

            Hello everyone, I'm trying to understand this function. Someone wrote this piece of code and I would like to use to do something simular. On a daily basis we read .txt files into our SQL database through SSIS. The data that's going through is growing fast and takes a long time to process. (for example a year of data is read every day into the db) I want to make a dayset so that we only read one single day into the DB on top of the existing data and not the whole year. Someone wrote this peace of code:

            Select * , ROW_NUMBER() OVER (PARTITION BY PK_Assignment ORDER BY DATE, TIME) as LineNbr
            from ProductivityColliDagSet

            I was wondering what the Partition By statement means. My guess was that you have to place the fields there that makes the record unique. Can someone help me out with this because I read so many things on the internet and still I don't understand. Also I don't have much experience with SQL, just the basics. Ambertje

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #8

            Ambertje wrote:

            I want to make a dayset so that we only read one single day into the DB on top of the existing data and not the whole year.

            I don't see what that has to do with partitions - the database of course wouldn't have anything to do with reading files.

            Ambertje wrote:

            and takes a long time to process.

            Someone should optimize that process. I wouldn't be surprised if that means moving it out of SSIS but maybe it can be sped up as is. Normally one profiles a process by actually timing the various parts and then using that data to determine where things can be made faster. Although your description might suggest that every day you read the previous years data. If so one obvious optimization there would be to some producing a file that has a years worth of data. How one does that depends on the data.

            1 Reply Last reply
            0
            • J Jorgen Andersson

              Real databases use a sequence. ;-)

              Wrong is evil and must be defeated. - Jeff Ello[^]

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

              Begone with your Oracle concepts bah humbug!

              Never underestimate the power of human stupidity RAH

              J 1 Reply Last reply
              0
              • A Ambertje

                Hello everyone, I'm trying to understand this function. Someone wrote this piece of code and I would like to use to do something simular. On a daily basis we read .txt files into our SQL database through SSIS. The data that's going through is growing fast and takes a long time to process. (for example a year of data is read every day into the db) I want to make a dayset so that we only read one single day into the DB on top of the existing data and not the whole year. Someone wrote this peace of code:

                Select * , ROW_NUMBER() OVER (PARTITION BY PK_Assignment ORDER BY DATE, TIME) as LineNbr
                from ProductivityColliDagSet

                I was wondering what the Partition By statement means. My guess was that you have to place the fields there that makes the record unique. Can someone help me out with this because I read so many things on the internet and still I don't understand. Also I don't have much experience with SQL, just the basics. Ambertje

                G Offline
                G Offline
                GuyThiebaut
                wrote on last edited by
                #10

                The best way to understand this is to create a couple of tables and experiment. It is quite difficult to explain what is going on in a partition statement - the closest I can get is transposing data in Excel. However that does not explain how to go about transposing the data. Think of it as changing column headings into row contents and row contents into column headings.

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                A 1 Reply Last reply
                0
                • L Lost User

                  It determines the grouping. Play around with below script;

                  BEGIN TRANSACTION

                  CREATE TABLE SomeTest(
                  	Field1 INTEGER,
                  	Field2 CHAR(1),
                  	Data VARCHAR(50))
                  	
                  INSERT INTO SomeTest VALUES (1, 'C', 'Test')
                  INSERT INTO SomeTest VALUES (1, 'A', 'Test')
                  INSERT INTO SomeTest VALUES (7, 'C', 'Test')
                  INSERT INTO SomeTest VALUES (7, 'A', 'Test')
                  INSERT INTO SomeTest VALUES (4, 'C', 'Test')
                  
                  SELECT \*
                       , ROW\_NUMBER() OVER ( PARTITION BY Field2 ORDER BY Field1 ) 
                    FROM SomeTest
                  

                  ROLLBACK

                  If you change the field that's being partitioned by to "Field1", you'll see a different grouping in the result-set (with each group receiving a unique numbering). It's often used with a primary key because some idiot forgot to add an identity-field :)

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  A Offline
                  A Offline
                  Ambertje
                  wrote on last edited by
                  #11

                  Eddy, This example made it clear to me, I think I understand what it's doing. Now I can try to rewrite the code to my needs. Thank you for taking the time to help me :thumbsup: Ambertje

                  1 Reply Last reply
                  0
                  • G GuyThiebaut

                    The best way to understand this is to create a couple of tables and experiment. It is quite difficult to explain what is going on in a partition statement - the closest I can get is transposing data in Excel. However that does not explain how to go about transposing the data. Think of it as changing column headings into row contents and row contents into column headings.

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    A Offline
                    A Offline
                    Ambertje
                    wrote on last edited by
                    #12

                    Many thanks to everyone who is helping me, I really appreciate the effort you put into it to help newbe's. Ambertje

                    1 Reply Last reply
                    0
                    • M Mycroft Holmes

                      Begone with your Oracle concepts bah humbug!

                      Never underestimate the power of human stupidity RAH

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

                      :)

                      Wrong is evil and must be defeated. - Jeff Ello[^]

                      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