TIPTOP ERP 常用SQL总结
001、根据分群码资料 更新 料号所属分群码对应资料
--將Tiptop GP系統分群碼imz_file的資料複製更新到料件基本資料ima_file中對應的欄位 update ima_file set ima_file.ima07= (select imz_file.imz07 from imz_file where imz_file.imz01 = ima_file.ima06),--ABC碼 ima_file.ima08= (select imz_file.imz08 from imz_file where imz_file.imz01 = ima_file.ima06),--來源碼 ima_file.ima09= (select imz_file.imz09 from imz_file where imz_file.imz01 = ima_file.ima06),--其它分群碼一 ima_file.ima10= (select imz_file.imz10 from imz_file where imz_file.imz01 = ima_file.ima06),--其它分群碼二 ima_file.ima11= (select imz_file.imz11 from imz_file where imz_file.imz01 = ima_file.ima06),--其它分群碼三 ima_file.ima12= (select imz_file.imz12 from imz_file where imz_file.imz01 = ima_file.ima06),--其它分群碼四 ima_file.ima14= (select imz_file.imz14 from imz_file where imz_file.imz01 = ima_file.ima06),--是否為工程料件 ima_file.ima15= (select imz_file.imz15 from imz_file where imz_file.imz01 = ima_file.ima06),--保稅與否 ima_file.ima19= (select imz_file.imz19 from imz_file where imz_file.imz01 = ima_file.ima06),--保稅料件進出口分類統計用類 ima_file.ima21= (select imz_file.imz21 from imz_file where imz_file.imz01 = ima_file.ima06),--保稅料件稅則編號 ima_file.ima23= (select imz_file.imz23 from imz_file where imz_file.imz01 = ima_file.ima06),--倉管員 ima_file.ima24= (select imz_file.imz24 from imz_file where imz_file.imz01 = ima_file.ima06),--檢驗否 ima_file.ima25= (select imz_file.imz25 from imz_file where imz_file.imz01 = ima_file.ima06),--庫存單位 ima_file.ima27= (select imz_file.imz27 from imz_file where imz_file.imz01 = ima_file.ima06),--安全庫存量 ima_file.ima28= (select imz_file.imz28 from imz_file where imz_file.imz01 = ima_file.ima06),--安全庫存期間 ima_file.ima31= (select imz_file.imz31 from imz_file where imz_file.imz01 = ima_file.ima06),--銷售單位 ima_file.ima31_fac=(select imz_file.imz31_fac from imz_file where imz_file.imz01 = ima_file.ima06),--銷售單位/庫存單位換算率 ima_file.ima34= (select imz_file.imz34 from imz_file where imz_file.imz01 = ima_file.ima06),--成本中心 ima_file.ima35= (select imz_file.imz35 from imz_file where imz_file.imz01 = ima_file.ima06),--主要倉庫別 ima_file.ima36= (select imz_file.imz36 from imz_file where imz_file.imz01 = ima_file.ima06),--主要儲位別 ima_file.ima37= (select imz_file.imz37 from imz_file where imz_file.imz01 = ima_file.ima06),--補貨策略 ima_file.ima38= (select imz_file.imz38 from imz_file where imz_file.imz01 = ima_file.ima06),--再補貨點 ima_file.ima39= (select imz_file.imz39 from imz_file where imz_file.imz01 = ima_file.ima06),--料件所屬會計科目 ima_file.ima42= (select imz_file.imz42 from imz_file where imz_file.imz01 = ima_file.ima06),--批號追蹤方式 ima_file.ima43= (select imz_file.imz43 from imz_file where imz_file.imz01 = ima_file.ima06),--採購員 ima_file.ima44= (select imz_file.imz44 from imz_file where imz_file.imz01 = ima_file.ima06),--採購單位 ima_file.ima44_fac=(select imz_file.imz44_fac from imz_file where imz_file.imz01 = ima_file.ima06),--採購單位/庫存單位換算率 ima_file.ima45= (select imz_file.imz45 from imz_file where imz_file.imz01 = ima_file.ima06),--採購單位倍量 ima_file.ima46= (select imz_file.imz46 from imz_file where imz_file.imz01 = ima_file.ima06),--最少採購量 ima_file.ima47 =(select imz_file.imz47 from imz_file where imz_file.imz01 = ima_file.ima06),--採購損耗率 ima_file.ima48= (select imz_file.imz48 from imz_file where imz_file.imz01 = ima_file.ima06),--採購安全期 ima_file.ima49= (select imz_file.imz49 from imz_file where imz_file.imz01 = ima_file.ima06),--到廠前置期 ima_file.ima491=(select imz_file.imz491 from imz_file where imz_file.imz01 = ima_file.ima06),--入庫前置期 ima_file.ima50= (select imz_file.imz50 from imz_file where imz_file.imz01 = ima_file.ima06),--請購安全期 ima_file.ima51= (select imz_file.imz51 from imz_file where imz_file.imz01 = ima_file.ima06),--經濟訂購量 ima_file.ima52= (select imz_file.imz52 from imz_file where imz_file.imz01 = ima_file.ima06),--平均訂購量 ima_file.ima54= (select imz_file.imz54 from imz_file where imz_file.imz01 = ima_file.ima06),--主要供應商 ima_file.ima55= (select imz_file.imz55 from imz_file where imz_file.imz01 = ima_file.ima06),--生產單位 ima_file.ima55_fac=(select imz_file.imz55_fac from imz_file where imz_file.imz01 = ima_file.ima06),--生產單位/庫存單位換算率 ima_file.ima56= (select imz_file.imz56 from imz_file where imz_file.imz01 = ima_file.ima06),--生產單位倍量 ima_file.ima561=(select imz_file.imz561 from imz_file where imz_file.imz01 = ima_file.ima06),--最少生產量 ima_file.ima562=(select imz_file.imz562 from imz_file where imz_file.imz01 = ima_file.ima06),--生產損耗率 ima_file.ima571=(select imz_file.imz571 from imz_file where imz_file.imz01 = ima_file.ima06),--主製程料號 ima_file.ima59= (select imz_file.imz59 from imz_file where imz_file.imz01 = ima_file.ima06),--固定前置時間 ima_file.ima60= (select imz_file.imz60 from imz_file where imz_file.imz01 = ima_file.ima06),--變動前置時間 ima_file.ima61= (select imz_file.imz61 from imz_file where imz_file.imz01 = ima_file.ima06),--QC前置時間 ima_file.ima62= (select imz_file.imz62 from imz_file where imz_file.imz01 = ima_file.ima06),--最大累計前置時間 ima_file.ima63= (select imz_file.imz63 from imz_file where imz_file.imz01 = ima_file.ima06),--發料單位 ima_file.ima63_fac=(select imz_file.imz63_fac from imz_file where imz_file.imz01 = ima_file.ima06),--發料單位/庫存單位換算率 ima_file.ima64= (select imz_file.imz64 from imz_file where imz_file.imz01 = ima_file.ima06),--發料單位倍量 ima_file.ima641=(select imz_file.imz641 from imz_file where imz_file.imz01 = ima_file.ima06),--最少發料量 ima_file.ima65= (select imz_file.imz65 from imz_file where imz_file.imz01 = ima_file.ima06),--發料安全存量 ima_file.ima66= (select imz_file.imz66 from imz_file where imz_file.imz01 = ima_file.ima06),--發料安全期 ima_file.ima67= (select imz_file.imz67 from imz_file where imz_file.imz01 = ima_file.ima06),--計畫員 ima_file.ima68= (select imz_file.imz68 from imz_file where imz_file.imz01 = ima_file.ima06),--需求時距 ima_file.ima69= (select imz_file.imz69 from imz_file where imz_file.imz01 = ima_file.ima06),--計畫時距 ima_file.ima70= (select imz_file.imz70 from imz_file where imz_file.imz01 = ima_file.ima06),--消耗料件 ima_file.ima71= (select imz_file.imz71 from imz_file where imz_file.imz01 = ima_file.ima06),--儲存有效天數 ima_file.ima86= (select imz_file.imz86 from imz_file where imz_file.imz01 = ima_file.ima06),--成本單位 ima_file.ima86_fac=(select imz_file.imz86_fac from imz_file where imz_file.imz01 = ima_file.ima06),--成本/庫存單位換算率 ima_file.ima87= (select imz_file.imz87 from imz_file where imz_file.imz01 = ima_file.ima06),--成本項目 ima_file.ima871=(select imz_file.imz871 from imz_file where imz_file.imz01 = ima_file.ima06),--材料製造費用分攤率 ima_file.ima872=(select imz_file.imz872 from imz_file where imz_file.imz01 = ima_file.ima06),--材料製造費用成本項目 ima_file.ima873=(select imz_file.imz873 from imz_file where imz_file.imz01 = ima_file.ima06),--間接人工分攤率 ima_file.ima874=(select imz_file.imz874 from imz_file where imz_file.imz01 = ima_file.ima06),--人工製造費用成本項目 ima_file.ima88= (select imz_file.imz88 from imz_file where imz_file.imz01 = ima_file.ima06),--期間採購數量 ima_file.ima89= (select imz_file.imz89 from imz_file where imz_file.imz01 = ima_file.ima06),--期間採購使用的期間 ima_file.ima90= (select imz_file.imz90 from imz_file where imz_file.imz01 = ima_file.ima06),--期間採購使用的期間 ima_file.ima94= (select imz_file.imz94 from imz_file where imz_file.imz01 = ima_file.ima06),--預設製程編號 (工單開立時預設之) ima_file.ima99= (select imz_file.imz99 from imz_file where imz_file.imz01 = ima_file.ima06),--再補貨量 ima_file.ima100=(select imz_file.imz100 from imz_file where imz_file.imz01 = ima_file.ima06),--檢驗程度 ima_file.ima101=(select imz_file.imz101 from imz_file where imz_file.imz01 = ima_file.ima06),--檢驗水準 ima_file.ima102=(select imz_file.imz102 from imz_file where imz_file.imz01 = ima_file.ima06),--級數 ima_file.ima103=(select imz_file.imz103 from imz_file where imz_file.imz01 = ima_file.ima06),--採購特性 ima_file.ima105=(select imz_file.imz105 from imz_file where imz_file.imz01 = ima_file.ima06),--是否為軟體物件 ima_file.ima106=(select imz_file.imz106 from imz_file where imz_file.imz01 = ima_file.ima06),--保稅料件型態 ima_file.ima107=(select imz_file.imz107 from imz_file where imz_file.imz01 = ima_file.ima06),--插件位置 ima_file.ima108=(select imz_file.imz108 from imz_file where imz_file.imz01 = ima_file.ima06),--工單發料前調撥否 ima_file.ima109=(select imz_file.imz109 from imz_file where imz_file.imz01 = ima_file.ima06),--材料類別 ima_file.ima110=(select imz_file.imz110 from imz_file where imz_file.imz01 = ima_file.ima06),--工單開立展開選項 ima_file.ima130=(select imz_file.imz130 from imz_file where imz_file.imz01 = ima_file.ima06),--產品銷售特性 ima_file.ima131=(select imz_file.imz131 from imz_file where imz_file.imz01 = ima_file.ima06),--產品分類編號 ima_file.ima132=(select imz_file.imz132 from imz_file where imz_file.imz01 = ima_file.ima06),--費用科目編號 ima_file.ima133=(select imz_file.imz133 from imz_file where imz_file.imz01 = ima_file.ima06),--產品預測料號 ima_file.ima134=(select imz_file.imz134 from imz_file where imz_file.imz01 = ima_file.ima06),--主要包裝方式編號 ima_file.ima147=(select imz_file.imz147 from imz_file where imz_file.imz01 = ima_file.ima06),--插件位置與QPA是否要勾稽 ima_file.ima148=(select imz_file.imz148 from imz_file where imz_file.imz01 = ima_file.ima06),--保證期(天) ima_file.ima903=(select imz_file.imz903 from imz_file where imz_file.imz01 = ima_file.ima06),--可否做聯產品入庫 ima_file.ima906=(select imz_file.imz906 from imz_file where imz_file.imz01 = ima_file.ima06),--單位使用方式1.單一單位2.母子 ima_file.ima907=(select imz_file.imz907 from imz_file where imz_file.imz01 = ima_file.ima06),--第二單位(母單位/參考單位) ima_file.ima908=(select imz_file.imz908 from imz_file where imz_file.imz01 = ima_file.ima06),--計價單位 ima_file.ima909=(select imz_file.imz909 from imz_file where imz_file.imz01 = ima_file.ima06),--MRP匯總時距(天)(預留欄位) ima_file.ima911=(select imz_file.imz911 from imz_file where imz_file.imz01 = ima_file.ima06),--是否為重覆性生產料件 (Y/N) ima_file.ima136=(select imz_file.imz136 from imz_file where imz_file.imz01 = ima_file.ima06),--主要WIP 倉庫 ima_file.ima137=(select imz_file.imz137 from imz_file where imz_file.imz01 = ima_file.ima06) --主要WIP 儲位
002、稽核非正常跨月领退料(Ex:工单2014/4月全部完工入库,但是2014/5月还有领退料动作)
SELECT * FROM --稽核工单跨月领退料 (你应该将下面的tlf06改为tlf07) (SELECT tlf62, --工单号码 MAX(CASE WHEN substr(tlf13,1,5) ='asft6' THEN tlf06 ELSE NULL END) asft620, --入库 MAX(CASE WHEN substr(tlf13,1,5) ='asfi5' THEN tlf06 ELSE NULL END) asfi5xx --领退料 FROM tlf_file WHERE EXISTS (SELECT 1 FROM sfb_file WHERE sfb01 = tlf62 AND sfb09>=sfb08) GROUP BY tlf62 ORDER BY tlf62 ) WHERE TRUNC(asft620,'month')<TRUNC(asfi5xx,'month')
003、一条SQL递归层次展BOM,各阶层
--递归层次显示料号11MP28440ZZZZ001110-004101的BOM结构 SELECT DISTINCT concat(lpad(' ',3*(level-1)),bmb01) father_ima, concat(lpad(' ',3*(level+1-1)),bmb03) level_ima FROM bmb_file,bma_file WHERE bma01=bmb01 AND bma10='2' AND bmb14='0' START WITH bmb01 = '11MP28440ZZZZ001110-004101' AND bmb01 IS NOT NULL AND bmb14='0' AND (bmb05 IS NULL OR bmb05>sysdate) AND bmb04<=sysdate CONNECT BY PRIOR bmb03 = bmb01 --表示当前料号的bmb03是下阶料号的bmb01 ORDER BY father_ima DESC
--上述11MP28440ZZZZ001110-004101BOM结构中只查尾阶原物材料 WITH cte_level_ima AS --WITH .. AS SQL子查询复用 (--递归层次显示料号11MP28440ZZZZ001110-004101的BOM结构 SELECT DISTINCT concat(lpad(' ',3*(level-1)),bmb01) father_ima, concat(lpad(' ',3*(level+1-1)),bmb03) level_ima FROM bmb_file,bma_file WHERE bma01=bmb01 AND bma10='2' AND bmb14='0' START WITH bmb01 = '06000005' AND bmb01 IS NOT NULL AND bmb14='0' AND (bmb05 IS NULL OR bmb05>sysdate) AND bmb04<=sysdate CONNECT BY PRIOR bmb03=bmb01 --表示当前料号的bmb03是下阶料号的bmb01 ORDER BY father_ima DESC ) SELECT '11MP28440ZZZZ001110-004101' AS "尾阶原物材料" FROM dual UNION SELECT level_ima AS "尾阶原物材料" FROM cte_level_ima --where ltrim(' ',cte_level_ima.level_ima) not in (select (ltrim(' ',cte_level_ima.father_ima)) from cte_level_ima) WHERE cte_level_ima.level_ima NOT IN (SELECT father_ima FROM cte_level_ima) ORDER BY 1 DESC
005、库存30-60-90-180-360天呆滞分析表
--应用:Tiptop GP ERP axcr610(LCM存货货龄分析表),一条SQL搞定30、60、90、180、360天呆滞料表 WITH cte_cma_file AS (SELECT cma01,cma02,cma03,cma04,cma14,cma15,cmc03,cmc04 FROM cma_file,cmc_file WHERE cma01 = cmc01 AND cma15 <> 0 AND cma021 = cmc021 AND cma022 = cmc022 AND cma02 = to_date('2012/05/31','YYYY/MM/DD') AND cma021 = '2012' AND cma022='5' ORDER BY cma01,cma02 ), cte_cma_count AS (SELECT cma01, SUM(cmc04*(CASE WHEN ((cma02-cmc03)>=0 AND (cma02-cmc03)<=30) THEN 1 ELSE 0 END)) cmc04_t30, SUM(cmc04*(CASE WHEN ((cma02-cmc03)>=31 AND (cma02-cmc03)<=90) THEN 1 ELSE 0 END)) cmc04_t90, SUM(cmc04*(CASE WHEN ((cma02-cmc03)>=91 AND (cma02-cmc03)<=180) THEN 1 ELSE 0 END)) cmc04_t180, SUM(cmc04*(CASE WHEN ((cma02-cmc03)>=181 AND (cma02-cmc03)<=365) THEN 1 ELSE 0 END)) cmc04_t365, SUM(cmc04*(CASE WHEN ((cma02-cmc03)>=366) THEN 1 ELSE 0 END)) cmc04_t365UP FROM cte_cma_file GROUP BY cma01 ORDER BY cma01 ), cte_cma_price AS (SELECT DISTINCT cma01,cma14,cma15 FROM cte_cma_file ORDER BY cma01 ) SELECT cte_cma_count.cma01 "料号",cma14 "月平均加权单价",cma15 "总数量",cma14*cma15 "总金额", cmc04_t30 "30天呆滞总数量", cmc04_t30*cma14 "30天呆滞总金额", cmc04_t90 "90天呆滞总数量", cmc04_t90*cma14 "90天呆滞总金额", cmc04_t180 "180天呆滞总数量", cmc04_t180*cma14 "180天呆滞总金额", cmc04_t365 "365天呆滞总数量", cmc04_t365*cma14 "365天呆滞总金额", cmc04_t365UP "365UP天呆滞总数量",cmc04_t365UP*cma14 "365UP天呆滞总金额" FROM cte_cma_count,cte_cma_price WHERE cte_cma_count.cma01 = cte_cma_price.cma01 ORDER BY cte_cma_count.cma01
http://blog.csdn.net/yihuiworld