MySQL month negative margin order Details _20161027

Source: Internet
Author: User

#当月每天负毛利订单明细

SELECTc.id,a.city,a.username,a. Order date, A. Order number, A. Sales confirmation, a. Cost amount, a. Gross profit of 1, Case  whenA. Gross Profit 1<0  Then"negative margin" whenA. Gross Profit 1=0  Then"negative margin" whenA. Gross Profit 1>0  Then"positive margin"ELSE NULL END  asidentify 1,b. Benefits, (a. Gross Profit 1-Ifnull (b. Offer,0)) asGross margin 2, Case  whenA. Gross Profit 1=(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))<0  Then"Unused coupon negative margin" whenA. Gross Profit 1=(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))=0  Then"unused coupon 0 Gross margin" whenA. Gross Profit 1=(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))>0  Then"Coupon positive margin not used" whenA. Gross Profit 1>(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))<0  Then"Use coupon negative margin" whenA. Gross Profit 1>(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))=0  Then"Use coupon negative margin" whenA. Gross Profit 1>(A. Gross profit 1-Ifnull (b. Offer,0)) and(A. Gross profit 1-Ifnull (b. Offer,0))>0  Then"Use coupon positive margin"ELSE NULL END  asidentify 2,a. Premiums, (a. Gross Profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) asGross margin 3, Case  when(A. Gross profit 1-Ifnull (b. Offer,0))=(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))<0  Then"No giveaway negative margin" when(A. Gross profit 1-Ifnull (b. Offer,0))=(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))=0  Then"No giveaway 0 gross profit" when(A. Gross profit 1-Ifnull (b. Offer,0))=(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))>0  Then"no premium positive margin" when(A. Gross profit 1-Ifnull (b. Offer,0))>(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))<0  Then"There is a giveaway negative margin" when(A. Gross profit 1-Ifnull (b. Offer,0))>(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))=0  Then"There's a giveaway 0 gross." when(A. Gross profit 1-Ifnull (b. Offer,0))>(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0)) and(A. Gross profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))>0  Then"There is a gift of positive profit"ELSE NULL END  asIdentification 3, (a. Gross Profit 1-Ifnull (b. Offer,0)-Ifnull (A. Gift amount,0))/A. Sales Confirmation amount asNet gross margin from (SELECTA1.city,a1.username,date (A1. Order date) asOrder date, A1. Order number, A1. Sales Clerk,SUMSales asSales Confirmation Amount,SUM(gross margin) asGross margin 1,SUM(Cost amount) asCost Amount,SUM(IF(Sales=0  andCost Amount>0, Cost Amount,NULL)) asGift Amount from' A005_account ' asA1WHEREA1. Order Date>=Date_add (Date_add (Last_day (Date_add (current_date, INTERVAL- 1  Day)), INTERVAL1  Day), INTERVAL-1 MONTH) andA1. Order Date<current_date GROUP  byA1. Order number) asa Left JOIN(#当月每天每个订单ID优惠额SELECTB1.city,b1.username,date (B1. Use time) asUse time, B1. Order number,SUM(coupon amount) asDiscount Amount from' A016_order_customercoupon_xref ' asB1WHEREB1. Use time>=Date_add (Date_add (Last_day (Date_add (current_date, INTERVAL- 1  Day)), INTERVAL1  Day), INTERVAL-1 MONTH) andB1. Use time<current_date GROUP  byB1. Order number) asB onA. Order number=B. Order number Left JOIN' A000_city ' asC onC.city=a.city Left JOIN' A001_rest ' asD onD.username=A.username having(A. Gross profit 1-Ifnull (A. Gift amount,0)-Ifnull (b. Offer,0))<0ORDER  byC.id,a. Order date, (a. Gross profit 1-Ifnull (A. Gift amount,0)-Ifnull (b. Offer,0))

MySQL month negative margin order Details _20161027

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.