---------------2025-03-14
CREATE TABLE [dbo].[ZONE_PLANT] (
  [ID] numeric(19)  IDENTITY(1,1) NOT NULL,
  [ZONE_ID] int  NULL,
  [PLANE_LOCATION_ID] int  NULL,
  CONSTRAINT [PK__zone_pla__3213E83FEFD8CD6E] PRIMARY KEY CLUSTERED ([ID])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY]
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[ZONE_PLANT] SET (LOCK_ESCALATION = TABLE)


--------2025-03-26
ALTER TABLE   Item    add BE_WHOLE_LINE tinyint  NULL;
GO
update Item set BE_WHOLE_LINE=0 where BE_WHOLE_LINE is null;
GO

CREATE TABLE WorkOrderAllocationInfo (
    ID INT IDENTITY(1,1) PRIMARY KEY,
    WorkOrderNumber NVARCHAR(50),
    LineNumber INT,
    MaterialNumber NVARCHAR(50),
    Quantity float,
    Remarks NVARCHAR(200),
    LocationNumber NVARCHAR(50),
    Factory NVARCHAR(50),
    LOC NVARCHAR(50),
    AllocationDate DATE
);    
GO

-----------2025-04-02

ALTER TABLE   ITEM_KEY    add PROP_LC5 varchar(250) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE   ITEM_KEY    add PROP_LC6 varchar(250) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE   ASN_DETAIL    add PROP_LC5 varchar(250) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE   ASN_DETAIL    add PROP_LC6 varchar(250) COLLATE Chinese_PRC_CI_AS  NULL;
GO


------------2025-04-08
ALTER TABLE   ITEM    add POSITION_ID numeric(19)  NULL;
GO
ALTER TABLE ITEM
ADD CONSTRAINT FK_Item_POSITION_ID FOREIGN KEY (POSITION_ID)
REFERENCES [POSITION](ID);
GO

ALTER TABLE   LOCATION    add be_Urgent tinyint  NULL;
GO
update LOCATION set be_Urgent=0 where be_Urgent is null;
GO

------------2025-04-14
ALTER TABLE   WorkOrderAllocationInfo    add STORAGE_DATE DATE  ;

----创建非聚合索引
CREATE NONCLUSTERED INDEX [idx_task_item_key_id]
ON [dbo].[TASK] (
  [ITEM_KEY_ID] ASC
)


CREATE CLUSTERED INDEX idx_clust_EmployeeID ON Employees(EmployeeID);


-----------更新统计信息
UPDATE STATISTICS INVENTORY_LOG;
UPDATE STATISTICS BILL_TYPE;
UPDATE STATISTICS LOCATION;
UPDATE STATISTICS ZONE;
UPDATE STATISTICS ITEM_KEY;
UPDATE STATISTICS ITEM;
UPDATE STATISTICS TASK;
UPDATE STATISTICS INVENTORY;

--------2025-04-25
ALTER TABLE   TASK    add item_Code varchar(50) COLLATE Chinese_PRC_CI_AS  NULL;
GO

ALTER TABLE   SIGN_FILE    add LENGTH  float(20)  NULL;
GO
ALTER TABLE   SIGN_FILE    add WIDTH float(20)  NULL;
GO
ALTER TABLE   SIGN_FILE    add HEIGHT  float(20)  NULL;
GO
ALTER TABLE   SIGN_FILE    add WEIGHT  float(20)  NULL;
GO
ALTER TABLE   SIGN_FILE    add VOLUME  float(20)  NULL;
GO
ALTER TABLE   SIGN_FILE    add PACK_NUMBER  int  NULL;
GO
update SIGN_FILE set LENGTH=0 where LENGTH is null;
GO
update SIGN_FILE set WIDTH=0 where WIDTH is null;
GO
update SIGN_FILE set HEIGHT=0 where HEIGHT is null;
GO
update SIGN_FILE set WEIGHT=0 where WEIGHT is null;
GO
update SIGN_FILE set VOLUME=0 where VOLUME is null;
GO
update SIGN_FILE set PACK_NUMBER=0 where PACK_NUMBER is null;
GO

--------2025-05-06
ALTER TABLE   Item    add be_Image_File tinyint  NULL;
GO
update Item set be_Image_File=0 where be_Image_File is null;
GO


CREATE NONCLUSTERED INDEX [IDX_INVENTORY_STOCK_ID]
ON [dbo].[INVENTORY] (
  [LP] ASC
)
----非空索引
CREATE INDEX FK_default_Warehouse_Area_ID_Null ON Item (default_Warehouse_Area_ID) WHERE default_Warehouse_Area_ID IS NULL;

