------------2024-02-02
select max(t.id) ID , max(z.NAME) zname ,COUNT(*) hs,t.END_REGION
	
			from task t 
			left join LOCATION loc1 on loc1.ID=t.SRC_LOC_ID
			left join [ZONE] z on z.ID=loc1.ZONE_ID
			left join LOCATION loc2 on loc2.ID=t.DST_LOC_ID
			left join agvtask agv on agv.id=t.agvTask_ID
			where t.MOVED_QUANTITY_MU=0 and t.PLAN_QUANTITY>0
			group by z.id,t.END_REGION

			
			----
CREATE TABLE [dbo].[Plant_Info] (
  [id] numeric(19)  IDENTITY(1,1) NOT NULL,
  [code] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
  [name] varchar(150) COLLATE Chinese_PRC_CI_AS  NULL,
  [description] varchar(250) COLLATE Chinese_PRC_CI_AS  NULL,
  [disabled] tinyint  NULL,
  CONSTRAINT [PK__Plant_In__3213E83F87E6D235] 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].[Plant_Info] SET (LOCK_ESCALATION = TABLE)

CREATE TABLE [dbo].[Plant_Location] (
  [id] numeric(19)  IDENTITY(1,1) NOT NULL,
  [plant_Info_ID] numeric(19)  NULL,
  [code] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
  [name] varchar(150) COLLATE Chinese_PRC_CI_AS  NULL,
  [description] varchar(250) COLLATE Chinese_PRC_CI_AS  NULL,
  [disabled] tinyint  NULL,
  CONSTRAINT [PK__Plant_Lo__3213E83FC0BD957A] 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 [FK45667FAS549S5529SD1S] FOREIGN KEY ([plant_Info_ID]) REFERENCES [dbo].[Plant_Info] ([id]) ON DELETE NO ACTION ON UPDATE NO ACTION
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[Plant_Location] SET (LOCK_ESCALATION = TABLE)
---2024-2-28
ALTER TABLE    TASK   add  positionStatus   varchar(125) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE    ReturnTask   add  returnMSG   varchar(125) COLLATE Chinese_PRC_CI_AS  NULL;
GO

---------------2024-02-25
CREATE TABLE [dbo].[Urgent_Order] (
  [id] numeric(19)  IDENTITY(1,1) NOT NULL,
  [code] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
  [sourceId] varchar(150) COLLATE Chinese_PRC_CI_AS  NULL,
	[lineNumber] varchar(150) COLLATE Chinese_PRC_CI_AS  NULL,
  [description] varchar(250) COLLATE Chinese_PRC_CI_AS  NULL,
  [disabled] [datetime]  NULL,
	[quantity] [float]  NULL,
	
  CONSTRAINT [PK_Urgent_Order_3213E83F87E6D235] 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


--------删除表
drop table INVOICE
drop table FEE_TYPE
drop table FEE
drop table bill_Fee
drop table apply_Bill
drop table apply_Bill_Detail
drop table inventory_Count
drop table Plan_Pick_Ticket
drop table Plan_Pick_Ticket_Detail
drop table ASN_CA_VIEW
drop table ASN_WORK_VIEW
drop table Item_Out_In_View
drop table pickTicket_View
drop table MES_TASK
drop table asn_inventory_view
drop table ZX_OUT_DETAIL
drop table ZONE_T_LOG

drop table agvtask0
-----------------2024-05-04

ALTER TABLE    agvtask   add returnTime [datetime]  NULL;

ALTER TABLE    agvtask   add founderName varchar(50) COLLATE Chinese_PRC_CI_AS  NULL;

ALTER TABLE    agvtask   add remarks varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;


--------------2024-06-06

ALTER TABLE    agvtask   add urgentTime [datetime]   NULL;

--------------2024-07-03
ALTER TABLE    PICK_TICKET_DETAIL   add strBill16 varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    PICK_TICKET_DETAIL   add strBill17 varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    PICK_TICKET_DETAIL   add strBill18 varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    PICK_TICKET_DETAIL   add strBill19 varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    PICK_TICKET_DETAIL   add strBill20 varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;


--------------2024-07-15
ALTER TABLE   Plant_Location    add beWave tinyint  NULL;
GO
update Plant_Location set beWave=1 where beWave is NULL;
---------------2024-08-21
ALTER TABLE   Plant_Info    add re_Deposit tinyint  NULL;
GO
update Plant_Info set re_Deposit=1 where re_Deposit is NULL;
GO
ALTER TABLE    Plant_Location   add loc_Floor varchar(150) COLLATE Chinese_PRC_CI_AS  NULL;

GO
ALTER TABLE    task   add strBill6 varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE    task   add strBill7 varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE    task   add strBill8 varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE    task   add strBill9 varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE    task   add strBill10 varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
GO
ALTER TABLE   BILL_TYPE    add be_Presentation tinyint  NULL;
GO
update BILL_TYPE set be_Presentation=1 where be_Presentation is NULL;
GO
ALTER TABLE   BILL_TYPE    add be_warehouse tinyint  NULL;
GO
update BILL_TYPE set be_warehouse=1 where be_warehouse is NULL;
GO

----20240827
ALTER TABLE   Plant_Location    add be_Register tinyint  NULL;
GO
update Plant_Location set be_Register=0 where be_Register is NULL;
GO
----------20240904
CREATE TABLE [dbo].[Receiving_Inventory] (
  [ID] numeric(19)  IDENTITY(1,1) NOT NULL,
  [DISCRIMINATOR] varchar(255) COLLATE Chinese_PRC_CI_AS  NOT NULL,
  [LOCATION_ID] numeric(19)  NOT NULL,
  [ITEM_KEY_ID] numeric(19)  NOT NULL,
  [RECEIVED_QUANTITY] float(53)  NULL,
  [RECEIVED_QUANTITY_MU] float(53)  NULL,
  [RECEIVE_DATE] datetime  NULL,
  [OPERATOR] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [DESCRIPTION] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
  [BE_MOVED] tinyint  NULL,
  [BE_PRINT] tinyint  NULL,
  [BAR_CODE] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
  [WEIGHT] float(53)  NULL,
  [VOLUME] float(53)  NULL,
  [strBill1] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [strBill2] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [strBill3] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [strBill4] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [strBill5] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  CONSTRAINT [PK__Receivin__3214EC27602B8EEB] 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 [FK45F2467D9E10C788] FOREIGN KEY ([ITEM_KEY_ID]) REFERENCES [dbo].[ITEM_KEY] ([ID]) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT [FK45F2467DF283ACA5] FOREIGN KEY ([LOCATION_ID]) REFERENCES [dbo].[LOCATION] ([ID]) ON DELETE NO ACTION ON UPDATE NO ACTION
)  
ON [PRIMARY]
GO

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


ALTER TABLE   Receiving_Inventory    add shipped_Quantity float(14)  NULL;
GO
update Receiving_Inventory set shipped_Quantity=0 where shipped_Quantity is NULL;
GO
------------------------
CREATE TABLE [dbo].[Sap_Out_Type] (
  [ID] numeric(19)  IDENTITY(1,1) NOT NULL,
  [CODE] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [NAME] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  [DISABLED] tinyint  NULL,
  [DESCRIPTION] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
  [operator] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
  CONSTRAINT [PK__Sap_Out___3214EC27251109D0] 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].[Sap_Out_Type] SET (LOCK_ESCALATION = TABLE)



ALTER TABLE   Receiving_Inventory    add receivedRecordId numeric(19)  NULL;


------------更新合单区库位上容器码
select loc.id ,loc.CODE,loc.name,loc.TYPE,STOCK,
	left(loc.CODE,6),(select s.id from stock s where s.lp=left(loc.CODE,6))
	from LOCATION loc  where type='合单'  
	update LOCATION set stock=(select s.id from stock s where s.lp=left(CODE,6))
	where type='合单'  

	
create  view inventory_current_month_summary as
SELECT ZONE_ID, LOG_TYPE, COUNT(*) AS Total
FROM inventory_log
JOIN location ON inventory_log.SRCLOC_ID = location.ID
WHERE YEAR(OCCUR_TIME) = YEAR(DATEADD(MONTH, -1, GETDATE()))
  AND MONTH(OCCUR_TIME) = MONTH(DATEADD(MONTH, -1, GETDATE()))
GROUP BY ZONE_ID, LOG_TYPE;

------
create  view inventory_summary_weekly as
SELECT   
    CONVERT(date, OCCUR_TIME) AS Date,  
    SUM( CASE WHEN LOG_TYPE = 'SHIPPING' THEN 1 ELSE 0 END) AS Delivery,  
    COUNT(DISTINCT CASE WHEN LOG_TYPE = 'HAND_ORDER' THEN related_Bill ELSE NULL END) AS Presentation, 
    SUM( CASE WHEN LOG_TYPE = 'RECEIVING' THEN 1 ELSE 0 END) AS Storage,
    CASE 
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Monday' THEN '周一'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Tuesday' THEN '周二'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Wednesday' THEN '周三'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Thursday' THEN '周四'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Friday' THEN '周五'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Saturday' THEN '周六'
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Sunday' THEN '周日'
    END AS Weekday 
FROM   
    inventory_log  
WHERE   

 OCCUR_TIME >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AND OCCUR_TIME < DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) + 1, 0)
