SQL 查询即时库存(带查询仓位名称)原创
31人赞赏了该文章
1,129次浏览
编辑于2024年04月24日 13:51:04
SQL 语句查询即时库存,带有查询仓位名称,如果不需要查询仓位名称,可以把With 语句部分去掉:
WITH TEMPA AS ( SELECT DISTINCT a.FSTOCKID , a.FSTOCKLOCID , 'FF' + CAST (B.FFLEXID AS VARCHAR) FFLEXID , CASE WHEN B.FFLEXID = 100001 THEN c.FF100001 --需要根据实际的维度改下,T_BAS_FLEXVALUESDETAIL中FF1000的字段都要填上 WHEN B.FFLEXID = 100002 THEN c.FF100002 ELSE 0 END FFLEXVALUE FROM T_STK_INVENTORY a --T_STK_INVENTORY代表数据源,可替换成仓位所在表,如单据明细、即时库存等 INNER JOIN T_BD_STOCKFLEXITEM B ON a.FSTOCKID = B.FSTOCKID LEFT JOIN T_BAS_FLEXVALUESDETAIL c ON c.FID = a.FSTOCKLOCID ), TEMPB AS ( SELECT T2.FSTOCKLOCID , FSTOCKLOC = ( STUFF(( SELECT ',' + T1.FNAME FROM ( SELECT DISTINCT A.FSTOCKLOCID , B.FNAME FROM TEMPA A INNER JOIN T_BAS_FLEXVALUESENTRY_L B ON A.FFLEXVALUE = B.FENTRYID AND B.FLOCALEID = 2052 ) T1 WHERE T1.FSTOCKLOCID = T2.FSTOCKLOCID FOR XML PATH('') ), 1, 1, '') ) FROM ( SELECT DISTINCT A.FSTOCKLOCID , B.FNAME FROM TEMPA A INNER JOIN T_BAS_FLEXVALUESENTRY_L B ON A.FFLEXVALUE = B.FENTRYID AND B.FLOCALEID = 2052 ) T2 GROUP BY T2.FSTOCKLOCID ) SELECT a.FID , a.FSTOCKORGID ,--库存组织 a.FKEEPERTYPEID , --保管者类型 a.FKEEPERID ,--保管者 a.FOWNERTYPEID ,--货主类型 a.FOWNERID ,--货主 a.FSTOCKID ,--仓库 a.FSTOCKLOCID ,--仓位 ISNULL(loc.FSTOCKLOC,'') FSTOCKLOC,--仓位 a.FAUXPROPID ,--辅助属性 a.FSTOCKSTATUSID ,--库存状态 a.FLOT ,--批号 a.FBOMID ,--BOM版本 a.FMTONO ,--计划跟踪号 a.FPROJECTNO ,--项目编号 (CASE WHEN d.FISEXPPARTOFLOT='1' THEN e.FPRODUCEDATE ELSE a.FPRODUCEDATE END) FPRODUCEDATE ,--生产日期 (CASE WHEN d.FISEXPPARTOFLOT='1' THEN e.FEXPIRYDATE ELSE a.FEXPIRYDATE END) FEXPIRYDATE ,--有效期至 a.FBASEUNITID ,--基本单位 a.FBASEQTY ,--库存基本单位数量 a.FSECQTY ,--辅单位数量 a.FSTOCKUNITID ,--库存单位 c.FMATERIALID ,--物料內码 (CASE WHEN d.FSTOREURNUM=0 THEN a.FBASEQTY ELSE a.FBASEQTY*d.FSTOREURNOM/d.FSTOREURNUM END) FQTY ,--库存主单位数量 (CASE WHEN d.FSTOREURNUM=0 THEN ISNULL(b.FBASELOCKQTY, 0) ELSE ISNULL(b.FBASELOCKQTY, 0)*d.FSTOREURNOM/d.FSTOREURNUM END) FLOCKQTY ,--锁库数量 a.FSECUNITID ,--辅单位 (CASE WHEN d.FSTOREURNUM=0 THEN (a.FBASEQTY - ISNULL(b.FBASELOCKQTY, 0)) ELSE (a.FBASEQTY - ISNULL(b.FBASELOCKQTY, 0))*d.FSTOREURNOM/d.FSTOREURNUM END) FAVBQTY ,--可用量 a.FUPDATETIME ,--最近更新日期 a.FISEFFECTIVED,--是否有效库存 ISNULL(b.FBASELOCKQTY, 0) FBASELOCKQTY ,--锁库数量(基本单位) ISNULL(b.FSECLOCKQTY, 0) FSECLOCKQTY ,--锁库数量(辅单位) ( a.FBASEQTY - ISNULL(b.FBASELOCKQTY, 0) ) FBASEAVBQTY ,--可用量(基本单位) ( a.FSECQTY - ISNULL(b.FSECLOCKQTY, 0) ) FSECAVBQTY--可用量(辅单位) FROM T_STK_INVENTORY a --即时库存表 INNER JOIN T_BD_MATERIAL c ON c.FMASTERID=a.FMATERIALID and c.FUSEORGID=a.FSTOCKORGID --物料表 INNER JOIN T_BD_MATERIALSTOCK d ON d.FMATERIALID=c.FMATERIALID INNER JOIN T_BD_STOCK stock on stock.FSTOCKID=a.FSTOCKID --仓库表 INNER JOIN T_ORG_ORGANIZATIONS org on org.FORGID=a.FSTOCKORGID --库存组织 LEFT JOIN T_BD_LOTMASTER e on e.FLOTID=a.FLOT --批号 LEFT JOIN TEMPB loc ON loc.FSTOCKLOCID = a.FSTOCKLOCID --关联查询仓位名称 LEFT JOIN ( SELECT TKE.FSUPPLYINTERID ,--锁库数量相关,不需要考虑可用量可以不要这个join SUM(ISNULL(TKE.FBASEQTY, 0)) AS FBASELOCKQTY , SUM(ISNULL(TKE.FSECQTY, 0)) AS FSECLOCKQTY FROM T_PLN_RESERVELINKENTRY TKE WHERE TKE.FSUPPLYFORMID = 'STK_Inventory' AND TKE.FBASEQTY > 0 AND (( TKE.FLINKTYPE = '4' AND 1= ( SELECT COUNT(1) FROM T_BAS_SYSPARAMETER WHERE fparameterobjid='MFG_PLNParam' AND fparameters.exist('/BillHead[IsEnableReserve="True"]')=0) ) OR (1=1 AND 0=(SELECT COUNT(1) FROM T_BAS_SYSPARAMETER WHERE fparameterobjid='MFG_PLNParam' AND fparameters.exist('/BillHead[IsEnableReserve="True"]')=0)) ) GROUP BY TKE.FSUPPLYINTERID ) b ON b.FSUPPLYINTERID = a.FID;
赞 31
31人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!