CREATE INDEX FK_default_Warehouse_Area_ID_Null ON Item (default_Warehouse_Area_ID) WHERE default_Warehouse_Area_ID IS NULL;

---//修改字段类型
ALTER TABLE Employees
ALTER COLUMN Name VARCHAR(30);

ALTER TABLE INTERFACE_LOG
ALTER COLUMN REQUEST VARCHAR(200);
ALTER TABLE INTERFACE_LOG
ALTER COLUMN FILE_NAME VARCHAR(200);
---修改字段名
EXEC sp_rename    'ITEM_KEY.PROP_LC5', 'PROP_LC10', 'COLUMN';


ALTER TABLE   ITEM_KEY    add PROP_LC5 varchar(200) COLLATE Chinese_PRC_CI_AS  NULL;

-------2025-05-23
ALTER TABLE   INVENTORY    add Item_Code varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;


-----2025-05-28

ALTER TABLE   SIGN_FILE    add viewfileUrl varchar(200) COLLATE Chinese_PRC_CI_AS  NULL;

select * from  SIGN_FILE  where viewfileUrl is not null;
update SIGN_FILE set viewfileUrl=remark where viewfileUrl is null and remark is not  null;
UPDATE SIGN_FILE
SET viewfileUrl = REPLACE(viewfileUrl, 'http://192.168.2.88:8080', '*.asnImageServlet?imageSourcePath=C:/tomcat-8080/webapps')
WHERE viewfileUrl is not null;

---------2025-06-12
ALTER TABLE PICK_TICKET_DETAIL
ALTER COLUMN PROP_C8 VARCHAR(400);

---//查询sql server 版本号
SELECT @@VERSION;




-------2025-06-30


CREATE TABLE [dbo].[MATERIAL_FACTORY] (
  [ID] numeric(19)  IDENTITY(1,1) NOT NULL,
  [item_ID] numeric(19)  NOT NULL,
  [plan_Code] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
  [be_Mark_Weight] tinyint  NULL,
  [description] varchar(200) COLLATE Chinese_PRC_CI_AS  NULL,
  [create_Date] datetime  NULL,
  CONSTRAINT [PK__MATERIAL__3214EC272AE3C342] PRIMARY KEY CLUSTERED ([ID])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY],
  CONSTRAINT [FK7D07A1D263DC565] FOREIGN KEY ([item_ID]) REFERENCES [dbo].[ITEM] ([ID]) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT [UQ__MATERIAL__1B62B42A15045732] UNIQUE NONCLUSTERED ([item_ID] ASC, [plan_Code] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY]
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[MATERIAL_FACTORY] SET (LOCK_ESCALATION = TABLE)


ALTER TABLE   ITEM_KEY    add be_Mark_Weight tinyint  NULL;
GO
update ITEM_KEY set be_Mark_Weight=(select item.be_Mark_Weight from item item where item.id=item_id) where be_Mark_Weight is null;
GO

ALTER TABLE   ASN_DETAIL    add be_Mark_Weight tinyint  NULL;
GO
update ASN_DETAIL set be_Mark_Weight=(select item.be_Mark_Weight from item item where item.id=item_id) where be_Mark_Weight is null;
GO


--热度值查询
select max(t.id),i.id,count(DISTINCT t.BILLCODE+lc.CODE+i.CODE)
from task t
left join ITEM_KEY ik on ik.id=t.ITEM_KEY_ID
left join item i  on i.id=ik.ITEM_ID
left join LOCATION  lc on lc.id=t.SRC_LOC_ID
where t.CREATE_TIME>='2025-03-01' AND
t.CREATE_TIME<'2025-04-01' AND
t.MOVED_QUANTITY_MU>0
GROUP BY i.id;


select max(t.id),i.id,max(i.CODE),count(DISTINCT t.BILLCODE+lc.CODE+i.CODE)
from task t
left join ITEM_KEY ik on ik.id=t.ITEM_KEY_ID
left join item i  on i.id=ik.ITEM_ID
left join LOCATION  lc on lc.id=t.SRC_LOC_ID
where t.CREATE_TIME>='2025-03-01' AND
t.CREATE_TIME<'2025-04-01' AND
t.MOVED_QUANTITY_MU>0
GROUP BY i.id;

select id,code,SALE_TIME from item where id in(select i.id
from task t
left join ITEM_KEY ik on ik.id=t.ITEM_KEY_ID
left join item i  on i.id=ik.ITEM_ID
where t.CREATE_TIME>='2025-03-01' AND
t.CREATE_TIME<'2025-04-01' AND
t.MOVED_QUANTITY_MU>0
GROUP BY i.id)