GROUP BY   
    CONVERT(date, OCCUR_TIME),
    CASE 
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Monday' THEN 1
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Tuesday' THEN 2
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Wednesday' THEN 3
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Thursday' THEN 4
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Friday' THEN 5
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Saturday' THEN 6
        WHEN DATENAME(WEEKDAY, CONVERT(date, OCCUR_TIME)) = 'Sunday' THEN 7
    END;
    
create  view task_outbound_summary as
SELECT END_REGION,  
       quantity_met,  
       quantity_unmet,  
       quantity_met + quantity_unmet AS total_quantity 
FROM ( 
    SELECT END_REGION, 
           SUM(CASE WHEN MOVED_QUANTITY_MU >= PLAN_QUANTITY THEN 1 ELSE 0 END) AS quantity_met, 
           SUM(CASE WHEN MOVED_QUANTITY_MU < PLAN_QUANTITY THEN 1 ELSE 0 END) AS quantity_unmet 
    FROM TASK 
    WHERE END_REGION IS NOT NULL
    AND CAST(CREATE_TIME AS DATE) = CAST(GETDATE() AS DATE)   -- 添加根据 CREATE_TIME 查询当天的条件
    GROUP BY END_REGION 
) AS subquery;

create  view inventory_bimonth_summary as
SELECT
    current_month.current_month_count,
    current_month.current_month_presentation,
    current_month.current_month_delivery,
    current_month.current_month_storage,
    previous_month.previous_month_count,
    previous_month.previous_month_presentation,
    previous_month.previous_month_delivery,
    previous_month.previous_month_storage,
    (previous_month.previous_month_delivery + previous_month.previous_month_storage) AS p_sum,
    (current_month.current_month_delivery + current_month.current_month_storage) AS c_sum,
    current_month.current_month_presentation * 1.0 / previous_month.previous_month_presentation AS presentation_ratio,
    current_month.current_month_delivery * 1.0 / previous_month.previous_month_delivery AS delivery_ratio,
    current_month.current_month_storage * 1.0 / previous_month.previous_month_storage AS storage_ratio
