Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql - group by parts of a string in sql

Tags:

sql

mysql

I'm using sql to pull together a table like this:

person         flavour                                purchase
andy         likes-orange_hates-vanilla                  $5
george       likes-orange_hates-icecream                 $5
gena         likes-vanilla_hates-bourban                 $2
gena         likes-vanilla_hates-bourban                 $4

Is it possible to go from that to:

                total
likes-orange     $10
likes-vanilla    $6

even better would be

              total      av. purchase size     av. purchases per person    total people
likes-orange    $10                $5                   1                        2
likes-vanilla   $6                 $3                   2                        1

I just have access to a phpmyadmin.

like image 629
ntc Avatar asked Dec 17 '22 03:12

ntc


1 Answers

Try

select substring_index(flavor,'_',1), sum(purchase) from table group by 1
like image 152
Jim Garrison Avatar answered Jan 03 '23 22:01

Jim Garrison