SQL Interview Question | 21 |

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

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

  • @VishalSingh-ul2ko
    @VishalSingh-ul2ko 9 วันที่ผ่านมา +1

    Great explaining..Your step-by-step breakdown made it easy to understand the logic and purpose behind each part..

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      thanks.

  • @Harishsatya-b4b
    @Harishsatya-b4b 10 วันที่ผ่านมา +1

    Well Explained 👍

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      Thank you 🙂

  • @narendra742
    @narendra742 9 วันที่ผ่านมา +1

    Nicely explained

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      Thanks.

  • @srikanth2610
    @srikanth2610 9 วันที่ผ่านมา +1

    Simply you can filter the date by state = 'approved'

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      But how do you sum multiple approved transactions and transaction counts.

  • @SundeepKumarModiboina
    @SundeepKumarModiboina 9 วันที่ผ่านมา +1

    Just a small change, used IIF insted of Case when
    WITH cte AS ( SELECT *,FORMAT(trans_date, 'yyy-MM') as month FROM transactions)
    SELECT month, country, count(*) AS trans_count, SUM(amount) AS trans_total_Amount, SUM(IIF(state = 'Approved', 1, 0)) AS approved_count, SUM(IIF(state = 'Approved', amount, 0)) AS approved_total_amount
    FROM cte
    GROUP BY month, country

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      Nice 👍

  • @user-gq6cg3ls7f
    @user-gq6cg3ls7f 10 วันที่ผ่านมา +1

    another approach
    with cte as(
    select FORMAT(trans_date,'yyyy-MM') as month,country, count(*) cnt, sum(amount) trans_total_amount
    from transactions_thus
    group by FORMAT(trans_date,'yyyy-MM') , country
    ), cte2 as(
    select FORMAT(trans_date,'yyyy-MM') as month2, count(*) approved, amount from transactions_thus where state='approved'
    group by amount, FORMAT(trans_date,'yyyy-MM')
    )
    select c1.*, approved, amount from cte c1 inner join cte2 c2
    on c1.month = c2.month2

    • @codinglake
      @codinglake  10 วันที่ผ่านมา

      Nice one 👍

  • @rampavan9521
    @rampavan9521 3 วันที่ผ่านมา

    thumbnail output is different than actual output (😇)
    select
    to_char(trans_date , 'yyyy-mm'),country,
    count(state),sum(case when state = 'approved' then 1 else 0 end),sum(amount) from transactions10
    group by to_char(trans_date , 'yyyy-mm'),country

    • @codinglake
      @codinglake  7 ชั่วโมงที่ผ่านมา +1

      Due to space issues.

  • @jayaprakashreddyittech
    @jayaprakashreddyittech 2 วันที่ผ่านมา

    with cte AS (SELECT *,DATE_FORMAT(trans_date,'%y-%m') AS months
    ,case when state='approved' then 1 ELSE 0 end AS approved_tx_count,
    case when state='approved' then amount ELSE 0 end AS approved_tx_amount FROM transactions)
    SELECT months,country,COUNT(*) AS tx_count,SUM(amount) AS tx_amount,
    SUM(approved_tx_count),
    max(approved_tx_amount) FROM cte GROUP BY months,country ;

    • @codinglake
      @codinglake  7 ชั่วโมงที่ผ่านมา

      Good 👍

  • @vishnu1619
    @vishnu1619 9 วันที่ผ่านมา +1

    U made complicated😢

    • @codinglake
      @codinglake  9 วันที่ผ่านมา

      Try once then see it's easy.

    • @wonderoverload0
      @wonderoverload0 8 วันที่ผ่านมา

      This is literally the way one will write it. What's complicated in it?