Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgungPambudi/f7b76433c6e271fc18da4bc42f1eabff to your computer and use it in GitHub Desktop.
Save AgungPambudi/f7b76433c6e271fc18da4bc42f1eabff to your computer and use it in GitHub Desktop.
MySQL SELECT query to view certain condition using SUM IF DISTINCT CONCAT
# title : mysql_select_query_to_view_certain_condition_using_sum_if_distinct_concat.sql
# description : select query to view certain condition
# author : Agung Pambudi
# website : http://agungpambudi.com
# email : mail@agungpambudi.com
# requirement : MySQL / Maria DB
# _ _ _
# ___ ___ _ _ ___ ___ ___ ___ _____| |_ _ _ _| |_| ___ ___ _____
# | .'| . | | | | . | . | .'| | . | | | . | |_| _| . | |
# |__,|_ |___|_|_|_ | _|__,|_|_|_|___|___|___|_|_|___|___|_|_|_|
# |___| |___|_|
#
select branch_code,
sum(if(type='S',qty,qty*-1)) as qty,
sum(if(type='S',(gross-ppn),(gross-ppn)*-1)) as net,
sum(if(type='S',gross,gross*-1)) as gross,
sum(if(type='S',qty*hpp,qty*hpp*-1)) as hpp,
count(distinct(concat(branch_code,trxdate,receiptnumber,station,shift))) as bill,
(sum(if(type='S',(gross-ppn),(gross-ppn)*-1))-sum(if(type='S',qty*hpp,qty*hpp*-1))) as margin,
(sum(if(type='S',(gross-ppn),(gross-ppn)*-1))-sum(if(type='S',qty*hpp,qty*hpp*-1)))/sum(if(type='S',qty*hpp,qty*hpp*-1))*100 as margin_prs
from FS092019 where htype='I' and product_code not in (select prd_code from prd_code_except) group by branch_code;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment