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

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ม.ค. 2025

ความคิดเห็น • 51

  • @AshutoshKumaryt
    @AshutoshKumaryt  2 ปีที่แล้ว +5

    👉 SQL Portfolio Project Indian Census- Part 1
    th-cam.com/video/I3YvjFfn478/w-d-xo.html

  • @ankursharma1461
    @ankursharma1461 ปีที่แล้ว +10

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

    • @AshutoshKumaryt
      @AshutoshKumaryt  ปีที่แล้ว

      Thanks appreciate your words 🙏

    • @OppoTeam-u7s
      @OppoTeam-u7s 3 หลายเดือนก่อน

      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

  • @vanyagarg3331
    @vanyagarg3331 ปีที่แล้ว +2

    great explanation. Deserves much more views and subscribers❤

  • @Mawa_Broos
    @Mawa_Broos 2 หลายเดือนก่อน

    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.

  • @alokpatra2512
    @alokpatra2512 2 ปีที่แล้ว +1

    Your learning skill is so impressive ..

  • @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
    👍

  • @kishanbhise4733
    @kishanbhise4733 ปีที่แล้ว

    Hi Ashutosh , Great work with this playlist. Appreciate it

  • @priyanshitrivedi5605
    @priyanshitrivedi5605 ปีที่แล้ว +1

    Keep up the good work!

  • @akashkukreti09
    @akashkukreti09 7 หลายเดือนก่อน +1

    Keeep it up bro🔥👏

  • @adarishanmukh4583
    @adarishanmukh4583 2 ปีที่แล้ว

    Thanks bro Your explanation in Top Notch👌

  • @financetrainer
    @financetrainer 6 หลายเดือนก่อน +1

    Perfectly working

  • @PamTiwari
    @PamTiwari 2 ปีที่แล้ว

    Very clear 🙌

  • @shubhankarsatvaya
    @shubhankarsatvaya 3 หลายเดือนก่อน +1

    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;

  • @sonukumar-lf1qh
    @sonukumar-lf1qh ปีที่แล้ว +1

    nice explaination

  • @abhimanyutiwari100
    @abhimanyutiwari100 ปีที่แล้ว

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

  • @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 7 หลายเดือนก่อน

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

  • @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 2 ปีที่แล้ว

      @@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 2 ปีที่แล้ว +1

    awesome

  • @nirmalyapratap9927
    @nirmalyapratap9927 2 ปีที่แล้ว +1

    Hi for last value desecding order sorting will not work?

  • @BI-Rahul
    @BI-Rahul 2 ปีที่แล้ว

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

  • @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..😊

  • @userzzzabc
    @userzzzabc ปีที่แล้ว

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

  • @abhishekkumar-official2650
    @abhishekkumar-official2650 15 วันที่ผ่านมา

    why you left making youtube videos

  • @shreyanvinjamuri
    @shreyanvinjamuri ปีที่แล้ว +1

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

    • @AshutoshKumaryt
      @AshutoshKumaryt  ปีที่แล้ว

      Watch for 5 times at first it will be confusing

  • @ajitmission3227
    @ajitmission3227 3 หลายเดือนก่อน +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

  • @vijayavenkatasatyaavinashk7760
    @vijayavenkatasatyaavinashk7760 11 หลายเดือนก่อน

    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