sql - MySQL functions COUNT/MIN/MAX issue -
i run following queries sum same answer
sql 1 ::
select l_partkey,sum(l_linenumber) lineitem inner join orders on orders.o_orderkey = lineitem.l_orderkey o_weekday='tuesday' group l_partkey
sql 2 ::
select sum(if(orders.o_weekday='tuesday',l_linenumber, 0)) 'tuesday' lineitem inner join orders on orders.o_orderkey = lineitem.l_orderkey group lineitem.l_partkey
but when run above queries avg/count/min/max function instead of sum both queries gives different output.
say have following data:
monday 1 tuesday 2 tuesday 3
your first query looks @ last 2 rows. sum 5, average 2.5, number of lines 2.
your second query looks @ rows. first row, replaces number 1 0. sum still 5, average 5/3, , number of lines 3.
for sum, rows value of 0 not matter. rows 0 influence other aggregates count, min, max , avg.
Comments
Post a Comment