How to Add a Row Number to Your SQL Query Using row_number and partition by

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2021
  • In this episode, we'll take a look at how to add a row number to our query so that we can refer to that ordering when we use the data for other purposes. In order to do this, we're going to use two very important features of sql: row_number and partition by. Using these, we'll create a row number column for the whole column, and then split up our data and give a row number inside of those groups.
    Needing to hire technical resources for your project? Get the best:
    www.toptal.com/qKaO2b/worlds-...
    For developers looking for a new role, check out:
    www.toptal.com/qKaO2b/worlds-...
    Want to get access to premium content made just for you and have a chat with me? Find me on Patreon :
    / mackenziedataengineering
    Demo of my BZ RDP Cloaker:
    www.patreon.com/posts/how-to-...
    Want to work with me 1:1? Contact me today and book your free 20 min consultation!
    Contact form you can find at www.mackenziemackenzie.com/
    Follow me on social media:
    / mackenziedataanalytics
    / seamacke
    / seamacke
    / seamacke
    / psmackenzie
    Get Microsoft Office including Access:
    click.linksynergy.com/fs-bin/...
    Got a RUclips Channel? I use TubeBuddy, it is awesome. Give it a try:
    www.tubebuddy.com/seanmackenz...
    • How to Add a Row Numbe...
  • НаукаНаука

Комментарии • 20

  • @USA_to_Asia_in_2024
    @USA_to_Asia_in_2024 11 дней назад +1

    GREAT explanation.

  • @sarahgorton8927
    @sarahgorton8927 Год назад +1

    very well explained, thank u!

  • @Oleemil89
    @Oleemil89 Месяц назад +1

    Nice video! I enjoyed the intro song, but people have different expectations i guess. Who is playing and what song is it? Had a nice riff

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  Месяц назад

      I believe that track was called "Ripped" and was included with license as part of the Camtasia studio selections.. 2019 or 2020 version iirc. I thought it was groovy!

  • @sonathena3150
    @sonathena3150 2 года назад +1

    Hello Sean, do you have a copy of the dataset in this discussion? I hope you do have so we can try it by ourselves. Thanks

  • @maheshs8069
    @maheshs8069 Год назад +1

    Sir, will these rownum work for table with million record

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  Год назад

      I don't see why not! Give it a try. It may slow down though. At a point, you may want to just add a column to that table and make it persistent (and faster to work with).

  • @DCh-ks6ug
    @DCh-ks6ug 7 дней назад +1

    is it necessary to use order by function in the row_number function ?

  • @anilmanuka2052
    @anilmanuka2052 2 года назад +1

    How to update empty column a value in declare table

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  2 года назад

      If you are creating a new table, you can use a default to fill values that are not selected. If you wanted a date for example, you could make the field:
      MyDateField datetime default GetDate()

  • @diegovaldes3785
    @diegovaldes3785 Год назад +1

    is there a way that the row number continues the sequence after running the same query again?

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  Год назад

      Great question. It should continue generating more numbers if you added rows to the table, then executed the query again. Is that what you mean?

    • @diegovaldes3785
      @diegovaldes3785 Год назад

      @@seanmackenziedataengineering hmm let me give you an example: I made a query that selects data based on a search criteria and want a sequence number related to each one of the items of this data, then a process unrelated to SQL does things with that data so it does not fit with the search criteria. What I need is that, whenever new data comes in that fits the search data, the sequence number continues where it was left

  • @alekkermd7775
    @alekkermd7775 2 года назад +1

    The background music is very distracting.

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  2 года назад

      Thanks for the feedback! All my newest videos removed the intro music.

    • @USA_to_Asia_in_2024
      @USA_to_Asia_in_2024 11 дней назад

      Check out the mirror and you'll see a crybaby who cries about music and should have bigger fish to fry