//刷新热度值
update ITEM  set heat_Six_Months =(select count(DISTINCT t.BILLCODE+lc.CODE+i.CODE)
from task t
left join ITEM_KEY ik on ik.id=t.ITEM_KEY_ID
left join item i  on i.id=ik.ITEM_ID
left join LOCATION  lc on lc.id=t.SRC_LOC_ID
where t.operate_TIME>='2025-03-01' AND
t.operate_TIME<'2025-09-01' AND ITEM.id=i.id and 
t.MOVED_QUANTITY_MU>0 and t.TYPE='MV_PICKTICKET'
GROUP BY i.id) 

where ITEM.id in(select i1.id
from task t1
left join ITEM_KEY ik1 on ik1.id=t1.ITEM_KEY_ID
left join item i1  on i1.id=ik1.ITEM_ID
where t1.operate_TIME>='2025-03-01' AND
t1.operate_TIME<'2025-09-01' AND
t1.MOVED_QUANTITY_MU>0 and t1.TYPE='MV_PICKTICKET'
GROUP BY i1.id);


UPDATE ITEM SET SALE_TIME=1 WHERE heat_Six_Months IS NULL;
GO
UPDATE ITEM SET heat_Three_Months=1 WHERE heat_Three_Months IS NULL;
GO
UPDATE ITEM SET heat_Six_Months=1 WHERE heat_Six_Months IS NULL;




------20250709更新工厂序列号表
select * from MATERIAL_FACTORY;

INSERT into MATERIAL_FACTORY (ITEM_id,plan_Code,BE_MARK_WEIGHT,CREATE_DATE)
select item_id,prop_C9,BE_MARK_WEIGHT,GETDATE() 
from ITEM_KEY  
where prop_C9 is not null and item_id not in(select m.item_id from MATERIAL_FACTORY m )
GROUP BY item_id,prop_C9,BE_MARK_WEIGHT 


-----------20250714
ALTER TABLE   library_task    add issue_Date datetime NULL;
GO
ALTER TABLE   library_task    add arrival_Date datetime NULL;
GO




select 
max(inv.id) as id,
max(loc.code) as locCode,
item.id as item_id,
max(item.code) as itemCode,
max(item.name) as itemName,
max(inv.BAR_CODE) as barCode,
max(inv.STATUS) as status,
max(inv.LOCATION_ID) as LOCATION_ID,
max(inv.ITEM_KEY_ID) as ITEM_KEY_ID,
sum(inv.QUANTITY) as quantity,	
sum(inv.QUEUED_QUANTITY) as queuedQuantity,
max(ik.PROP_C1) as propC1,
max(ik.PROP_C2) as propC2,
max(ik.PROP_C3) as propC3,
max(ik.PROP_C4) as propC4,
max(ik.PROP_C5) as propC5,
max(ik.PROP_C6) as propC6,
max(ik.PROP_C7) as propC7,
max(ik.PROP_C8) as propC8,
max(ik.PROP_C9) as propC9,
max(ik.PROP_LC1) as PROP_LC1,
max(ik.PROP_LC2) as PROP_LC2,
max(ik.PROP_LC3) as PROP_LC3,
max(ik.PROP_LC4) as PROP_LC4,
max(ik.PROP_LC5) as PROP_LC5,
max(inv.SOI) as soi,
max(ik.PROP_D1) as propD1,
max(ik.PROP_D2) as propD2,
max(ik.PROP_D3) as propD3,
max(z.name) as kqName,
max(z.ID) as ZONE_ID,

case 
	when max(inv.coun_Plan) is null then 0
	else  1
end as lock,
case 
	when max(z.id) in(1766,1763,1765) 
then max(ls1.name) else max(ls.name) end as LSname,
max(item.Be_SAP_Item) Be_SAP_Item,
max(item.VOLUME) VOLUME,
max(item.BE_WEIGHT) BE_WEIGHT,
max(item.SHIP_RULES) SHIP_RULES,
max(item.FEE_TYPE) FEE_TYPE,
max(item.SINGLE_ITEM) SINGLE_ITEM,
max(item.BE_MARK_WEIGHT) BE_MARK_WEIGHT,
max(item.UNIT) UNIT,
max(item.STR_EXTEND1) STR_EXTEND1,
max(item.STR_EXTEND2) STR_EXTEND2,
max(item.STR_EXTEND3) STR_EXTEND3,
max(item.STR_EXTEND4) STR_EXTEND4,
ISNULL(stock1.agvFlag, 0) agvFlag,
loc.id locId,
max(lts.NAME) as lts_name,
item.ISBOM validity,
CONVERT(varchar(100), min(inv.STORAGE_DATE), 20) as storageDate,
 max(inv.moveStatus) moveStatus,
