insert into core_spare_part(site,bu_no,part_no,part_desc,serial_number,status,arrival_time,remark,create_time,create_by)
values(#{site},#{buNo},#{partNo},#{partDesc},#{serialNumber},#{status},#{arrivalTime},#{remark},GETDATE(),#{createBy})
select count(1)
from core_spare_part
where site = #{site}
and bu_no = #{buNo}
and serial_number = #{serialNumber}
delete from core_spare_part
where site = #{site} and bu_no = #{buNo} and part_no = #{partNo} and serial_number = #{serialNumber}
SELECT
a.citem_code,
min(A.id) id,
A.site,
min(A.warehouse_id) AS warehouse_id,
A.part_no,min(A.batch_no) as batch_no,
min(A.location_id) AS location_id,
sum(isnull(A.in_qty, 0)) AS inQty,
sum(isnull(A.out_qty, 0)) AS outQty,
sum(isnull(A.qty_on_hand, 0)) AS qtyOnHand,
B.part_description,
min(B.spec) AS spec,
B.min_stock,
B.max_stock,
min(A.partner_id) AS partner_id,
min(A.first_in_date) AS first_in_date,
B.average_price,
B.umid,
(sum(isnull(A.qty_on_hand, 0)) * B.average_price) as totalCost,
dbo.get_bu_desc(A.site, A.citem_code) buDesc,
B.est_use_qty
from inventory_stock A
left join part_spare B on A.site = B.site and A.part_no = B.part_no and a.citem_code = b.bu_no
A.site in (select site from eam_access_site where username = #{query.username})
and (A.site + '-' + A.citem_code) in (select * from dbo.query_bu(#{query.username}))
and A.batch_no is not null and b.part_no is not null
AND dbo.get_bu_desc (a.site, a.citem_code) = #{query.buDesc}
and B.active = #{query.active}
and A.batch_no like '%'+#{query.batchNo}+'%'
and A.warehouse_id like '%'+#{query.warehouseId}+'%'
and A.location_id like '%'+#{query.locationId}+'%'
and A.part_no like '%'+#{query.partNo}+'%'
and B.part_description like '%'+#{query.partDescription}+'%'
and B.spec like '%'+#{query.spec}+'%'
and A.first_in_date >= #{query.startFirstInDate}
and A.first_in_date #{query.endFirstInDate}
and A.qty_on_hand >= #{query.startQtyOnHand}
and A.qty_on_hand #{query.endQtyOnHand}
and A.qty_on_hand ]]> B.min_stock
and A.qty_on_hand B.min_stock
and A.qty_on_hand = 0
and A.qty_on_hand != 0
GROUP BY
A.site, A.part_no, B.part_description, A.batch_no, b.min_stock, b.max_stock, B.average_price, B.umid, a.citem_code,B.est_use_qty
A.site, A.part_no, B.part_description, b.min_stock, b.max_stock, B.average_price, B.umid, a.citem_code,B.est_use_qty
update inventory_stock set qty_of_po = isnull(qty_of_po,0) + #{qtyOfPo} where id = #{id}
update inventory_stock set qty_on_hand = isnull(qty_on_hand,0) + #{qtyOfPo}, qty_of_po = isnull(qty_of_po,0) - #{qtyOfPo} where id = #{id}
update inventory_stock set qty_of_po = isnull(qty_of_po,0) - #{qtyOfPo} where id = #{id}
update core_spare_part
set status = #{status},
arrival_time = #{arrivalTime},
remark = #{remark},
update_time = getdate(),
update_by = #{updateBy}
where site = #{site} and bu_no = #{buNo} and part_no = #{partNo} and serial_number = #{serialNumber}
SELECT
A.citem_code,
min(A.id) as id,
A.site,
min(A.warehouse_id) AS warehouse_id,
A.part_no,min(A.batch_no) as batch_no,
min(A.location_id) AS location_id,
sum(isnull( A.in_qty , 0 )) AS inQty,
sum(isnull( A.out_qty , 0 )) AS outQty,
sum(isnull( A.qty_on_hand , 0 )) AS qtyOnHand,
B.part_description,
min(B.spec) AS spec,
B.min_stock,
B.max_stock,
min(A.partner_id) AS partner_id,
min(A.first_in_date) AS first_in_date,
B.average_price,
B.umid,
sum(isnull(A.qty_of_po,0)) as qty_of_po,
(sum(isnull( A.qty_on_hand , 0 )) * B.average_price) as totalCost,
dbo.get_bu_desc( A.site,A.citem_code) buDesc
from inventory_stock A
left join part_spare B on A.site = B.site and A.part_no = B.part_no and a.citem_code = b.bu_no
A.site in (select site from eam_access_site where username = #{query.username})
and (A.site + '-' + A.citem_code) in (select * from dbo.query_bu(#{query.username}))
and A.batch_no is not null and b.part_no is not null
AND dbo.get_bu_desc (a.site, a.citem_code) = #{query.buDesc}
and B.active = #{query.active}
and a.citem_code = #{query.buNo}
and A.batch_no like '%'+#{query.batchNo}+'%'
and A.warehouse_id like '%'+#{query.warehouseId}+'%'
and A.location_id like '%'+#{query.locationId}+'%'
and A.part_no like '%'+#{query.partNo}+'%'
and B.part_description like '%'+#{query.partDescription}+'%'
and B.spec like '%'+#{query.spec}+'%'
and A.first_in_date >= #{query.startFirstInDate}
and A.first_in_date #{query.endFirstInDate}
and A.qty_on_hand >= #{query.startQtyOnHand}
and A.qty_on_hand #{query.endQtyOnHand}
and A.qty_on_hand ]]> B.min_stock
and A.qty_on_hand = 0
and A.qty_on_hand != 0
GROUP BY
A.site, A.part_no, B.part_description, A.batch_no, b.min_stock, b.max_stock, B.average_price, B.umid,a.citem_code
A.site, A.part_no, B.part_description, b.min_stock, b.max_stock, B.average_price, B.umid,a.citem_code
having sum(A.qty_on_hand) B.min_stock
having sum(A.qty_on_hand) + sum(isnull(A.qty_of_po,0.0)) B.min_stock
WITH ranked_records AS (
SELECT
D.OrderNo, ewc.site, ewc.bu_no, ewc.partno, ps.part_description,
ewc.oldSerialNo, ewc.newSerialNo, ewc.created_date, ps.spec, ps.umid,
ps.remark, ewc.created_by, d.fault_reason, b.resource_id,DATEDIFF(HOUR, CAST(ewc.created_date AS DATETIME), CAST(GETDATE() AS DATETIME)) as workTime,
dbo.eam_Get_eamResourceDesc(d.Site, d.ObjectID) AS resourceDesc,ps.location_id,ps.est_use_qty,v.SupplierName,
CASE WHEN DATEADD(year, ps.est_use_qty, ewc.created_date)> GETDATE() THEN '否' ELSE '是' END AS lifespan,
ROW_NUMBER() OVER (PARTITION BY ewc.partNo ORDER BY ewc.created_date DESC) AS rn
FROM eam_workOrder_coreComponents ewc
LEFT JOIN part_spare ps ON ewc.site = ps.site AND ewc.bu_no = ps.bu_no AND ewc.partNo = ps.part_no
LEFT JOIN eam_workOrder d ON ewc.site = d.Site AND ewc.bu_no = d.bu_no AND ewc.OrderNo = d.OrderNo
LEFT JOIN resource B ON ewc.site = b.site AND d.objectid = b.resource_id
LEFT JOIN view_Supplier v ON ps.site = V.Site AND ps.supplier_id = V.SupplierID
WHERE ps.coreFlag = 'Y'
and ewc.site = #{query.site}
and ewc.bu_no = #{query.buNo}
and ewc.partNo = #{query.partNo}
and ps.part_description like '%'+ #{query.partDescription} +'%'
)
SELECT OrderNo, site, bu_no, partno, part_description, oldSerialNo, newSerialNo,workTime,
created_date, spec, umid, remark, created_by, fault_reason, resource_id, resourceDesc,location_id,est_use_qty,lifespan,SupplierName
FROM ranked_records
WHERE rn = 1;
WITH ranked_records AS (
SELECT
D.OrderNo, ewc.site, ewc.bu_no, ewc.partno, ps.part_description,
ewc.oldSerialNo, ewc.newSerialNo, ewc.created_date, ps.spec, ps.umid,
ps.remark, ewc.created_by, d.fault_reason, b.resource_id,
dbo.eam_Get_eamResourceDesc(d.Site, d.ObjectID) AS resourceDesc,ps.location_id,ps.est_use_qty,v.SupplierName,
CASE WHEN DATEADD(year, ps.est_use_qty, ewc.created_date)> GETDATE() THEN '否' ELSE '是' END AS lifespan,
ROW_NUMBER() OVER (PARTITION BY ewc.partNo ORDER BY ewc.created_date DESC) AS rn
FROM eam_workOrder_coreComponents ewc
LEFT JOIN part_spare ps ON ewc.site = ps.site AND ewc.bu_no = ps.bu_no AND ewc.partNo = ps.part_no
LEFT JOIN eam_workOrder d ON ewc.site = d.Site AND ewc.bu_no = d.bu_no AND ewc.OrderNo = d.OrderNo
LEFT JOIN resource B ON ewc.site = b.site AND d.objectid = b.resource_id
LEFT JOIN view_Supplier v ON ps.site = V.Site AND ps.supplier_id = V.SupplierID
WHERE ps.coreFlag = 'Y'
and ewc.site = #{query.site}
and ewc.bu_no = #{query.buNo}
and ewc.partNo = #{query.partNo}
and ps.part_description like '%'+ #{query.partDescription} +'%'
)
SELECT
OrderNo, site, bu_no, partno, part_description, oldSerialNo, newSerialNo,
created_date, spec, umid, remark, created_by, fault_reason, resource_id, resourceDesc,
location_id, est_use_qty, lifespan, SupplierName
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY site,bu_no,partno,oldSerialNo ORDER BY created_date ASC) AS rn2
FROM ranked_records
) AS subquery
WHERE rn > 1 and rn2 = 1;
WITH SerialStatus AS (
SELECT
site,
bu_no,
OrderNo,
partno,
oldSerialNo AS serialNo,
'下机' AS action,
created_by,
created_date
FROM eam_workOrder_coreComponents
WHERE oldSerialNo IS NOT NULL and site = #{query.site} AND bu_no = #{query.buNo} AND partNo = #{query.partNo}
UNION ALL
SELECT
site,
bu_no,
OrderNo,
partno,
newSerialNo AS serialNo,
'上机' AS action,
created_by,
created_date
FROM eam_workOrder_coreComponents
WHERE newSerialNo IS NOT NULL and site = #{query.site} AND bu_no = #{query.buNo} AND partNo = #{query.partNo}
),
RankedSerials AS (
SELECT
site,
bu_no,
OrderNo,
partno,
serialNo,
action,
created_by,
created_date,
ROW_NUMBER() OVER (PARTITION BY partno, serialNo,SerialStatus.action ORDER BY created_date) AS rn
FROM SerialStatus
),
TimeDifferences AS (
SELECT
site,
bu_no,
OrderNo,
partno,
serialNo,
action,
created_by,
created_date,
CASE
WHEN action = '上机' THEN '第' + CAST(rn AS VARCHAR) + '次上机'
WHEN action = '下机' THEN '第' + CAST(rn AS VARCHAR) + '次下机'
END AS status,
DATEDIFF(DAY,
LAG(created_date) OVER (PARTITION BY partno, serialNo ORDER BY created_date),
created_date
) AS time_diff_months
FROM RankedSerials
)
SELECT
ewc.site,
ewc.bu_no,
ewc.OrderNo,
ewc.partno,
ps.part_description,
ps.est_use_qty,
ewc.serialNo,
ewc.action,
ewc.created_by,
ewc.created_date,
ewc.status as onOffStatus,
edf.FeedBackDesc,
ewc.time_diff_months
FROM TimeDifferences ewc
LEFT JOIN part_spare ps ON ewc.site = ps.site AND ewc.bu_no = ps.bu_no AND ewc.partNo = ps.part_no
LEFT JOIN eam_workOrder ew ON ewc.site = ew.site AND ewc.bu_no = ew.bu_no AND ewc.OrderNo = ew.OrderNo
LEFT JOIN eam_defect_feedBack edf on ew.site = edf.site and ew.PlanID = edf.feedBackID and ew.bu_no = edf.bu_no
WHERE ps.coreFlag = 'Y'
and ewc.serialNo = #{query.serialNo}
ORDER BY partno, serialNo, created_date
select dbo.get_bu_desc(csp.site,csp.bu_no) as bu,
csp.*,
ps.location_id
from core_spare_part csp
left join part_spare ps on csp.site = ps.site and csp.bu_no = ps.bu_no and csp.part_no = ps.part_no
and csp.site = #{site}
and csp.bu_no = #{buNo}
and csp.part_no like '%' + #{partNo} + '%'
and csp.part_desc like '%' + #{partDesc} + '%'
and csp.serial_number like '%' + #{serialNumber} + '%'
and csp.status = #{status}