[Python] SQLdevelopersfruit puzzle

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I have Fruits table with two columns Item and Count

    Item Count
    Apple 5
    Banana 3
    Apple 2
    Banana 7
    Kiwi 3

    I need output as

    Item Count
    Apple 7
    Banana 10
    Kiwi 3

    I tried using :

    select
    [Items] ,
    sum(case when [Items] = 'Apple' then 1 else 0 end) as Apple,
    sum(case when [Items] = 'Banana' then 1 else 0 end) as Banana,
    sum(case when [Items] = 'Kiwi' then 1 else 0 end) as Kiwi
    from
    dbo.Fruits
    group by
    [Items]
    order by [Items]


    But it gives me wrong output

    Items Apple Banana Kiwi
    Apple 2 0 0
    Banana 0 2 0
    Kiwi 0 0 1

    Could you please suggest thank you.

    Continue reading...

Compartilhe esta Página