BOSCH SQL Interview Question - FIRST_VALUE() WINDOWS Function

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

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

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

    With cte as ( select * , count (category) over (order by (select null) rows between unbounded preceding and current row) as rn from brands )
    Select coalesce (category, max(category) over (partition by rn)) as category,
    Brand_name
    From cte;

  • @prajju8114
    @prajju8114 2 หลายเดือนก่อน +1

    with cte_null as
    (
    select *,ROW_NUMBER() over(order by (select null)) as rn from brands
    ),
    cte_2 as
    (
    select *,case when category is null then 0 else 1 end cnt from cte_null
    ),
    cte_3 as
    (
    select category,brand_name,sum(cnt) over(order by rn) as mn from cte_2
    )
    select first_value(category) over (partition by mn order by mn) as category,brand_name from cte_3

  • @iamram436
    @iamram436 7 หลายเดือนก่อน +3

    with cte as(select * , 1 as num from brands),
    cte2 as (select *,row_number()over(order by num) as rn from cte),
    cte3 as(select *,min(category) over(order by rn asc) as category2 from cte2)
    select category2 as category,brand_name from cte3

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

      Yes, it works. Thanks for posting the different approach Ram.

  • @murugan-ty7nd
    @murugan-ty7nd 5 หลายเดือนก่อน +3

    with CTE as
    (
    Select
    row_number() over (order by (select null))as rn,
    category,Brand_name,
    (case when category is null then 0 else 1 end) as m
    from brands
    ), cte2 as
    (Select rn,category,Brand_name,sum(m) over (order by rn)as n from cte)
    select rn,first_value(category)over(partition by n order by rn ) as category,Brand_name from cte2

  • @TheBlacklist244
    @TheBlacklist244 6 หลายเดือนก่อน +2

    Can use NTIle with first_value also :
    Select first_value(category) over(partition by s.n) as category, brand_name
    from (Select *, ntile(2) over() as n from brands) as s;

    • @CloudChallengers
      @CloudChallengers  6 หลายเดือนก่อน

      @TheBlacklist244, thanks for posting different approach. But your query seems incomplete. Can you share the full query here?

  • @rakeshracharla3301
    @rakeshracharla3301 5 หลายเดือนก่อน +2

    My Solution:
    select case when brand_name in ('5-star','dairymilk','perk','eclair') then 'chocolate'
    when brand_name in ('Britania','goodday','boost') then 'Biscuits' end as category,brand_name
    from chocolate_category;

    • @CloudChallengers
      @CloudChallengers  5 หลายเดือนก่อน

      You can think of any other better solution.

  • @Ujjwalmishra-t1w
    @Ujjwalmishra-t1w 2 หลายเดือนก่อน +1

    Sir u are a nice person

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

      Thanks for sharing your valuable feedback

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

    Nice explanation

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

    Select first_value(Category) over (Partition by Seg order by RN) as Category,Brand_Name from
    (Select *,Sum(Flag) over (Order by Rn) as Seg from
    (Select *,Case When Category Is not null then 1 else 0 end as Flag,row_number() over (Order by (Select Null)) as Rn
    from Brands)N)N1;

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

    -- Solution using approach unbounded preceding
    WITH brand_vw AS (
    SELECT
    *,
    LAST_VALUE(category IGNORE NULLS) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS new_category
    FROM
    brands
    )
    SELECT new_category, brand_name
    FROM brand_vw
    ORDER BY id;

  • @rajkumarpanigrahi2013
    @rajkumarpanigrahi2013 4 หลายเดือนก่อน +1

    Hey brother you have not been uploading vedio since last week
    Why are you not uploading video please upload

    • @CloudChallengers
      @CloudChallengers  4 หลายเดือนก่อน +1

      @rajkumarpanigrahi2013, Thanks for the comment.
      New video on SQL is out now. Please watch and like the video.

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

    with cte as(
    select * ,case when category is null then 0 else 1 end nn,row_number() over(order by (select null)) rn
    from brands
    ),f as(
    select category,brand_name,sum(nn) over(order by rn)m,rn
    from cte)
    select first_value(category) over(partition by m order by rn)category ,brand_name
    from f

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      Thanks for sharing different approach Vaibhav.

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

    Wecan use lag and case for this query,

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      Thanks Nandhini. It would be much appreciated if you could share the query here.

    • @DE_Pranav
      @DE_Pranav 7 หลายเดือนก่อน

      can you please share query.

  • @vijaygupta7059
    @vijaygupta7059 7 หลายเดือนก่อน +2

    my solution in MSSQL DB ::
    with cte as
    (
    Select *,count(category)over( order by (select null) rows between unbounded preceding and 0 following ) as new from brands
    )
    Select FIRST_VALUE(category)over(partition by new order by category desc) as category,brand_name from cte

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      Thanks for posting different approach Vijay. Keep posting different approaches for upcoming videos as well.

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

    You can opt simple solution using LAG() and CAse.
    SELECT CASE
    WHEN category is null THEN LAG(category) over()
    ELSE category
    END AS category, brand_name
    FROM brands;

    • @DE_Pranav
      @DE_Pranav 7 หลายเดือนก่อน +2

      not getting required op with this. can you check?

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      @LeaningGCP, your query seems incomplete. Can you share the full query here?

    • @LeaningGCP
      @LeaningGCP 7 หลายเดือนก่อน

      @@CloudChallengers @DE_Pranav Correct, My bad. It's not full filling the desired outcome. Tested with just one row update :(

    • @tagurreddy3549
      @tagurreddy3549 5 หลายเดือนก่อน +1

      @@DE_Pranav
      1 SELECT
      2 CASE
      3 WHEN CATEGORY IS NOT NULL THEN CATEGORY
      4 ELSE LAG(CATEGORY IGNORE NULLS) OVER (ORDER BY ROWNUM)
      5 END "CATEGEORY",
      6 BRAND_NAME "BRAND NAME"
      7 FROM
      8* brands
      9 /
      CATEGEORY BRAND NAME
      -------------------------------------------------- --------------------------------------------------
      chocolates 5-star
      chocolates dairy milk
      chocolates perk
      chocolates eclair
      Biscuits Britania
      Biscuits good day
      Biscuits boost
      7 rows selected.

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

    with cte as(
    select *,ROW_NUMBER() over(order by (select null)) as flag from brands)
    ,
    cte1 as(
    select *,sum(case when category is not null then 1 else 0 end) over (order by flag) as
    new_flag
    from cte)
    select FIRST_VALUE(category) over(partition by new_flag order by flag) as category,brand_name from cte1

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      Thanks for quick response Ajay. This query works as well.

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

    you have used CTE not view.

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      That's right Sai. Thanks for highlighting.

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

    with cte_null AS
    (SELECT
    *,
    ROW_NUMBER() over(order by (select null)) AS rn
    FROM brands)
    SELECT
    first_value(category) OVER(PARTITION BY cn ORDER BY rn) AS category,
    brand_name
    FROM
    (SELECT
    *,
    COUNT(category) OVER(ORDER BY rn) AS cn
    FROM cte_null) category_cnt;

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

    provide your mail address in the description box so that someone can mail you if he/she is having any question to ask.
    please try to solve this question.
    id (timestamp) emp_id (integer)
    1/13/2024 9:25 10
    1/13/2024 19:35 10
    1/16/2024 9:10 10
    1/16/2024 18:10 10
    2/11/2024 9:07 10
    2/11/2024 19:20 10
    2/17/2024 8:40 17
    2/17/2024 18:04 17
    3/23/2024 9:20 10
    3/23/2024 18:30 10
    query to find the employees total working hours during weekends.
    output should be
    emp_id Total weekend
    Working_hours
    17 9.4
    10 29.55

    • @CloudChallengers
      @CloudChallengers  7 หลายเดือนก่อน

      Thanks for suggestion Chandan, I added email id in the description box now. I also noted down the questions you are posting. I will post the videos on those questions soon.

    • @devendrabarasker4987
      @devendrabarasker4987 7 หลายเดือนก่อน

      Try this code -
      with cte as(
      select emp_id,CAST(datehour AS date)date,CAST(datehour AS time)hm,DATEPART(WEEKDAY,datehour)wd
      from emp_work
      ),cte2 as(
      select DATE, emp_id,MIN(hm)start_time,MAX(hm)end_time from cte
      where wd in(1,7)
      group by DATE ,emp_id
      ),cte3 as(
      select *,DATEDIFF(MINUTE,start_time,end_time)*1.0/60 diff from cte2
      )select distinct(emp_id),SUM(diff) over (partition by emp_id)Total_Weekend_Working_Hours from cte3

    • @tagurreddy3549
      @tagurreddy3549 7 หลายเดือนก่อน

      -- Create the table
      CREATE TABLE work_hours (
      id TIMESTAMP,
      emp_id INTEGER
      );
      -- Insert the records
      INSERT ALL
      INTO work_hours VALUES (TO_TIMESTAMP('2024-01-13 09:25:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-01-13 19:35:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-01-16 09:10:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-01-16 18:10:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-02-11 09:07:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-02-11 19:20:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-02-17 08:40:00', 'YYYY-MM-DD HH24:MI:SS'), 17)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-02-17 18:04:00', 'YYYY-MM-DD HH24:MI:SS'), 17)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-03-23 09:20:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      INTO work_hours VALUES (TO_TIMESTAMP('2024-03-23 18:30:00', 'YYYY-MM-DD HH24:MI:SS'), 10)
      SELECT * FROM dual; SQL>
      SQL> SELECT
      2 emp_id,
      3 ROUND(SUM(EXTRACT(HOUR FROM working_hours) + EXTRACT(MINUTE FROM working_hours) / 60), 2) AS Total_weekend_Working_hours
      4 FROM (
      5 SELECT
      6 emp_id,
      7 MAX(id) - MIN(id) AS working_hours
      8 FROM
      9 work_hours
      10 WHERE
      11 TO_CHAR(id, 'DY', 'NLS_DATE_LANGUAGE=American') IN ('SAT', 'SUN') -- Filter weekends
      12 GROUP BY
      13 emp_id,
      14 TO_CHAR(id, 'YYYY-MM-DD') -- Group by date to get total hours for each weekend day
      15 ) weekends
      16 GROUP BY
      17 emp_id
      18 ORDER BY
      19 emp_id;
      EMP_ID TOTAL_WEEKEND_WORKING_HOURS
      ---------- ---------------------------
      10 29.55
      17 9.4

    • @CloudChallengers
      @CloudChallengers  6 หลายเดือนก่อน

      video is out on this this question. th-cam.com/video/qPIMa7YtXqg/w-d-xo.htmlsi=YKGISgDmBN-NP3Cy

    • @anirbanbiswas7624
      @anirbanbiswas7624 5 หลายเดือนก่อน

      with cte as(select *,date(id) as date_r ,coalesce(lead(id) over(order by id),0)as logout_time,
      dayname(id)as day_name from employee_logs),
      cte2 as(select * from cte where day_name 'Tuesday' group by date_r),
      cte3 as(select *,timestampdiff(hour,id,logout_time)as hrs from cte2 order by emp_id)
      select emp_id,sum(hrs) as total_weekend_hrs from cte3 group by emp_id

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

    ------ solution 1 ---------------------------------------------------------------------------------------
    with cte as (
    select *
    ,count(category) over (order by brand_name) as grp
    from #brands
    )
    select first_value(category) over (partition by grp order by grp) as filled_category
    ,brand_name
    from cte
    ------ solution 2 ---------------------------------------------------------------------------------------
    with cte as (
    select *
    ,count(category) over (order by brand_name) as grp
    from #brands
    )
    SELECT isnull( category, max(category) over(partition by grp order by grp)) AS category
    , brand_name
    FROM cte;

    • @CloudChallengers
      @CloudChallengers  6 หลายเดือนก่อน

      @shashank_1180, these queries are not giving expected output. Bcz of the reason, you are doing sorting on brand_name.