inv.statuss,
max(z1.NAME) as z1_name,
max(inv.description) as invDes,
MAX(stock1.WEIGHT) as WEIGHT

from INVENTORY inv
left join ZONE kq on kq.id=inv.warehouse_Area_ID 
left join LOCATION loc on loc.id=inv.LOCATION_ID
left join LOCATIONS lts on lts.id=loc.LOCATIONS_ID
left join STOCK stock1 on stock1.id=loc.stock
left join LOCATION_STORAGE ls1 on ls1.ID=stock1.locationStorage_ID
left join ZONE z on z.ID =loc.ZONE_ID
left join ITEM_KEY ik on ik.id=inv.ITEM_KEY_ID
left join ITEM item on item.id=ik.ITEM_ID
left join STOCK stock on stock.ID=inv.LP
left join LOCATION_STORAGE ls on ls.ID=stock.locationStorage_ID
left join ZONE z1 on z1.id=item.default_Warehouse_Area_ID
where 1=1
and inv.QUANTITY>0
group by item.id,loc.id,ik.PROP_C1,ik.PROP_C2,ik.PROP_C3,ik.PROP_C5,ik.PROP_C9,ik.PROP_D2,inv.STATUS,item.isBOM,inv.statuss,stock1.agvFlag


GO


---优化索引
DECLARE @DatabaseName NVARCHAR(255) = 'LD_WMSDB'
DECLARE @TableName NVARCHAR(255)
DECLARE @SchemaName NVARCHAR(255)
DECLARE @SQL NVARCHAR(MAX)

-- 创建游标获取所有用户表
DECLARE TableCursor CURSOR FOR
SELECT s.name AS SchemaName, t.name AS TableName
FROM sys.tables t
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.is_ms_shipped = 0  -- 排除系统表
ORDER BY s.name, t.name

-- 打开游标
OPEN TableCursor

-- 获取第一条记录
FETCH NEXT FROM TableCursor INTO @SchemaName, @TableName

-- 循环处理每张表
WHILE @@FETCH_STATUS = 0
BEGIN
    -- 构建并执行重建索引的SQL
    SET @SQL = 'USE [' + @DatabaseName + ']; ALTER INDEX ALL ON [' + @SchemaName + '].[' + @TableName + '] REBUILD;'
    PRINT '正在重建: ' + @SchemaName + '.' + @TableName + ' 的索引'
    EXEC sp_executesql @SQL
    
    -- 获取下一条记录
    FETCH NEXT FROM TableCursor INTO @SchemaName, @TableName
END

-- 关闭并释放游标
CLOSE TableCursor
DEALLOCATE TableCursor

PRINT '所有表索引重建完成'



------更新统计信息
USE LD_WMSDB;
GO

DECLARE @tableName NVARCHAR(255)
DECLARE @schemaName NVARCHAR(255)
DECLARE @sql NVARCHAR(500)

DECLARE table_cursor CURSOR FOR
SELECT s.name, t.name 
FROM sys.tables t
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.type = 'U' -- 只包含用户表

OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @schemaName, @tableName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'UPDATE STATISTICS [' + @schemaName + '].[' + @tableName + '] WITH SAMPLE 10 PERCENT'
    PRINT '正在更新: ' + @schemaName + '.' + @tableName
    EXEC sp_executesql @sql
    
    FETCH NEXT FROM table_cursor INTO @schemaName, @tableName
END

CLOSE table_cursor
DEALLOCATE table_cursor
PRINT '所有表统计信息更新完成'



--------------20250721
ALTER TABLE   Bill_Type_Config    add prioritize  int  NULL;

GO
UPDATE Bill_Type_Config SET prioritize =1 WHERE prioritize IS NULL;


---------2025-08-25

ALTER TABLE   item    add heat_Three_Months  int  NULL;
GO
ALTER TABLE   item    add heat_Six_Months  int  NULL;
GO
ALTER TABLE   item    add PICKING_DATE  DATE  NULL;
GO
UPDATE ITEM SET heat_Three_Months=1 WHERE heat_Three_Months IS NULL;
GO
UPDATE ITEM SET heat_Six_Months=1 WHERE heat_Six_Months IS NULL;