FROM
    (
        SELECT
            COUNT(*) AS current_month_count,
            COUNT(DISTINCT CASE WHEN LOG_TYPE = 'HAND_ORDER' THEN related_Bill ELSE NULL END) AS current_month_presentation,
            SUM(CASE WHEN LOG_TYPE = 'SHIPPING' THEN 1 ELSE 0 END) AS current_month_delivery,
            SUM(CASE WHEN LOG_TYPE = 'RECEIVING' THEN 1 ELSE 0 END) AS current_month_storage
        FROM
            inventory_log
        WHERE
            YEAR(OCCUR_TIME) = YEAR(GETDATE()) AND MONTH(OCCUR_TIME) = MONTH(GETDATE())
    ) AS current_month,
    (
        SELECT
            COUNT(*) AS previous_month_count,
            COUNT(DISTINCT CASE WHEN LOG_TYPE = 'HAND_ORDER' THEN related_Bill ELSE NULL END) AS previous_month_presentation, 
            SUM(CASE WHEN LOG_TYPE = 'SHIPPING' THEN 1 ELSE 0 END) AS previous_month_delivery,
            SUM(CASE WHEN LOG_TYPE = 'RECEIVING' THEN 1 ELSE 0 END) AS previous_month_storage
        FROM
            inventory_log
        WHERE
            YEAR(OCCUR_TIME) = YEAR(DATEADD(MONTH, -1, GETDATE())) AND MONTH(OCCUR_TIME) = MONTH(DATEADD(MONTH, -1, GETDATE()))
    ) AS previous_month;

