-2

I have a table named sales_table like below

Date type amount
2021-12-01 Cash 100
2021-12-01 Google Pay 200
2021-12-01 Google Pay 300
2021-12-02 Cash 100
2021-12-02 Google Pay 200
2021-12-02 Google Pay 200

I want need some query in SQL so that I can get data in below format.

Date Cash Google Pay
2021-12-01 100 500
2021-12-02 100 400

Can anyone please help with this?

LukStorms
  • 28,916
  • 5
  • 31
  • 45

1 Answers1

1

Maybe this

select sale.Date
, sum(case when sale.type = 'Cash' 
           then sale.amount end) as Cash
, sum(case when sale.type = 'Google Pay' 
           then sale.amount end) as "Google Pay" 
from sales_table as sale
where sale.type in ('Cash', 'Google Pay')
group by sale.Date
order by sale.Date 

Or this (Sql Server)

select *
from (
  select "Date", "type", amount 
  from sales_table
) src
pivot (
  sum(amount) 
  for "type" in ('Cash', 'Google Pay')
) pvt
LukStorms
  • 28,916
  • 5
  • 31
  • 45
  • 1
    As if there weren't dozens of duplicates for pivot tables for both mysql and ms sql server... – Shadow Dec 03 '21 at 09:03