SQL statement usage in Python

Source: Internet
Author: User

applying SQL statements in functions
def_get_cust_number (self,cr,uid,ids,field_name,args,context=None): Res={} forOrderinchSelf.browse (CR, UID, IDS, context=context): #获取单据信息
AA=Order.originifAA: SQL = "Select T1.cust_numr_no from Stock_fah t0 left joins Sale_orde T1 on T0.origin=t1.name where t0.origin= '%s '"% (AA) Cr.execute (SQL) Dict4=cr.dictfetchall ()ifDICT4:DD=dict4[0]['Cust_numr_no'] Res[order.id]=DDElse: Res[order.id]="' Else: Res[order.id]="' returnRes

or a

Another way of writing in a function cr.execute
Cr.execute (" "Select Ref_line_id,nolqty from (select Ref_line_id,sum (COALESCE ("Case" t0.location_id=get_warehouse_id (' into position ') then Product_qty when t0.location_id=get_warehouse_id (' original position ') then Product_qty else-1*product_qty end,0)] Dlvqty from Stock_ynum T0 left join Stock_fah t1 on T0.picking_id=t1.id whe Re t0.state= ' Done ' and ((t0.location_id=get_warehouse_id (' into position ') and t0.location_dest_id=get_warehouse_id (' Customers ' ) or (t0.location_id=get_warehouse_id (' Customers ') and t0.location_dest_id=get_warehous e_id (' into position ')) or ((t0.location_id=get_warehouse_id (' original position ') and t0.location_dest_id=get_warehouse_id (' Customers ')) or (t0.location_id=get_warehouse_id (' Customers ') and t0.location_dest_id=get_warehouse_id (' original position '))) GROUP by t0.product_id,ref_num,ref_line_id ) t where ref_line_id in%s" ", (Tuple (IDs),)) Jhqty= Dict (Cr.fetchall ())

or create views

Initialize executing functions: Create a view, with SQL statements, purple for SQL statements

defInit (SELF,CR): Tools.sql.drop_view_if_exists (CR,'Cust_cp_info_report') Cr.execute ("""Create or Replace view Cust_product_info_report as ( Select t0.id,t0.hpartner_id as hpartner_id, T0.khwl_code as khwl_code,t1.product_mp_id as PRODUCTN, T3.pname,t3.mmar,t3.spec, t4.id as Name_uom, T5.price as Custo_price,t0.product_meno as Product_meno, T0.M Eno as meno,t5.date_from,t5.date_to from Product_custo_info t0 left JOIN product_order T1 o N t0.product_tmpl_id=t1.product_tmpl_id left JOIN product_model T3 on t3.id=t1.product_tmpl_id Left join Ke_partner T2 in t2.id=t0.hpartner_id left join Product_dan T4 on T3.uom_id=t4.id Left join Partner_cp_price T5 in T5.ppp_line_id=t0.id where (now () >=t5.date_from and now () <=T5 . date_to) or (T5.date_from is null and t5.date_to are null) or (T5.date_from is null and (now () <=t5.da te_to) or (T5.date_to is null and (now () >=t5.date_from)) ) """)

SQL statement usage in Python

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.