----------------------------
select max(v.id) id,max(v.COUNT_PLAN_ID) COUNT_PLAN_ID ,
				count(v.hs) hs,
				max(v.z_name) z_name,
				max(v.number) number,
				v.NEED_COUNT needCount,
				max(v.targetArea) targetArea,
				count(v.fno) finishNo,
				v.count_Code from 

(select max(cr.id) id,max(cr.COUNT_PLAN_ID) COUNT_PLAN_ID ,
				count(cr.id) hs,
				max(z.NAME) z_name,
				ISNULL(max(s.number),0) number,
				count(cr.id) fno,
				cr.NEED_COUNT,
				max(cr.targetArea) targetArea,
				max(cr.count_Code) count_Code
		from COUNT_RECORD cr
		left join LOCATION loc on loc.ID=cr.LOCATION_ID
		left join [ZONE] z on z.ID=loc.ZONE_ID
		left join ShiXiao s on s.warehouseArea=z.ID
		group by cr.count_Code,cr.COUNT_PLAN_ID,cr.ITEM_KEY_ID,cr.LOCATION_ID,cr.NEED_COUNT) v
		
		group by v.count_Code,v.COUNT_PLAN_ID,v.NEED_COUNT
	---------调整为------------
	select max(cr.id) id,max(cr.COUNT_PLAN_ID) COUNT_PLAN_ID ,
				count(DISTINCT ik.LOT+loc.CODE) hs,
				max(z.NAME) z_name,
				ISNULL(max(s.number),0) number,
				count(DISTINCT ik.LOT+loc.CODE) finishNo,
				cr.NEED_COUNT needCount,
				max(cr.targetArea) targetArea,
				max(cr.count_Code) count_Code
		from COUNT_RECORD cr
		left join LOCATION loc on loc.ID=cr.LOCATION_ID
		left join [ZONE] z on z.ID=loc.ZONE_ID
		left join ShiXiao s on s.warehouseArea=z.ID
    LEFT JOIN ITEM_KEY ik ON ik.id=cr.ITEM_KEY_ID
 
		group by cr.count_Code,cr.COUNT_PLAN_ID,cr.NEED_COUNT;	
		
		
		
	------------------
	select max(z.id) id,max(cr.COUNT_PLAN_ID) COUNT_PLAN_ID ,
				count(DISTINCT ik.LOT+loc.CODE) hs,
				max(z.NAME) z_name,
				max(s.number) number
		from COUNT_RECORD as cr
		left join LOCATION loc on loc.ID=cr.LOCATION_ID
		left join [ZONE] z on z.ID=loc.ZONE_ID
		left join ShiXiao s on s.warehouseArea=z.ID
        LEFT JOIN ITEM_KEY ik ON ik.id=cr.ITEM_KEY_ID
		group by cr.COUNT_PLAN_ID,z.id
	---------------上面视图调整为下面的视图
	    select max(z.id) id,max(cr.COUNT_PLAN_ID) COUNT_PLAN_ID ,
				count(DISTINCT ik.LOT+loc.CODE) hs,
				max(z.NAME) z_name,
				max(s.number) number
		from COUNT_RECORD as cr
		left join LOCATION loc on loc.ID=cr.LOCATION_ID
		left join [ZONE] z on z.ID=loc.ZONE_ID
		left join ShiXiao s on s.warehouseArea=z.ID
        LEFT JOIN ITEM_KEY ik ON ik.id=cr.ITEM_KEY_ID
		group by cr.COUNT_PLAN_ID,z.id	
		
----------------------------查询料号信息的语句
		select 
 item.id as 序号,
 item.code as 料号,
 item.name as 描述,
 item.CA_name as 包装类型,
 item.pack_Number as 规格数量,
item.weight as 规格重量,
item.suggest as 净重,
 item.length as 长,
item.width as 宽,
item.height as 高,
item.volume as 体积 ,
 item.unit as 单位,
 wa.name  as 推荐库区,
 w.name as 默认库区,
 item.good_Type as 类型,
 item.ship_Rules as 价值等级,
 item.valid_Period as 保质期,
 item.ALERT_LEADING_DAYS as 预警天数,
 item.SINGLE_ITEM as 批次号,
 item.brand as 批次规则,
 item.be_Unpacking as 是否包装更新,
 item.smallUnit as 包装,
 item.be_Mark_Weight as 是否序列号,
 item.isBOM as 是否有限期,
 item.be_Weight as 危险品,
 item.be_Inv as 是否由库存,
 item.be_SAP_Item as 是否SAP料号,
 item.be_Whole_Line as 是否米数,
 P.name as 库存组,
 item.sale_Time as 热度值,
 item.str_Extend1 as 备注,
 item.str_Extend2 as 币种,
 item.str_Extend3 as 元值
 from ITEM item
 left join ZONE  wa on  wa.id=item.warehouse_Area_ID
 left join ZONE w on w.id=item.default_Warehouse_Area_ID
 left join position p on p.id=item.POSITION_ID 
 
 ---------------2025-08-30
 ALTER TABLE   Receiving_Inventory    add pick_Date  datetime  NULL;
