2013-11-14 2 views
0

у меня есть 3 таблицы следующим образом:Не группировать по последним датам

BarcodeGroupMap

enter image description here

CategoryMaster

enter image description here

Prod uctMaster:

enter image description here

Из этих 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 

результатов Этот запрос в следующем:

enter image description here

Теперь, как мы видим, в результате запроса штрих-код повторяется,

Я просто хотел показать один штрих-код один раз, согласно его последнему Создано от 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 

Но это не дает мне правильный результат.

Пожалуйста, помогите мне.

Я просто хочу отобразить один штрих-код только один раз в соответствии с его latested createddate в Barcodegroupmap.

ответ

1

У меня нет никакого способа на самом деле пытается этот вопрос, потому что у меня нет соответствующих таблиц и данных, но это должно вам начать работу:

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 
1

Вы можете присоединиться к таблицам нет?

SELECT bgm.BarcodeItem, 
cm.CategoryID,cm.CategoryName,pm.ProductName,pm.ProductID,bgm.EffectFrom,bgm.Groups 
from BarcodeGroupMap bgm 
INNER JOIN CategoryMaster cm on bgm.categoryID=cm.CategoryID 
INNER JOIN ProductMaster pm ON bgm.ProductID=pm.ProductID 
WHERE bgm.CreatedDate = (select top 1 CreatedDate from BarcodeGroupMap bm2 
WHERE bgm.BarcodeItem = bm2.BarcodeItem ORDER BY CreatedDate desc) order by bgm.BarcodeItem 

Если вы не хотите, чтобы присоединиться к

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 
where bgm.CreatedDate= (select top 1 CreatedDate from BarcodeGroupMap bm2 
WHERE bgm.BarcodeItem = bm2.BarcodeItem ORDER BY CreatedDate desc) order by bm1.BarcodeItem 
Смежные вопросы