是在语句不会返回我行空

问题描述 投票:-1回答:1

我使用的是复杂的查询。我需要总是返回我的行,即使它没有发现任何东西。

SELECT 
  a.InventoryItemID,
  a.Name,
  a.RetailPrice,
  b.MainGroupItemCode,
  b.MainGroupItemID,
  c.VatValue,
  a.Code,
  a.Weight,
  b.MainGroupItemName,
  a.RetailPrice2,
  a.FreePrice,
  case when isnull(e.IsActive,0)=1 and isnull(d.price,0)!=0 then d.Price else RetailPrice End  as CustomPrice 
from InventoryMaster a 
join InventoryMainGroupItems b on a.MainGroupItemID=b.MainGroupItemID 
join VatCodes c on b.VatCodeID=c.VatCodeID 
join InventoryPrices d on d.InventoryItemID=a.InventoryItemID 
join InventoryCatalog e on e.CatalogID=d.CatalogID 
where a.InventoryItemID=2 and ISNULL(e.catalogID,1)=3

问题是在最后一行ISNULL(e.catalogID,1)= 3。在我的表不与3号。所以,它不返回我什么存在采用catalogId,但采用catalogId与1号。我已经设置,如果是空回到我1,遗憾的是我没有得到任何一行从我的背部查询。我怎样才能解决这个问题 ?

我的问题已经解决了,我只是想增加一个连接表与一个wheere条件isnide

SELECT * 
from 
( 
SELECT t1.ID, 
t1.Name, 

COALESCE(t2.price,t1.Price) AS price , 
Row_number() OVER(partition BY t1.ID ORDER BY t1.ID) rn 
FROM InventoryMaster t1 
LEFT JOIN inventoryprices t2 
ON t1.ID=t2.ID 

LEFT join InventoryCatalog t3 
ON t3.ID=t2.ID and t3.ID=2 

where t1.ID=2 
) t 
WHERE t.rn=1

它返回我总是从第一个表清查retailprice

sql-server sql-server-2008-r2
1个回答
0
投票

加入3周附近的cols选择的开始

  d.InventoryItemID,
  d.CatalogID,
  e.CatalogID

然后从那里取出并ISNULL,并运行看看你。

这可能是因为

join InventoryCatalog e
needs 
ON d.CatalogID=ISNULL(e.catalogID,1)
© www.soinside.com 2019 - 2024. All rights reserved.