GO
 


select * from item where volume>0 and be_Image_File=0;
update item set largeWeight=volume where volume>0 and be_Image_File=0;
update item set volume=0 where volume>0 and be_Image_File=0;






----------2025-09-10
CREATE TABLE [dbo].[Automatic_Wave_Doc] (
  [ID] numeric(19)  IDENTITY(1,1) NOT NULL,
  [hour1] int  NULL,
  [minute1] int  NULL,
  [be_Enabled1] tinyint  NULL,
  [hour2] int  NULL,
  [minute2] int  NULL,
  [be_Enabled2] tinyint  NULL,
  [hour3] int  NULL,
  [minute3] int  NULL,
  [be_Enabled3] tinyint  NULL,
  [hour4] int  NULL,
  [minute4] int  NULL,
  [be_Enabled4] tinyint  NULL,
  [hour5] int  NULL,
  [minute5] int  NULL,
  [be_Enabled5] tinyint  NULL,
  [hour6] int  NULL,
  [minute6] int  NULL,
  [be_Enabled6] tinyint  NULL,
  [hour7] int  NULL,
  [minute7] int  NULL,
  [be_Enabled7] tinyint  NULL,
  [hour8] int  NULL,
  [minute8] int  NULL,
  [be_Enabled8] tinyint  NULL,
  [hour9] int  NULL,
  [minute9] int  NULL,
  [be_Enabled9] tinyint  NULL,
  [hour10] int  NULL,
  [minute10] int  NULL,
  [be_Enabled10] tinyint  NULL,
  CONSTRAINT [PK__Automati__3214EC27832C79F0] PRIMARY KEY CLUSTERED ([ID])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY]
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[Automatic_Wave_Doc] SET (LOCK_ESCALATION = TABLE)


----2025-10-15

  ALTER TABLE   ASN_DETAIL    add length  float  NULL;
 GO
  ALTER TABLE   ASN_DETAIL    add width  float  NULL;
  GO
  ALTER TABLE   ASN_DETAIL    add height  float  NULL;
  GO 
  UPDATE ASN_DETAIL SET length=0 WHERE length IS NULL;
  go
   UPDATE ASN_DETAIL SET width=0 WHERE width IS NULL;
  go
   UPDATE ASN_DETAIL SET height=0 WHERE height IS NULL;
  go
  
    ALTER TABLE   Automatic_Wave_Doc    add START_DATE  datetime  NULL;
 GO
     ALTER TABLE   Automatic_Wave_Doc    add END_DATE  datetime  NULL;
  GO
  
ALTER TABLE   Automatic_Wave_Doc    add be_lx1  tinyint  NULL;    
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx2  tinyint  NULL;     
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx3  tinyint  NULL;      
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx4  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx5  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx6  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx7  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx8  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx9  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx10  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx11  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx12  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx13  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx14  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lx15  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lk1  tinyint  NULL; 
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lk2  tinyint  NULL;
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lk3  tinyint  NULL;     
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lk4  tinyint  NULL;
 GO
ALTER TABLE   Automatic_Wave_Doc    add be_lk5  tinyint  NULL;
 GO 
update Automatic_Wave_Doc set be_lx1=0 where be_lx1 is null;
 GO 
update Automatic_Wave_Doc set be_lx2=0 where be_lx2 is null;
 GO 
update Automatic_Wave_Doc set be_lx3=0 where be_lx3 is null;
 GO 
update Automatic_Wave_Doc set be_lx4=0 where be_lx4 is null;
 GO 
update Automatic_Wave_Doc set be_lx5=0 where be_lx5 is null;
 GO 
update Automatic_Wave_Doc set be_lx6=0 where be_lx6 is null;
 GO 
update Automatic_Wave_Doc set be_lx7=0 where be_lx7 is null;
 GO 
update Automatic_Wave_Doc set be_lx8=0 where be_lx8 is null;
 GO 
update Automatic_Wave_Doc set be_lx9=0 where be_lx9 is null;
 GO 
update Automatic_Wave_Doc set be_lx10=0 where be_lx10 is null;
 GO 
