Viki Cullen Viki Cullen - 4 months ago 8
SQL Question

Show as empty for Duplicates in Particular column in MYSQL

I am having a table called Bills in MySQL.

Bill item totalprice

BILL_1 Fossil Watch 9000
BILL_1 Fastrack 9000
BILL_1 Fastrack 9000
BILL_2 Woodlands 7000
BILL_2 Woodlands 7000
BILL_3 Denim Shirt 9000
BILL_3 Levis Jean 9000


The SELECT statement should return like below

Bill item totalprice

BILL_1 Fossil Watch 9000
BILL_1 Fastrack -
BILL_1 Fastrack -
BILL_2 Woodlands 7000
BILL_2 Woodlands -
BILL_3 Denim Shirt 9000
BILL_3 Levis Jean -
The totalprice column is total bill amount.

JPG JPG
Answer

Not sure if this is what you exactly want or not, but try it:

select t.Bill, t.item, t.totalPrice
from (
    select 
        b.Bill, b.item
        ,if(@price <> b.totalPrice or @price is null, b.totalPrice, '-') as totalPrice
        ,@price := b.totalPrice as dummy
    from (select * from Bills order by totalPrice desc, item desc) b
    cross join (select @price := null) t
) t

Demo Here

Edited:

select t.Bill, t.item, t.totalPrice
from (
    select 
        b.Bill, b.item
        ,if(@price <> b.totalPrice or @price is null, b.totalPrice, '-') as totalPrice
        ,@price := b.totalPrice as dummy
    from (select * from Bills order by Bill, totalPrice desc) b
    cross join (select @price := null) t
) t

New Demo