create  view task_current_week_summary as
SELECT COUNT(*) AS TotalCount
 FROM TASK
JOIN PICK_TICKET_DETAIL ON TASK.PICK_TICKET_DETAIL_ID = PICK_TICKET_DETAIL.ID
 JOIN PICK_TICKET ON PICK_TICKET_DETAIL.PICK_TICKET_ID = PICK_TICKET.ID
WHERE DATEPART(wk, TASK.OPERATE_TIME) = DATEPART(wk, GETDATE())
 AND YEAR(TASK.OPERATE_TIME) = YEAR(GETDATE());


create view task_current_week_summary_2 as
SELECT
     PICK_TICKET.equated_Quantity,
    SUM(CASE WHEN TASK.warehouse_Area_ID = 1763 THEN 1 ELSE 0 END) AS total_1763,
    SUM(CASE WHEN TASK.warehouse_Area_ID = 1765 THEN 1 ELSE 0 END) AS total_1765,
    SUM(CASE WHEN TASK.warehouse_Area_ID = 1773 THEN 1 ELSE 0 END) AS total_1773
FROM 
    TASK
    JOIN PICK_TICKET_DETAIL ON TASK.PICK_TICKET_DETAIL_ID = PICK_TICKET_DETAIL.ID
    JOIN PICK_TICKET ON PICK_TICKET_DETAIL.PICK_TICKET_ID = PICK_TICKET.ID
WHERE 
    DATEPART(wk, PICK_TICKET.UPDATE_TIME) = DATEPART(wk, GETDATE())
    AND YEAR(PICK_TICKET.UPDATE_TIME) = YEAR(GETDATE())
GROUP BY
 PICK_TICKET.equated_Quantity


	
	
	--------------2024-10-18
	ALTER TABLE    agvtask   add dateTime1 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime2 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime3 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime4 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime5 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime6 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime7 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime8 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime9 [datetime]  NULL;
	ALTER TABLE    agvtask   add dateTime10 [datetime]  NULL;
	
	ALTER TABLE    task   add dateBill4 [datetime]  NULL;
ALTER TABLE    task   add dateBill5 [datetime]  NULL;
ALTER TABLE    task   add dateBill6 [datetime]  NULL;
ALTER TABLE    task   add dateBill7 [datetime]  NULL;
ALTER TABLE    task   add dateBill8 [datetime]  NULL;
-----------2024-10-22
ALTER TABLE    agvtask   add  agvStartSlotCode varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    agvtask   add  agvEndSlotCode varchar(100) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    agvtask   add  agvRemarks varchar(200) COLLATE Chinese_PRC_CI_AS  NULL;
ALTER TABLE    agvtask   add  agvReturnRemarks varchar(200) COLLATE Chinese_PRC_CI_AS  NULL;

----2024-10-23
ALTER TABLE   Plant_Location    add bePriorityWave tinyint  NULL;
GO
update Plant_Location set bePriorityWave=0 where bePriorityWave is NULL;
GO

------------
order by 
CASE myColumn
        WHEN 'A' THEN 1
        WHEN 'B' THEN 2
        ELSE myColumn
    END ASC;
------------------2024-10-25
ALTER TABLE   ZONE    add beStock tinyint  NULL;
GO
update ZONE set beStock=1 where beStock is null;
GO
ALTER TABLE   ZONE    add beBill tinyint  NULL;
GO
update ZONE set beBill=1 where beBill is null;

----------20241203
ALTER TABLE   BILL_TYPE    add be_Print tinyint  NULL;
GO
update BILL_TYPE set be_Print=0 where be_Print is null;
GO

----------20241211
ALTER TABLE   RECEIVED_RECORD    add sap_Status varchar(20) COLLATE Chinese_PRC_CI_AS  NULL;
GO