update Automatic_Wave_Doc set be_lx11=0 where be_lx11 is null;
 GO 
update Automatic_Wave_Doc set be_lx12=0 where be_lx12 is null;
 GO 
update Automatic_Wave_Doc set be_lx13=0 where be_lx13 is null;
 GO 
update Automatic_Wave_Doc set be_lx14=0 where be_lx14 is null;
 GO 
update Automatic_Wave_Doc set be_lx15=0 where be_lx15 is null;
 GO 
update Automatic_Wave_Doc set be_lx1=0 where be_lx1 is null;
 GO 
update Automatic_Wave_Doc set be_lk1=0 where be_lk1 is null;
 GO 
update Automatic_Wave_Doc set be_lk2=0 where be_lk2 is null;
 GO 
update Automatic_Wave_Doc set be_lk3=0 where be_lk3 is null;
 GO 
update Automatic_Wave_Doc set be_lk4=0 where be_lk4 is null;
 GO 
update Automatic_Wave_Doc set be_lk5=0 where be_lk5 is null;
GO


---------------------原库存视图
select 
max(inv.id) as id,
max(loc.code) as locCode,
item.id as item_id,
max(item.code) as itemCode,
max(item.name) as itemName,
max(inv.BAR_CODE) as barCode,
max(inv.STATUS) as status,
max(inv.LOCATION_ID) as LOCATION_ID,
max(inv.ITEM_KEY_ID) as ITEM_KEY_ID,
sum(inv.QUANTITY) as quantity,	
sum(inv.QUEUED_QUANTITY) as queuedQuantity,
max(ik.PROP_C1) as propC1,
max(ik.PROP_C2) as propC2,
max(ik.PROP_C3) as propC3,
max(ik.PROP_C4) as propC4,
max(ik.PROP_C5) as propC5,
max(ik.PROP_C6) as propC6,
max(ik.PROP_C7) as propC7,
max(ik.PROP_C8) as propC8,
max(ik.PROP_C9) as propC9,
max(ik.PROP_LC1) as PROP_LC1,
max(ik.PROP_LC2) as PROP_LC2,
max(ik.PROP_LC3) as PROP_LC3,
max(ik.PROP_LC4) as PROP_LC4,
max(ik.PROP_LC5) as PROP_LC5,
max(inv.SOI) as soi,
max(ik.PROP_D1) as propD1,
max(ik.PROP_D2) as propD2,
max(ik.PROP_D3) as propD3,
max(z.name) as kqName,
max(z.ID) as ZONE_ID,

case 
	when max(inv.coun_Plan) is null then 0
	else  1
end as lock,
case 
	when max(z.id) in(1766,1763,1765) 
then max(ls1.name) else max(ls.name) end as LSname,
max(item.Be_SAP_Item) Be_SAP_Item,
max(item.VOLUME) VOLUME,
max(item.BE_WEIGHT) BE_WEIGHT,
max(item.SHIP_RULES) SHIP_RULES,
max(item.FEE_TYPE) FEE_TYPE,
max(item.SINGLE_ITEM) SINGLE_ITEM,
max(item.BE_MARK_WEIGHT) BE_MARK_WEIGHT,
max(item.UNIT) UNIT,
max(item.STR_EXTEND1) STR_EXTEND1,
max(item.STR_EXTEND2) STR_EXTEND2,
max(item.STR_EXTEND3) STR_EXTEND3,
max(item.STR_EXTEND4) STR_EXTEND4,
ISNULL(stock1.agvFlag, 0) agvFlag,
loc.id locId,
max(lts.NAME) as lts_name,
item.ISBOM validity,
CONVERT(varchar(100), min(inv.STORAGE_DATE), 20) as storageDate,
 max(inv.moveStatus) moveStatus,
inv.statuss,
max(z1.NAME) as z1_name,
max(inv.description) as invDes,
MAX(stock1.WEIGHT) as WEIGHT

from INVENTORY inv
left join ZONE kq on kq.id=inv.warehouse_Area_ID 
left join LOCATION loc on loc.id=inv.LOCATION_ID
left join LOCATIONS lts on lts.id=loc.LOCATIONS_ID
left join STOCK stock1 on stock1.id=loc.stock
left join LOCATION_STORAGE ls1 on ls1.ID=stock1.locationStorage_ID
left join ZONE z on z.ID =loc.ZONE_ID
left join ITEM_KEY ik on ik.id=inv.ITEM_KEY_ID
left join ITEM item on item.id=ik.ITEM_ID
left join STOCK stock on stock.ID=inv.LP
left join LOCATION_STORAGE ls on ls.ID=stock.locationStorage_ID
left join ZONE z1 on z1.id=item.default_Warehouse_Area_ID
where 1=1
and inv.QUANTITY>0
group by item.id,loc.id,ik.PROP_C1,ik.PROP_C2,ik.PROP_C3,
ik.PROP_C5,ik.PROP_C9,ik.PROP_D2,inv.STATUS,
item.isBOM,inv.statuss,stock1.agvFlag


