SQL Question in AMAZON Interview🔥Practice it Before Your Interviews😎 DE, Data Scientist, Analyst❤️

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

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

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

    Try this question by your own first, post the solution in comments ... then match it with my solution ✌️
    And if you are enjoying these kind of videos and it looks helpful to you, then complete 1000 likes ❤️❤️

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

      Sir please make video on Pivot function in Oracle...

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

    create table SALE (merchant STRING, amount int, pay_mode STRING);
    insert into SALE values('merchant_1',150,'cash');
    insert into SALE values('merchant_1',500,'online');
    insert into SALE values('merchant_2',450,'online');
    insert into SALE values('merchant_1',100,'cash');
    insert into SALE values('merchant_3',600,'cash');
    insert into SALE values('merchant_5',200,'online');
    insert into SALE values('merchant_2',100,'online');

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

    Hey Shashank,
    Just a piece of advice, it would be great if you also include the CREATE table statements in the description box of every interview question video. It would be helpful for us to practice.

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

      Don't be lazy you can write your own create statement data are available in video 😎

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

    Please add more SQL Hackerrank rank or leetcode problems (Solution + Approach + Common mistakes )asked in companies . THIS IS very helpful 🙌

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

    Amazing video, more videos like this ❤️

  • @mrwho.7163
    @mrwho.7163 2 ปีที่แล้ว +10

    GREAT VIDEO MAN !!! can u do a fang joining road map for 2 yr experienced software engineer ? ps : tier 3 collage , tier 2 collage

  • @mahesh.khatai93
    @mahesh.khatai93 2 ปีที่แล้ว +3

    Hey Shashank ,
    Thanks for the video . As you suggested for trying once before the solution . I tried to solve the problem statement using CTE approach .
    1. 2 CTE - Online_data & Cash_data which adds the amount .
    2. Full Outer Join CTE's on merchant_name and do NVL handling .
    WITH online_data AS
    (
    SELECT merchant,payment_mode,SUM(amount) as online_total_amount from payment_data
    WHERE payment_mode = 'Online'
    GROUP BY merchant,payment_mode
    ),
    cash_data AS
    (
    SELECT merchant,payment_mode,SUM(amount) as cash_total_amount from payment_data
    WHERE payment_mode = 'Cash'
    GROUP BY merchant,payment_mode
    )
    SELECT coalesce(a.merchant,b.merchant) as merchant,
    NVL(CAST(a.online_total_amount AS STRING),'0') as online_amount,
    NVL(CAST(b.cash_total_amount AS STRING),'0') as cash_amount
    FROM online_data a FULL OUTER JOIN cash_data b
    ON a.merchant = b.merchant
    ORDER by merchant

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

    amazing. thanks so much for bringing such problem!
    please make more of such content

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

    whenever i watch ur videos i thought that here is hopes to get better path🤩...ur all podcasts and interviews r fruitful for me.. Hat's Off 🙌❤️🙌

  • @Priyanka-us8rw
    @Priyanka-us8rw 2 ปีที่แล้ว +2

    It would be great if you create playlist for such sql interview questions for different companies.

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

    Really nice video. We need a playlist of all such advanced SQL questions.

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

    Thank you sir 🙏 for this video and excited for this kind of videos in future

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

    These type of good SQL questions r not easily available, well explained, you are doing an awesome work 👏👏🙂

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

    Earlier I used the pivot concept in SQL to solve this but your solution was so crisp and efficient.

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

    Great work brother....please continue the same....like from my side

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

    I made the mistake by summing at individual level but you catch it and explain why we need overall case statement in sum

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

    Hey this way of learning SQL QUERIES is Super cool. Do bring us such content very often to us ✨.
    Keep up the good work, cheers

  • @SANJAYYADAV-hm2bs
    @SANJAYYADAV-hm2bs 11 หลายเดือนก่อน

    good question.
    please make a separate playlist with all important questions for product companies.

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

    Nice explanation bro, keep on posting new videos 📹 👍 👌

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

    Thank you very much for this video and the lesson. It definitely caught me off guard when I jumped into conclusion as soon as I finished reading the questions and written the 1st statement with the groupby like you have mentioned, then quickly realized from the output column indeed I haven't been paying enough attention.

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

    Hahaha, I work in a product based startup and whenever i am making dashs using sql, I am often asked for exact same view for data visualisation. And it is nice to see that you are focusing on the optimised way of writing the query.

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

    Never imagined this type of solutions is possible

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

    This is my solution with Pivot
    select merchant,isnull(cash,0) as Cash_Amount ,isnull(online,0) as Online_Amount
    from (select merchant,amount,payment_mode from PAYMENTS_DATA)s
    pivot(sum(amount) for payment_mode in ([cash],[online])) table2

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

    I made the mistake of using the aggregate SUM function inside the WHEN clause.

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

    Amazing man

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

    Thanks for great content.
    Happy Teacher's Day.

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

    I got a question with a similar approach in my previous SQL interview. I wish I had seen this video before the interview!

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

    Nice explanation and well summarized
    Thank you brother

  • @AravindKumar-vq1yh
    @AravindKumar-vq1yh 2 ปีที่แล้ว +1

    Hey Shashank I am an upcoming data engineer placed in a startup ,just a fresher and your videos are too good man ,they are motivating me a Lott 💗 I hope to meet you some time in the future and let's discuss more about data engineering 🔥

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

    select merchant,
    isnull(Sum(case when payment_mode = 'Cash' then Amount else null end ),0)as cash_amount,
    isnull(sum(case when payment_mode = 'Online' then Amount else null end ),0)as online_amount
    from payments_data
    group by merchant

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

    Very informative bro

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

    Really a good one . I paused the video n thought of a sub query on top of the base data/query which you showed in the first iteration , but liked your approach too , you made it w/o even using a sub query.
    I would suggest you to create a separate play list for the same which should have atleast one from each category like this one, some different window functions and so on ... covering the breadth (different types) and then add complexity on each type slowly 👍

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

      There could be many approaches. You'll need to show the most optimal one at interviews.
      If you can avoid joins then the query will perform better.
      The more you can do with less is usually the best approach in SQL.
      Keeping in mind readability.

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

    using postgresql:
    select merchant,
    sum(case
    when payment_mode = 'cash' then amount else 0
    end) as cash_amount,
    sum(case
    when payment_mode = 'online' then amount else 0
    end) as online_amount
    from payments_data
    group by merchant
    order by merchant
    😁😁

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

    Nice one for brain exercise.

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

    with cte as (
    select mname,pmode,sum(amount) as amt from march group by mname,pmode order by mname )
    select mname,max(case when pmode = 'cash' then amt else 0 end )as cash,
    max(case when pmode = 'online' then amt else 0 end ) as "online"
    from cte group by mname;

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

    Hello Sir, can this problem be solved with window function? i tried it with window function but not getting output in correct format?
    my approach - select merchant,sum(amount) over(partition by merchant,payment_mode),payment_mode from leetcode.payments_data order by merchant;

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

      You have to do the group by in order to produce 1 record for each merchant

  • @ShivamSharma-mn1wm
    @ShivamSharma-mn1wm 2 ปีที่แล้ว

    Easy Peasy bro :), thnx for this video it helps boosting our confidence.

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

    2 days back, I used pivot and I write very big code.
    With this method, it will be a short code

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

    Select merchant , cash_mode, online_mode , case cash_mode>=0 or online_mode>=0 then cash_mode+online_mode else 0 as total from table group by merchant

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

    Without case you can do it with a mix of with clause and union all to achieve it.

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

    Nice Sir, thanks

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

    I did this question in MS SQL Server using 2 Approaches.
    Approach 1 Using Case Statement :-
    SELECT merchant, SUM(CASE WHEN payment_mode = 'CASH' THEN amount ELSE 0 END) as cash_amount,
    SUM(CASE WHEN payment_mode = 'ONLINE' THEN amount ELSE 0 END) as online_amount
    FROM payments_data
    GROUP BY merchant
    Approach 2 Using Pivot Function :-
    SELECT merchant, SUM(CASE WHEN CASH is NULL THEN 0 ELSE CASH END) as cash_amount, SUM(CASE WHEN ONLINE is NULL THEN 0 ELSE ONLINE END) as online_amount
    FROM payments_data
    PIVOT(
    MAX(amount)
    FOR payment_mode in
    (CASH,ONLINE)
    ) pivotdata
    GROUP BY merchant

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

    For beginners in SQL , you can try with outer join queries as building blocks and then move to better way of writing.
    SELECT DISTINCT
    C.MERCHANT,
    COALESCE(A.TOTAL,0) AS CASH_TOTAL ,
    COALESCE(B.TOTAL,0) AS ONLINE_TOTAL
    FROM
    C16 C LEFT OUTER JOIN
    (SELECT merchant,SUM(AMOUNT) TOTAL FROM C16 WHERE P_MODE = 'CASH' GROUP BY merchant) A
    ON C.MERCHANT = A. MERCHANT
    LEFT OUTER JOIN
    (SELECT merchant,SUM(AMOUNT) TOTAL FROM C16 WHERE P_MODE = 'ONLINE' GROUP BY merchant) B
    ON C.MERCHANT = B. MERCHANT

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

    I have read about case when but i have never thought it would be such a handy operation😳. I would like to say buddy love the way you explained♥️. Also I have a doubt how, where, and which course to opt to practise these types of sql queries so that🙏 I can prepare it

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

    If I use pivot instead of case when statement. Then which query will be best in the term of performance? And why?

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

    Very helpful

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

    with cte1 as
    (select merchant,payment,sum(amount) as total_amount from amazon group by merchant,payment)
    select merchant,max(case when payment='cash' then total_amount end) offline,max(case when payment='online' then total_amount end) online from cte1 group by merchant

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

    with x as (select payment.merchant,case when payment_mode ='cash' then amount else 0 end as cash_amount
    ,case when payment_mode ='online'
    then amount else 0 end as online_amount
    from payment)
    select distinct merchant,sum(cash_amount)over(partition by merchant) as cash_amount,sum(online_amount)
    over(partition by merchant) as online_amount from x;

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

    Heyy I am looking for a video on Namma Yatri challenge by Justpay on Unstop. I really want to participate! Can you please make a video on that?

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

    how to approach when there are multiple payment modes ?

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

    select merchant, cash_amount, online_amount from payments_data pivot(sum(amount) for (payment_mode) in ('Cash' as Cash_amount, 'Online' as Online_amount)) as pivottable;
    is this correct,,, I'm new to pivot function so correct me if I am wrong

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

    Is their any platform for practicing SQL questions like DSA

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

    Thank you bro

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

    select merchant, sum(case when payment_mode = 'cash' then amount else 0 end)as cash_amount
    ,sum(case when payment_mode = 'online' then amount else 0 end)as online_amount from payments_data
    group by merchant

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

    I think my solution is lengthy. But still I gave a try.
    with cte as
    (
    select
    a.merchant ,
    b.payment_mode
    from
    (select distinct merchant from payments_data) as a ,
    (select distinct payment_mode from payments_data) as b
    )
    select merchant , [cash] as cash_amount , [online] as online_amount from
    (
    select a.merchant ,
    a.payment_mode ,
    coalesce(b.amount, 0) as amount
    from
    cte as a
    left join
    payments_data as b
    on a.merchant = b.merchant and a.payment_mode = b.payment_mode
    ) as a
    pivot
    (
    sum(amount)
    for payment_mode in ([cash] , [online])
    ) as pivot_table;

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

    I usually use decode in such scenario instead of case

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

    Can you explain how to unpivot result?

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

    My first thought was to use SELF JOIN, lol. For some unknown reason, CASE WHEN isn't something that comes to mind intuitively while using SQL. I guess we can solve it using self join but it would probably be very inefficient.
    I probably need to step up my SQL game, specially now that I'm working on ML at FAANG.

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

      Which one and what experience you join can you share the process?

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

    You use (end ) in sql quey what is it meaning..?

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

    Hi, can you share code to create table?

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

      @shashank : same request from my side as well. If you provide the script n the data (insert statements with the same data you have in the video), people can first make the table ready n then practice /exp in their local

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

      ==================
      TABLE CREATION
      ==================
      CREATE TABLE amazon (
      merchant VARCHAR (50),
      amount INT,
      payment_mode VARCHAR(20)
      );
      INSERT INTO amazon (merchant,amount, payment_mode)
      VALUES("mer_1",150,"CASH"),
      ("mer_1",500,"ONLINE"),
      ("mer_2",450,"ONLINE"),
      ("mer_1",100,"CASH"),
      ("mer_3",600,"CASH"),
      ("mer_5",200,"ONLINE"),
      ("mer_2",100,"ONLINE");

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

    select merchant,sum(case when payment_mode = 'cash' then amount else 0 end) cash_amt,
    sum(case when payment_mode = 'online' then amount else 0 end) online_amt -- sum(amount)
    from merchant
    group by merchant

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

    Please make a video on windows function

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

    if it is a leetcode problem can you please tell me the problem number.

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

    my solutions would be:
    select merchant,
    sum (case when payment_mode = 'Cash' then amount else 0 end ) as Cash_payment,
    sum (case when payment_mode = 'Online' then amount else 0 end ) as Online_payment
    from transactions
    Group by merchant

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

    Nice question.
    Here is my solution.
    select merchant,
    sum(
    CASE WHEN payment_mode="CASH" THEN amount
    ELSE 0
    END) as "Cash_amount",
    sum(
    CASE WHEN payment_mode="ONLINE" THEN amount
    ELSE 0
    END) as "online_amount"
    from payments_date group by merchant;

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

    An alternate solution:
    with online_table as (select Merchant, sum(Amount) as Online_Amount from mechant where Payment_mode = 'Online'
    group by Merchant)
    ,cash_table as (select Merchant, sum(Amount) as Cash_Amount from mechant
    where Payment_mode = 'Cash'
    group by Merchant)
    select ot.Merchant,ot.Online_Amount,ct.Cash_Amount from online_table ot left join cash_table ct on ot.Merchant = ct.Merchant;

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

    how to get this questions on our sql server?

  • @SwetankSingh-vq2tz
    @SwetankSingh-vq2tz 2 ปีที่แล้ว

    select Merchant,Coalesce(sum(case when PaymentMode = 'Online' then Amount end),0) as "Online",coalesce(sum(case when PaymentMode = 'Cash' then Amount end),0) as "Cash"
    from Merchant
    group by merchant;

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

    On which platform we can practice such type of questions ?? 🙄🙄

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

    with cte as(
    select merchant,SUM(case when pay_mode='cash' then amount else 0 end)cash,SUM(case when pay_mode='online' then amount else 0 end)online from SALE group by merchant,pay_mode)
    select merchant,MAX(cash)cash_max,MAX(online)online_max from cte group by merchant

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

    Sir how can I speak english like u sir please replay me sir .

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

    Please share data set

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

    Please do it by pivoting

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

    Great content sir..! One typo in Bio Practice*

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

      Thanks for pointing that out, corrected it 😊

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

    From where I can learn SQL

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

    Select merchant,
    SUM(Case
    When payment_mode = ‘CASH’ then amount
    Else 0
    END) As Cash_amount,
    SUM(Case
    When payment_mode = ‘ONLINE’ then amount
    Else 0
    END) As online_amount,
    From payments_data
    Group by merchant;

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

    Is there any other way we can achieve the solution using the cursor?

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

      You can try it out if it works .. share the solution here

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

      Why use complex concepts when you use a simple case when. Also, we have to resort to case when because theres no sumif in ansi sql. If for example youre on big query you can simply use sumif(amount, mode =cash) with a group by.

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

    My solution :
    select marchant,
    sum(case when payment_mode='CASH' then amount else 0 end) Cash_amount,
    sum(case when payment_mode='ONLINE' then amount else 0 end) Online_amount
    from payments_data
    group by marchant
    order by marchant

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

    SELECT
    merchant,
    sum(CASE WHEN payment_mode = 'CASH' THEN amount end) AS cash_amount,
    sum(CASE WHEN payment_mode = 'ONLINE' THEN amount end)AS online_amount
    FROM payments_data
    --WHERE
    GROUP BY merchant

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

    Select merchant,
    Case
    When payment_mode = 'Online' Then Sum(amount) Else 0 End as Online_amount,
    When payment_mode = 'Cash' Then Sum(amount) Else 0 End as Cash_amount
    From payments
    Group By merchant, payment_mode

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

    select merchant,sum(cash_amount) as cash_amount,sum(online_amount) as online_amount from(
    select merchant,
    CASE WHEN (payment_mode = "CASH") THEN SUM(amount)
    ELSE 0 END as cash_amount,
    CASE WHEN (payment_mode = 'ONLINE') THEN SUM(amount)
    ELSE 0 END as online_amount
    from payments_data group by merchant,payment_mode) pd group by pd.merchant;

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

    We can achieve this by using decode

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

    This was not very hard! I suggest maybe you come up with some pretty complex queries for you viewers as a competetion for them, and then maybe you can solve that a week later live on video!!

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

    Please like karo video ko

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

    DECODE (payment_mode, 'CASH', amount,0) as cash_amount, DECODE (payment_mode, 'ONLINE', amount,0) as online_amount in Oracle

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

    with i_table as (
    select merchant, sum(amount) as total_amount, payment_mode
    from sales
    group by merchant, payment_mode
    )
    select
    merchant, sum(cash_amount) as cash_amount, sum(online_amount) as online_amount
    FROM (
    select
    merchant,
    CASE WHEN payment_mode = "CASH" THEN total_amount ELSE 0 END as cash_amount,
    CASE WHEN payment_mode = "ONLINE" THEN total_amount ELSE 0 END as online_amount
    From i_table
    ) x
    group by x.merchant

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

    select merchant,isnull(sum([CASH]),0),isnull(sum([ONLINE]),0) from tr1
    pivot(sum(amount) for payment_mode in ([CASH],[ONLINE])) as pvt
    group by merchant

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

    -------------------------------------------------------------
    Postgres Solution with CTE - Bad way
    ---------------------------------------------------------------
    with cte_sum as (select merchant, sum(amount) as SS, payment_mode,
    (case when payment_mode='CASH' then 1 else 0 end) as PM
    from amazon group by merchant, payment_mode
    order by merchant)
    select merchant,
    MAX(case when pm=1 then SS else 0 end) as CASH,
    MAX(case when pm=0 then SS else 0 end) as ONLINE
    from cte_sum
    group by merchant
    ----------------------------------------------------
    Postgres Solution - smart way of doing
    -----------------------------------------------------
    select merchant,
    SUM(case when payment_mode='CASH' then amount else 0 end) as CASH,
    SUM(case when payment_mode='ONLINE' then amount else 0 end) as ONLINE
    from amazon
    group by merchant

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

    PIVOT
    spreding column -> Payment_mode
    aggregating col -> sum(amount)
    group by column -> Merchants
    select
    Merchant
    ifnull(CASH,0) as cash_amount,
    ifnull(ONLINE,0) online_amount
    from
    (select Payment_mode, amount, Merchant from pay_data)DQ
    PIVOT
    ( sum(amount) for Payment_mode IN ([CASH), [ONLINE]) )PQ