First Value in SQL | Last Value SQL | Nth Value SQL | Functions Advanced SQL | Ashutosh Kumar

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 50

  • @AshutoshKumaryt
    @AshutoshKumaryt  2 роки тому +5

    👉 SQL Portfolio Project Indian Census- Part 1
    ua-cam.com/video/I3YvjFfn478/v-deo.html

  • @ankursharma1461
    @ankursharma1461 11 місяців тому +10

    Who are you dear brother, never seen anyone like you explaining all these advanced concepts of sql, just flawless.

    • @AshutoshKumaryt
      @AshutoshKumaryt  11 місяців тому

      Thanks appreciate your words 🙏

    • @OppoTeam-u7s
      @OppoTeam-u7s Місяць тому

      Brother why not you use row between in first query

  • @shrutibajaj7249
    @shrutibajaj7249 Рік тому +1

    Thankyou Ashutosh.
    It's not easy to explain to someone these Advanced SQL concepts in simple terms

  • @Mawa_Broos
    @Mawa_Broos 15 днів тому

    Hi Ashutosh Thank you so much your videos amazing content few days i learn so much knowledge your Videos Thank you so much

  • @perumalbalachandranjj2358
    @perumalbalachandranjj2358 2 роки тому +2

    nth_value works in mysql. Thank you for your clear explanations.

  • @markhausmann3394
    @markhausmann3394 Рік тому +1

    -- You are right. NTH_VALUE is indeed not implemented in SQL Server by now.
    -- Even with the most recent version this feature is not present.
    -- But I think you can work arround this limitation by combining FIRST_VALUE with LAG.
    -- Based on your example the solution should look like this.
    -- Greetings and thumbs up You are a good teacher !
    ;WITH c1 AS (
    SELECT
    *,
    n2_day_sales = LEAD(Sales,1) OVER ( PARTITION BY State ORDER BY Date ),
    n3_day_sales = LEAD(Sales,2) OVER ( PARTITION BY State ORDER BY Date )
    FROM
    first_value_last_value
    )
    SELECT
    *,
    first_day_sales = FIRST_VALUE(Sales) OVER ( PARTITION BY State ORDER BY Date ),
    last_day_sales = LAST_VALUE(Sales) OVER ( PARTITION BY State ORDER BY Date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ),
    second_day_sales = FIRST_VALUE(n2_day_sales) OVER ( PARTITION BY State ORDER BY Date ),
    third_day_sales = FIRST_VALUE(n3_day_sales) OVER ( PARTITION BY State ORDER BY Date )
    FROM
    c1
    ORDER BY ID
    👍

  • @vanyagarg3331
    @vanyagarg3331 Рік тому +2

    great explanation. Deserves much more views and subscribers❤

  • @financetrainer
    @financetrainer 4 місяці тому +1

    Perfectly working

  • @alokpatra2512
    @alokpatra2512 2 роки тому +1

    Your learning skill is so impressive ..

  • @kishanbhise4733
    @kishanbhise4733 Рік тому

    Hi Ashutosh , Great work with this playlist. Appreciate it

  • @akashkukreti09
    @akashkukreti09 5 місяців тому +1

    Keeep it up bro🔥👏

  • @priyanshitrivedi5605
    @priyanshitrivedi5605 Рік тому +1

    Keep up the good work!

  • @shubhankarsatvaya
    @shubhankarsatvaya Місяць тому

    for the 5th day you can use the dense rank
    SELECT empid, state, date,salary
    FROM (
    SELECT empid, state, date, salary
    DENSE_RANK() OVER (PARTITION BY state ORDER BY salary DESC) AS rank
    FROM employee
    ) AS ranked_salaries
    WHERE rank = 5;

  • @adarishanmukh4583
    @adarishanmukh4583 Рік тому

    Thanks bro Your explanation in Top Notch👌

  • @sonukumar-lf1qh
    @sonukumar-lf1qh Рік тому +1

    nice explaination

  • @PamTiwari
    @PamTiwari Рік тому

    Very clear 🙌

  • @ajitmission3227
    @ajitmission3227 2 місяці тому +1

    select * ,
    FirstValue = first_value(Sales) over(partition by state order by date ),
    LastValue = first_value(Sales) over(partition by state order by date desc)
    from dbo.Running_Sum_DataA
    This query is also working fine for last value. why we will large query

  • @abhinavsingh706
    @abhinavsingh706 2 роки тому +1

    We can use Top in sql server in place of Limit.
    Also, first or nth values can also be queried by rank/row_number function.
    Any special scenarios where first/last_value will be useful ?

    • @AshutoshKumaryt
      @AshutoshKumaryt  2 роки тому +1

      Rank would first provide the rank the whatever rank value you want you would need to to put in a temp table or subquery to filter out that rank value, while the above functions saves that extra step

    • @PamTiwari
      @PamTiwari Рік тому

      @@AshutoshKumaryt yes for nth value we can use Sub-query, dense_rank() and in outer query we have to put where condition like dense-rank = 5

  • @BI-Rahul
    @BI-Rahul Рік тому +1

    awesome

  • @geetaarora6721
    @geetaarora6721 Рік тому

    Yes we can use select top n query as an alternative of limit

    • @AshutoshKumaryt
      @AshutoshKumaryt  Рік тому

      Yeah,btw happy Diwali 🪔

    • @geetaarora6721
      @geetaarora6721 Рік тому

      @@AshutoshKumaryt Happy Diwali...isi trh ki informative vedios se sabki life Roshan krte rho..😊

  • @bittupaul9455
    @bittupaul9455 Рік тому

    Ashutosh, can we use rows between current row and unbounded following for last value? As partition is sorted by dates, last value has to be last row in the partition and it doesn't matter which row, we start, last value will always come from last row.

  • @siddhantgupta5301
    @siddhantgupta5301 5 місяців тому

    can we directly us ROW NUMBER function With PARTITION BY clause to get the nth value??

  • @BI-Rahul
    @BI-Rahul Рік тому

    Can you please make the same video using postgreSQL or the Redhift please?

  • @abhimanyutiwari100
    @abhimanyutiwari100 Рік тому

    Hi, I'm still not sure why not whole partition by data is available for last_value()?

  • @nirmalyapratap9927
    @nirmalyapratap9927 Рік тому +1

    Hi for last value desecding order sorting will not work?

  • @userzzzabc
    @userzzzabc Рік тому

    What if instead of first/last it gets replaced with min/max

  • @shreyanvinjamuri
    @shreyanvinjamuri Рік тому +1

    THis is so confusing and not so clear...!!

  • @vijayavenkatasatyaavinashk7760
    @vijayavenkatasatyaavinashk7760 9 місяців тому

    we can find the nth value like below
    SELECT *,
    FIRST_VALUE(sales) OVER (partition by state ORDER BY date) AS first_value,
    LAST_VALUE(sales) OVER (partition by state ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_value,
    (SELECT sales FROM dbo.value s2 where s1.state = s2.state ORDER BY date OFFSET 2 /*n-1*/ ROWS FETCH NEXT 1 ROWS ONLY) AS nth_value
    FROM dbo.value s1;

  • @pranishsonone7839
    @pranishsonone7839 Рік тому +1

    Aabe Hindi nahi aati ky

  • @markhausmann3394
    @markhausmann3394 Рік тому +1

    -- You are right. NTH_VALUE is indeed not implemented in SQL Server by now.
    -- Even with the most recent version this feature is not present.
    -- But I think you can work arround this limitation by combining FIRST_VALUE with LAG.
    -- Based on your example the solution should look like this.
    -- Greetings and thumbs up You are a good teacher !
    ;WITH c1 AS (
    SELECT
    *,
    n2_day_sales = LEAD(Sales,1) OVER ( PARTITION BY State ORDER BY Date ),
    n3_day_sales = LEAD(Sales,2) OVER ( PARTITION BY State ORDER BY Date )
    FROM
    first_value_last_value
    )
    SELECT
    *,
    first_day_sales = FIRST_VALUE(Sales) OVER ( PARTITION BY State ORDER BY Date ),
    last_day_sales = LAST_VALUE(Sales) OVER ( PARTITION BY State ORDER BY Date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ),
    second_day_sales = FIRST_VALUE(n2_day_sales) OVER ( PARTITION BY State ORDER BY Date ),
    third_day_sales = FIRST_VALUE(n3_day_sales) OVER ( PARTITION BY State ORDER BY Date )
    FROM
    c1
    ORDER BY ID