Very strong SQL statements

Source: Internet
Author: User

Select row . *, RowNum rownum

From (select PartyId,

           Partyname, Partycode, sum (ordernum) as Ordernum, sum (orderamount) as OrderAmount                   From (select Pp.id as PartyId, pp.name as Partyname, Pp.code as Partycode, Count (tso.id) as Ordernum, "SUM" (case when tsoh.order_type = ten th En (TSO. Unit_price * TSO. Final_quantity-(TSO. Final_quantity/tso. ordering_quantity) * (TSO. COUPON + TSO. Discount_fee) ELSE (-tso.unit_price * TSO. Final_quantity + (TSO. Final_quantity/tso. ordering_quantity) * (TSO. COUPON + TSO. Discount_fee)) end) as OrderAmount from Tra_sales_order Tso INNER JOIN GO O_goods G on g.id = Tso. goods_id INNER JOIN Tra_sales_order_headeR Tsoh on tsoh.id = TSO. order_header_id left join Par_party pp on pp.id = tsoh.buyer_id INNER Join Wh_ware _house WWH on wwh.id = tsoh.warehouse_id WHERE tso. Order_status > Tso. Order_status < 305 and tso.final_quantity > 0 and Tsoh. warehouse_id in (select cd.domain_id from Com_domain CD where C d.source_id = 1608171622470497 and Cd.source_type = ' Department ' and Cd.domain _type = ' warehouse ') and g.brand_id in (select cd.domain_id from Com_ Domain cd where cd.source_id = 1608171622470497 and Cd.source_type = ' Department ' and Cd.domain_type = ' brand ') and g.product_id in (select Cd.domai              n_id        From Com_domain CD where cd.source_id = 1608171622470497 and cd.source_t               ype = ' Department ' and Cd.domain_type = ' product ') and Pp.party_type = 40 and wwh.id = 1403072229050000 and TSO. branch_company_id = 2 and TSO.                   Has_invoice = 0 GROUP by pp.id, Pp.name, Pp.code UNION all select Pp.id as PartyId,                   Pp.name as Partyname, Pp.code as Partycode, Count (tso.id) as Ordernum, "SUM" (case when tsoh.order_type = Ten then (TSO). Unit_price * TSO. Final_quantity-(TSO. Final_quantity/tso. ordering_quantity) * (TSO. COUPON + TSO. Discount_fee) ELSE (-tso.unit_price * TSO. Final_quantity + (TSO. Final_quantity/tso. ordering_quantity) * (TSO. COUPON + TSO.  Discount_fee)) end) as OrderAmount from Tra_sales_order_offline Tso INNER JOIN goo_goods G on g.id = Tso. goods_id INNER JOIN tra_sales_order_header tsoh on tsoh.id = TSO. order_header_id left join Par_party pp on pp.id = tsoh.buyer_id INNER Join Wh_ware _house WWH on wwh.id = tsoh.warehouse_id WHERE tso. Order_status > Tso. Order_status < 305 and tso.final_quantity > 0 and Tsoh. warehouse_id in (select cd.domain_id from Com_domain CD where C d.source_id = 1608171622470497 and Cd.source_type = ' Department ' and Cd.domain _type = ' warehouse ') and g.brand_id in (SELect cd.domain_id from Com_domain cd where cd.source_id = 1608171622470497 and Cd.source_type = ' Department ' and Cd.domain_type = ' brand ') and G. product_id in (select cd.domain_id from Com_domain CD where CD. source_id = 1608171622470497 and Cd.source_type = ' Department ' and cd.domain_t ype = ' product ') and Pp.party_type = + Wwh.id = 1403072229050000 and TSO. branch_company_id = 2 and TSO. Has_invoice = 0 GROUP by pp.id, Pp.name, Pp.code) TSO GROUP by PartyId, Partyname, Partycode order BY OrderAmount ASC) Row_

where RowNum <= 45;

--1608171622470497 (Long), 1608171622470497 (Long), 1608171622470497 (Long), (Integer), 1511161907500008 (Long), 2 ( Long), False (Boolean), 1608171622470497 (Long), 1608171622470497 (Long), 1608171622470497 (Long), (Integer), 1511161907500008 (Long), 2 (long), False (Boolean)

Very strong SQL statements

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.