我有3张桌子,如下所示:
条码组图
类别大师
产品主:
从这3个表中,我进行了以下查询:
select bgm.BarcodeItem,
cm.CategoryID,cm.CategoryName,pm.ProductName,pm.ProductID,bgm.EffectFrom,bgm.Groups
from BarcodeGroupMap bgm,CategoryMaster cm,ProductMaster pm where
bgm.ProductID=pm.ProductID and bgm.categoryID=cm.CategoryID
该查询结果如下:
现在,正如我们在查询结果中看到的,条形码在重复,
我只是想表明一个条形码一次,根据其最新Createddate从Barcodegroupmap表。
为此,我提出以下查询:
select bgm.BarcodeItem,
cm.CategoryID,cm.CategoryName,pm.ProductName,pm.ProductID,bgm.EffectFrom,bgm.Groups
from BarcodeGroupMap bgm,CategoryMaster cm,ProductMaster pm where
bgm.ProductID=pm.ProductID and bgm.categoryID=cm.CategoryID and
bgm.BarcodeItem= select BarcodeItem from BarcodeGroupMap bm1 where
CreatedDate= (select top 1 CreatedDate from BarcodeGroupMap bm2
)order by bm1.BarcodeItem
但是它没有给我正确的结果。
请帮我。
我只想根据Barcodegroupmap中其最新的createddate一次显示一个条形码项目。
我没有实际尝试此查询的方法,因为我没有相关的表和数据,但这应该可以帮助您入门:
SELECT BarcodeItem, temp.CategoryID, cm.CategoryName, temp.ProductID, pm.ProductName, EffectFrom FROM (
SELECT BarcodeItem, CategoryID, ProductID, CONVERT(date, EffectFrom) as EffectFrom,
RANK() OVER (PARTITION BY BarcodeItem ORDER BY EffectFrom DESC) dest_rank
FROM BarcodeGroupMap
) temp
inner join CategoryMaster cm on cm.CategoryID = temp.CategoryID
inner join ProductMaster pm on pm.ProductID = temp.ProductID
where temp.dest_rank = 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句