汇总总结一下在T100中常用的SQL语句。
一、基础资料的查询
这里把aimm200中的所有的需要的数据全部串查到一个结果集中,然后直接Excel汇出即可。
select imafsite 据点,imaf001 料件编号,imaal003 品名,imaal004 规格,--imaf172 交货前置时间,imaf013 补给策略
imaa006 基础单位,imaf013 补给策略,imaa004 料件类别,imaf091 默认库位,imaf153 主要供应商,imaf145 采购单位批量,
imaf146 最小采购数量,imaf142 采购人员,imaf172 交货前置时间,imae023 必要性质,imae020 生产超交率,imae034 "默认部门/供应商",imae041 默认入库库位,imae071 固定生产前置时间,
imae091 倒扣料,imae101 默认发料库位,imae114 是否进料检验
--select imafsite 据点,imaf001 料件编号,imaal003 品名,imaal004 规格--,imaf013 补给策略
from imaa_t
left join imaf_t
on imafent = imaaent
and imaf001 = imaa001
left join imaal_t
on imafent = imaalent
and imaf001 = imaal001
left join imae_t
on imafent = imaeent
and imafsite = imaesite
and imaf001 = imae001
where imafent = 88
and imafsite = 'ALL'
and imaal002 = 'zh_CN'
and imaf013 = '1'
--and imaf001 like '5002%'
--and imaal003 like '%标签%'
--and (imaf001 like '1%' OR imaf001 like '2%' OR imaf001 like '3%' OR imaf001 like '4%')
order by imaa001
查询结果
二、基础数据的更新
一般来讲,执行更新的时候,要么做一下数据备份,防止更新错误的时候及时可以进行数据的回滚。但是我习惯才用先查询要更新的条数,然后在执行更新的语句,两次sql的结果集条数一致的时候,再去执行事务的提交操作。
一般就是先查询,后update。
--查询基础资料中倒扣料3个是否勾选
select imae091 倒扣料,imae092 是否发料前调拨,imae023 主要性质 from imae_t left join imaal_t
on imaeent = imaalent
and imae001 = imaal001
where imaeent = 88
--and imaesite = 'JHSL'
and imaal002 = 'zh_CN'
and imae001 like '5002%'
and imaal003 = 'EPDM'
更新语句一:
--芳纶线、EPDM、CSM、CR混炼胶、CIIR、CR、维纶线增强层、芳纶网眼布(NX布)、PET增强层
--imae101 默认发料库位
UPDATE imae_t
SET imae091 = 'Y',imae092 = 'Y'--,imae101 = 1110
--set imae023 = '4'
WHERE EXISTS (
SELECT 1
FROM imaal_t
WHERE imae_t.imaeent = imaal_t.imaalent
AND imae_t.imae001 = imaal_t.imaal001
AND imae_t.imaeent = 88
--AND imae_t.imaesite = 'JHG1'
AND imaal_t.imaal002 = 'zh_CN'
--and imae_t.imae001 like '5002%'
AND imaal_t.imaal003 = '铝管管材'
);
偶尔直接查询有多少条
select count(*) from imae_t where imaeent = 88
and imaesite = 'JHG1' and imae001 like '5002%'
更新语句二:
--芳纶线、EPDM、EPDM混炼胶 补给策略imaf013 = '4'
UPDATE imaf_t
SET imaf013 = '4'
WHERE imafent = 88
AND imaf001 IN (
SELECT imaal001
FROM imaal_t
WHERE imaalent = 88
AND imaal001 IN (
SELECT imaf001
FROM imaf_t
WHERE imafent = 88
--AND imafsite = 'JHNL'
AND imaal002 = 'zh_CN'
AND imaal003 = '芳纶线'
--AND imaa004 = 'A'
)
);
更新语句三:
--更新料件据点生管作业
UPDATE imae_t
SET imae041 = '2504',
imae101 = '2504'
WHERE imae001 IN (
SELECT DISTINCT imae001
FROM imae_t
LEFT JOIN imaf_t ON imae001 = imaf001 AND imaeent = imafent AND imaesite = imafsite
LEFT JOIN imaal_t ON imae001 = imaal001 AND imaeent = imaalent
WHERE imaeent = 88
AND imaesite = 'NBJK'
AND imaal002 = 'zh_CN'
and imae001 ='109900021'
)
and imaeent = 88
AND imaesite = 'NBJK'
and imae001 ='109900021'
更新语句四:
--更新料件库存管理作业
UPDATE imaf_t
SET imaf091 = '2504'
WHERE imaf001 IN (
SELECT DISTINCT imae001
FROM imae_t
LEFT JOIN imaf_t ON imae001 = imaf001 AND imaeent = imafent AND imaesite = imafsite
LEFT JOIN imaal_t ON imae001 = imaal001 AND imaeent = imaalent
WHERE imaeent = 88
AND imaesite = 'NBJK'
AND imaal002 = 'zh_CN'
--AND imaal003 LIKE '%接头%'
and imae001 ='109900021'
)
and imafent = 88
AND imafsite = 'NBJK'
and imaf001 ='109900021'
更新之后再查询一下基础数据
--查询料件三个仓库的数据是否为空值
select distinct imae001,imaal003,imae041 默认入库库位,imae101 默认发料库位,imaf001,imaf091 默认库位 from imae_t
left join imaf_t
on imae001 = imaf001
and imaeent = imafent
and imaesite = imafsite
left join imaal_t
on imae001 = imaal001
and imaeent = imaalent
where imaeent = 88
and imaesite = 'NBJK'
and imaal002 = 'zh_CN'
and imaal003 like '胶管%'
--and imae001 ='109900021'
order by imae001
三、损耗率的更新
这个数据是在bom资料里面,这个损耗率的表是需要插入进去的,一般来讲他们搭建bom的时候并没有进行损耗率的维护,所以我们需要将自己设定好的损耗率数据直接插入到该表中。
一般来讲我先查一下元件,主键号对用的条数。
select bmbbsite 据点,bmbb001 主键料号,bmbb003 元件料号 from bmbb_t where bmbbent = 88 and bmbb012 is not null and bmbb011 is not null and bmbb001 like '1004%' order by bmbb001
--bmbb_t需要插入数据 跟bmba_t绑定
select * from bmbb_t where bmbbent = 88 and bmbbsite = 'JHG1' and bmbb003 like '4001%'
select count(*) from bmba_t where bmbaent = 88 and bmbasite = 'JHG1' and bmba003 like '4001%'
这里才是真正的插入sql
-- 注塑挤出 4001开头半成品 原料是 5002 塑胶粒子 4.5 %批量更新
insert into bmbb_t (BMBBENT,BMBBSITE,BMBB001,BMBB002,BMBB003,BMBB004,BMBB005,BMBB007,BMBB008,BMBB009,BMBB010,BMBB011,BMBB012)
select bmbaent,bmbasite,bmba001,bmba002,bmba003,bmba004,bmba005,bmba007,bmba008,1,50000,10,0
from bmba_t ,imaal_t a ,imaal_t b
where bmbaent=88 and bmbasite='JHSL' --and bmba001 = '400100974'
and bmba001=a.imaal001 and a.imaal002='zh_CN' and a.imaalent=88
and bmba003=b.imaal001 and b.imaal002='zh_CN' and b.imaalent=88
--and b.imaal003 = '管坯'
and bmba003 like '5002%'
--and bmba001 like '5002%'
插入之后还需要将一个字段设置成外置损耗率,然后系统在进行生产管理的时候,会自动根据这个字段标识,进行损耗率的计算更新。
update bmba_t set bmba029='2' where bmbaent=88 and bmbasite='JHSL' and bmba003
in (
SELECT ba.bmba003
FROM bmba_t ba
JOIN imaal_t i ON i.imaal001 = ba.bmba003 AND i.imaalent = ba.bmbaent AND i.imaal002 = 'zh_CN'
WHERE ba.bmbaent = 88
--AND i.imaal003 = '管坯'
AND ba.bmbasite = 'JHSL'
and ba.bmba003 like '5002%'
)
--= '400100974' and bmba001 like '3002%' and bmba029!='2'
在补充两条update语句:
--成型 3001半成品 原料是 4001 塑胶粒子 4 %批量更新
insert into bmbb_t (BMBBENT,BMBBSITE,BMBB001,BMBB002,BMBB003,BMBB004,BMBB005,BMBB007,BMBB008,BMBB009,BMBB010,BMBB011,BMBB012)
select bmbaent,bmbasite,bmba001,bmba002,bmba003,bmba004,bmba005,bmba007,bmba008,1,50000,4,0
from bmba_t
where bmbaent=86 and bmbasite='JHNL' and bmba003 like '4001%'
and bmba001 like '3001%'
update bmba_t set bmba029='2' where bmbaent=86 and bmbasite='JHNL' and bmba003 like '4001%' and bmba001 like '3001%' and bmba029!='2'
--总成 原料是 3001 名称 尼龙管或 两通 三通 四通 固定5套批量更新
insert into bmbb_t (BMBBENT,BMBBSITE,BMBB001,BMBB002,BMBB003,BMBB004,BMBB005,BMBB007,BMBB008,BMBB009,BMBB010,BMBB011,BMBB012)
select bmbaent,bmbasite,bmba001,bmba002,bmba003,bmba004,bmba005,bmba007,bmba008,1,50000,0,5 --,imaal003
from bmba_t,imaal_t
where bmbaent=86 and bmbasite='JHNL' and bmba003 like '3001%'
and imaal002='zh_CN' and imaalent=86 and imaal001=bmba003
and (imaal003 like '%尼龙管%' or imaal003 like '%两通%' or imaal003 like '%三通%' or imaal003 like '%四通%' )
and (bmba001 like '1%' or bmba001 like '2%' )
update bmba_t set bmba029='2' where bmbaent=86 and bmbasite='JHNL' and bmba003 like '3001%'
and (bmba001 like '1%' or bmba001 like '2%' ) and bmba029!='2'
and bmba001||bmba003 in (select bmbb001||bmba003 from bmbb_t where bmbbent=86 and bmbbsite='JHNL' )
四、在捡量数据的处理
这里就涉及到库存的问题了,一般来讲我们库存不对,都在ainq100这个作业里面进行查询。里面涉及到一个料的所有的动作。
涉及到一些明细。
主要的sql语句:
--查询错误在捡量数据 更新为正确数量
select * from inan_t where inanent = 88 and inansite = 'JHNL' and inan001 = '300202907' and ASCII(inan004) = 32;
update inan_t set inan010 = 0 where inanent = 88 and inansite = 'JHNL' and inan001 = '300202907' and ASCII(inan004) = 32;
update inan_t set inan010 = 2 where inanent = 88 and inansite = 'JHNL' and inan001 = indd002 and ASCII(inan004) = 32
在库存异动的过程中,可能是T100本身的逻辑代码写的有问题,在涉及到在捡量审核/未审核的时候,还有调拨单进行拨出审核和过账的时候,这会在库存表中产生库位为【空格】的一条数据,所以后期补充逻辑,检查数量的时候,都要加一个限定条件,也就是库位为【空格】的这条数据要处理。
这里ASCII(inan004) = 32 就是这个字段为【空格】的意思
涉及sql1:
--在捡量数量明细单
select * from inap_t where inapent = 88 and inapsite ='JHNL' and inap004 = '300202907'
delete from inap_t where inapent = 88 and inapsite ='JHNL' and inap004 = '300202907'
SELECT * FROM inan_t WHERE inanent = 88 AND inansite ='JHNL' AND inan001 ='200100006' AND ASCII(inan004) ='32'
涉及sql2:
--删除拨入单的逻辑
select * from inao_t where inaoent = 88
select * from psad_t where psadent = 88 and psadsite = 'JHNL' and psaddocno = 'DAP-24020009'
五、BOM资料维护
先查询一下:
--bom中的料件仓库维护
select bmba001 主键料号,bmba003,bmba009,bmba015,bmbasite,bmba030 倒扣料, bmba013 必要性质
from bmba_t b
left join imaal_t i
on b.bmbaent = i.imaalent
and b.bmba003 = i.imaal001
where bmbaent = 88
and bmbasite = 'JHNL'
and imaal002 = 'zh_CN'
and bmba003 = '600400085'
and bmba001 = '100400063'
--and imaal003 = '铝管管材'
--and bmba030 != 'Y'
order by bmba009--600400086
再去执行更新:
--更新bom中的某些数据
UPDATE bmba_t b
SET b.bmba015 = '2402'
--set b.bmba030 = 'Y'
WHERE EXISTS (
SELECT 1
FROM imaal_t i
WHERE b.bmbaent = i.imaalent
AND b.bmba003 = i.imaal001
AND b.bmbaent = 88
AND b.bmbasite = 'JHNL'
--and b.bmba030 != 'Y'
--AND i.imaal002 = 'zh_CN'
--AND i.imaal003 = '铝管管材'
and bmba003 = '600400085'
and bmba001 = '100400063'
--and bmba003 = '600204730'
);
目前这里面更新的只有默认发料仓库,如需更新其他字段,请在此sql上面进行修改即可。
六、大数据批量更新
这里才用临时表的概念进行更新,在Oracle中创建一个临时表,然后将需要更新的数据直接copy进去,最后在按照关系执行update即可。
创建临时表
--imaf的数据库 采购单位批量:145,最小采购数量:146 ,交货前置时间172, 补给策略imaf013 【4】无
create table abcc as select imaf001,imaf172 from imaf_t where imafent=88 and 1=2
然后粘贴数据进去
select * from abcc for update
之后执行update操作
update imaf_t set --imaf013=(select imaf013 from abcc where abcc.imaf001=imaf_t.imaf001 ),
--imaf153=(select imaf153 from abcc where abcc.imaf001=imaf_t.imaf001 ),
--imaf145 = (select imaf145 from abcc where abcc.imaf001=imaf_t.imaf001 ),
--imaf146 = (select imaf146 from abcc where abcc.imaf001=imaf_t.imaf001 ),
--imaf142=(select imaf142 from abcc where abcc.imaf001=imaf_t.imaf001 ),
imaf172=(select imaf172 from abcc where abcc.imaf001=imaf_t.imaf001 )
where imaf001 = (select imaf001 from abcc where abcc.imaf001=imaf_t.imaf001 )
and imafent= 88 --and imafsite='ALL'
补充一下,我这里主键唯一一般都是料号,但是第三方发送给我要更改的数据中经常有重复的料号,这样就会导致子查询中返回多条数据,这个时候可以直接在Excel中进行数据过滤,也可以直接sql进行查询重复的数据。按需即可。
SELECT imaf001, COUNT(*)
FROM abcc
GROUP BY imaf001
HAVING COUNT(*) > 1;
更新完毕之后,把这个表直接drop即可
drop table abcc
七、处理工单作业中的数据
查询符合条件的数据出来。
查询sql1:
--单头 供应商和库位为空
select distinct sfaa010 生产料号,imaal003 品名,imaal004 规格,sfaa034 预计入库库位,sfaa017 部门供应商
from sfaa_t
left join imaal_t
on sfaa010 = imaal001
and sfaaent = imaalent
and imaal002 = 'zh_CN'
where
sfaaent = 88
and sfaasite = 'NBJK'
and sfaadocdt > DATE '2024-03-01'
and (sfaa034 is null OR sfaa017 is null)
order by sfaa010
查询sql2:
--单身 库位为空
select distinct sfba005 bom料号,imaal003 品名,imaal004 规格,sfba009 倒扣料,sfba019 指定发料仓库,sfaadocdt
from sfba_t
left join imaal_t
on imaalent = sfbaent
and imaal001 = sfba005
and imaal002 = 'zh_CN'
left join sfaa_t
on sfaadocno = sfbadocno
and sfaaent = sfbaent
where sfbaent = 88
and imaal003 = '铝管管材'
and sfba009 != 'Y'
where sfbaent = 88
and sfbasite = 'NBJK'
and sfba019 = ' '
and sfaadocdt > DATE '2024-03-01'
order by sfba005
更新sql:
UPDATE sfba_t
SET sfba009 = 'Y'
WHERE sfbaent = 88
AND sfba009 != 'Y'
AND EXISTS (
SELECT 1
FROM imaal_t
WHERE imaalent = sfbaent
AND imaal001 = sfba005
AND imaal002 = 'zh_CN'
AND imaal003 = '铝管管材'
)
八、查询不同据点有不同交货前置时间的记录
查询sql:
SELECT
imaf001 AS 料件编号,
imaal003 AS 品名,
imaal004 AS 规格,
LISTAGG(imafsite, ',') WITHIN GROUP (ORDER BY imafsite) AS 不同据点,
COUNT(DISTINCT imaf172) AS 不同交货前置时间数
FROM
imaf_t
LEFT JOIN
imaal_t ON imafent = imaalent AND imaf001 = imaal001
WHERE
imafent = 88
AND imaal002 = 'zh_CN'
GROUP BY
imaf001, imaal003, imaal004
HAVING
COUNT(DISTINCT imaf172) > 1;
之后在进行数据的处理。