22,209
社区成员
发帖
与我相关
我的任务
分享
IF OBJECT_ID('dbo.EP_commodity_search') IS NOT NULL
DROP view dbo.EP_commodity_search
GO
CREATE VIEW [dbo].[EP_commodity_search] AS
select commodity.id as commodityid, commodity.name, classify.name as classify, series.name as series, category.name as category, spec.name as spec, color.name as color,
quantity = isnull(entry.quantity,0) - isnull(appear.quantity,0),
location = isnull(location.name,null)
from
(select id,name,classifyid,seriesid,categoryid,specid,colorid from EP_commodity)commodity
left join (select id,name from EP_commodity_classify)classify on classify.id = commodity.classifyid
left join (select id,name from EP_commodity_series)series on series.id = commodity.seriesid
left join (select id,name from EP_commodity_category)category on category.id = commodity.specid
left join (select id,name from EP_commodity_spec)spec on spec.id = commodity.specid
left join (select id,parentid,name from EP_commodity_color)color on color.id = commodity.colorid
left join (select commodityid,locationid,sum(quantity) as quantity from EP_commodity_entry group by commodityid,locationid)entry on entry.commodityid = commodity.id
left join (select commodityid,locationid,sum(quantity) as quantity from EP_commodity_appear group by commodityid,locationid)appear on appear.commodityid = commodity.id
left join (select commodityid,locationid from EP_commodity_location)commodityLocation on commodityLocation.commodityid = commodity.id
left join (select id,parentid,name from EP_location)location on location.id = commodityLocation.locationid
where isnull(entry.quantity,0) - isnull(appear.quantity,0) > 0
GO
select id,name,classifyid,seriesid,categoryid,specid,colorid
FROM EP_commodity AS commodity
LEFT JOIN EP_commodity_classify AS classify ON classify.id = commodity.classifyid
LEFT JOIN EP_commodity_series AS series on series.id = commodity.seriesid
LEFT JOIN EP_commodity_category AS category on category.id = commodity.specid
LEFT JOIN EP_commodity_spec AS spec on spec.id = commodity.specid
LEFT JOIN EP_commodity_color AS color on color.id = commodity.colorid
LEFT JOIN (
SELECT entry.commodityid,entry.locationid,entry.quantity-isnull(appear,quantity,0) AS quantity FROM (
SELECT commodityid,locationid,sum(quantity) as quantity from EP_commodity_entry group by commodityid,locationid
) AS entry
LEFT JOIN (
SELECT commodityid,locationid,sum(quantity) as quantity from EP_commodity_appear group by commodityid,locationid
) AS appear on appear.commodityid = entry.commodityid
) AS q
left join EP_location AS location on location.id = q.locationid
WHERE q.quantity>0