SQL--工作中遇到的
--递归查询产品分类
WITH cte AS ( SELECT * FROM syn_Categories WHERE id = $CategoryID$ UNION ALL SELECT syn_Categories.* FROM cte aaa , syn_Categories WHERE aaa.id = syn_Categories.parentId AND aaa.id != 0 ) SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY a.SKU ) AS ROWID , a.* , ISNULL(d.NetStock, 0) AS NetStock, c.SupplierCode FROM MVPProduct a LEFT JOIN SupplyCommitment b ON a.SKU = b.SKU LEFT JOIN (SELECT SKU, ID, NetStock FROM MVPStockUp WHERE ID IN ( SELECT MAX(ID) FROM MVPStockUp GROUP BY SKU)) d ON a.SKU = d.SKU AND ( $CategoryID$ = 0 OR ( CategoryID IN ( SELECT ID FROM cte WHERE ID NOT IN ( SELECT DISTINCT ( ParentId ) FROM cte )) )) LEFT JOIN SupplierSumMerchandise c ON b.SupplierID = c.SupplierID WHERE a.Status = 1
每天学一点,每天积累一天,进步就不止一点点!PS:好记性不如烂笔头,学会总结,学会思考~~~
----要飞翔,必须靠自己!