----------------新视图
WITH InventoryAgg AS (
    SELECT 
        ITEM_KEY_ID,
        LOCATION_ID,
        MAX(id) as max_id,
        MAX(BAR_CODE) as barCode,
        MAX(STATUS) as status,
        SUM(QUANTITY) as quantity,	
        SUM(QUEUED_QUANTITY) as queuedQuantity,
        MAX(SOI) as soi,
        MAX(coun_Plan) as coun_Plan,
        MAX(moveStatus) as moveStatus,
        MAX(statuss) as inv_statuss,
        MAX(description) as description,
        MIN(STORAGE_DATE) as storageDate,
        MAX(warehouse_Area_ID) as warehouse_Area_ID,
        MAX(LP) as LP
    FROM INVENTORY 
    WHERE QUANTITY > 0
    GROUP BY ITEM_KEY_ID, LOCATION_ID,status,statuss
)
SELECT 
    inv.max_id as id,
    loc.code as locCode,
    item.id as item_id,
    item.code as itemCode,
    item.name as itemName,
    inv.barCode,
    inv.status,
    inv.LOCATION_ID,
    inv.ITEM_KEY_ID,
    inv.quantity,	
    inv.queuedQuantity,
    ik.PROP_C1 as propC1,
    ik.PROP_C2 as propC2,
    ik.PROP_C3 as propC3,
    ik.PROP_C4 as propC4,
    ik.PROP_C5 as propC5,
    ik.PROP_C6 as propC6,
    ik.PROP_C7 as propC7,
    ik.PROP_C8 as propC8,
    ik.PROP_C9 as propC9,
    ik.PROP_LC1 as PROP_LC1,
    ik.PROP_LC2 as PROP_LC2,
    ik.PROP_LC3 as PROP_LC3,
    ik.PROP_LC4 as PROP_LC4,
    ik.PROP_LC5 as PROP_LC5,
    inv.soi,
    ik.PROP_D1 as propD1,
    ik.PROP_D2 as propD2,
    ik.PROP_D3 as propD3,
    z.name as kqName,
    z.ID as ZONE_ID,
    CASE WHEN inv.coun_Plan IS NULL THEN 0 ELSE 1 END as lock,
    CASE WHEN z.id IN (1766,1763,1765) THEN ls1.name ELSE ls.name END as LSname,
    item.Be_SAP_Item,
    item.VOLUME,
    item.BE_WEIGHT,
    item.SHIP_RULES,
    item.FEE_TYPE,
    item.SINGLE_ITEM,
    item.BE_MARK_WEIGHT,
    item.UNIT,
    item.STR_EXTEND1,
    item.STR_EXTEND2,
    item.STR_EXTEND3,
    item.STR_EXTEND4,
    ISNULL(stock1.agvFlag, 0) as agvFlag,
    loc.id as locId,
    lts.NAME as lts_name,
    item.ISBOM as validity,
    CONVERT(varchar(100), inv.storageDate, 20) as storageDate,
    inv.moveStatus,
    inv.inv_statuss as statuss,
    z1.NAME as z1_name,
    inv.description as invDes,
    stock1.WEIGHT as WEIGHT
FROM InventoryAgg inv
INNER JOIN ITEM_KEY ik ON ik.id = inv.ITEM_KEY_ID
INNER JOIN ITEM item ON item.id = ik.ITEM_ID
INNER JOIN LOCATION loc ON loc.id = inv.LOCATION_ID
LEFT JOIN ZONE kq ON kq.id = inv.warehouse_Area_ID 
LEFT JOIN LOCATIONS lts ON lts.id = loc.LOCATIONS_ID
LEFT JOIN STOCK stock1 ON stock1.id = loc.stock
LEFT JOIN LOCATION_STORAGE ls1 ON ls1.ID = stock1.locationStorage_ID
LEFT JOIN ZONE z ON z.ID = loc.ZONE_ID
LEFT JOIN STOCK stock ON stock.ID = inv.LP
LEFT JOIN LOCATION_STORAGE ls ON ls.ID = stock.locationStorage_ID
LEFT JOIN ZONE z1 ON z1.id = item.default_Warehouse_Area_ID;


