mysql - How GROUP BY and COUNT or SUM works together in SQL queries -


i wonder how group by , count or sum works in sql queries.

for example, why following code use sum function on rows within each group, not groups itself?

select      product_name,     sum(substring_index(company_name, '|', -1) '%live%' count_live,     sum(substring_index(company_name, '|', -1) '%demo%' count_demo,  sum(substring_index(company_name, '|', -1) not '%live%' , substring_index(company_name, '|', -1) not '%demo%') count_other,  count(*) total foo group product_name order total desc ; 

how executed? part of expression executed earlier?

aggregate function sum or count return result based on group of rows (aggregated column specified in group clause) in case obtain both: sum of value contained in rows same product_name , count of these rows.

the group condition lead aggregation , work aggregate funtion in same way ..


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -