select c.PurchaseNo as DocNo,c.PurchaseDate as DocDate,'' as 'User' ,'采购入库' as 'OperationType',c.Remark,c.Creator,c.CreateDate from It_Equip a join It_PurchaseInList b on a.EquipId=b.EquipId join It_PurchaseIn c on b.PurchaseId=c.PurchaseId where a.FixedNo='CCID-BB-BJB-23058' union select c.ReceiveNo as DocNo,c.ReceiveDate as DocDate,c.Custodian as 'User' ,'资产领用' as 'OperationType',c.Remark,c.Creator,c.CreateDate from It_Equip a join It_GoodsReceiveList b on a.EquipId=b.EquipId join It_GoodsReceive c on b.ReceiveId=c.ReceiveId where a.FixedNo='CCID-BB-BJB-23058' union select c.BackNo as DocNo,c.BackDate as DocDate,c.Applicant as 'User','资产退库' as 'OperationType',c.Remark,c.Creator,c.CreateDate from It_Equip a join It_GoodsBackList b on a.EquipId=b.EquipId join It_GoodsBack c on b.BackId=c.BackId where a.FixedNo='CCID-BB-BJB-23058' union select c.ScrapNo as DocNo,c.ScrapDate as DocDate,c.Applicant as 'User', '资产报废' as 'OperationType',c.Remark,c.Creator,c.CreateDate from It_Equip a join It_GoodsScrapList b on a.EquipId=b.EquipId join It_GoodsScrap c on b.ScrapId=c.ScrapId where a.FixedNo='CCID-BB-BJB-23058' union select b.ChangeNo as DocNo,b.RegDate as DocDate,b.CustodianAfter as 'User','资产变更' as 'OperationType',b.Remark,b.Creator,b.CreateDate from It_Equip a join It_GoodsChange b on a.EquipId=b.EquipId where a.FixedNo='CCID-BB-BJB-23058' 优化sql语句
时间: 2024-04-24 12:22:31 浏览: 169
可以考虑使用UNION ALL代替UNION,因为UNION会去重,而UNION ALL不会去重,从而减少了查询的开销。同时,可以使用子查询来减少重复的代码。优化后的SQL语句如下:
SELECT DocNo, DocDate, User, OperationType, Remark, Creator, CreateDate
FROM (
SELECT c.PurchaseNo AS DocNo, c.PurchaseDate AS DocDate, '' AS User, '采购入库' AS OperationType, c.Remark, c.Creator, c.CreateDate
FROM It_Equip a
JOIN It_PurchaseInList b ON a.EquipId = b.EquipId
JOIN It_PurchaseIn c ON b.PurchaseId = c.PurchaseId
WHERE a.FixedNo = 'CCID-BB-BJB-23058'
UNION ALL
SELECT c.ReceiveNo AS DocNo, c.ReceiveDate AS DocDate, c.Custodian AS User, '资产领用' AS OperationType, c.Remark, c.Creator, c.CreateDate
FROM It_Equip a
JOIN It_GoodsReceiveList b ON a.EquipId = b.EquipId
JOIN It_GoodsReceive c ON b.ReceiveId = c.ReceiveId
WHERE a.FixedNo = 'CCID-BB-BJB-23058'
UNION ALL
SELECT c.BackNo AS DocNo, c.BackDate AS DocDate, c.Applicant AS User, '资产退库' AS OperationType, c.Remark, c.Creator, c.CreateDate
FROM It_Equip a
JOIN It_GoodsBackList b ON a.EquipId = b.EquipId
JOIN It_GoodsBack c ON b.BackId = c.BackId
WHERE a.FixedNo = 'CCID-BB-BJB-23058'
UNION ALL
SELECT c.ScrapNo AS DocNo, c.ScrapDate AS DocDate, c.Applicant AS User, '资产报废' AS OperationType, c.Remark, c.Creator, c.CreateDate
FROM It_Equip a
JOIN It_GoodsScrapList b ON a.EquipId = b.EquipId
JOIN It_GoodsScrap c ON b.ScrapId = c.ScrapId
WHERE a.FixedNo = 'CCID-BB-BJB-23058'
UNION ALL
SELECT b.ChangeNo AS DocNo, b.RegDate AS DocDate, b.CustodianAfter AS User, '资产变更' AS OperationType, b.Remark, b.Creator, b.CreateDate
FROM It_Equip a
JOIN It_GoodsChange b ON a.EquipId = b.EquipId
WHERE a.FixedNo = 'CCID-BB-BJB-23058'
) AS t
ORDER BY DocDate DESC, DocNo DESC
阅读全文