2011-09-09 53 views
0

我有SQL查詢低於列出商店SQL存在不工作

每當我得到「所有城市類型」中的citytypename領域,我並不需要列出其他行的 特定storelocationid

結果

71 8 0 All City Types West Dhanalakshmi store  All Cities All States 
71 8 1 Tier 2   West Dhanalakshmi store  Bangalore  Karnataka 
71 8 2 Tier 2   West Dhanalakshmi store  Ahmedabad  Gujarat  

如何,我將重寫我的查詢呢?

SELECT LL.StoreLocationID 
     ,LC.[StoreID] 
     ,ISNULL(LC.CityID,0) CityID 
     ,ISNULL(C.CityTypeName,'All City Types') CityTypeName 
     ,ISNULL(LL.RegionName,'All Regions') RegionName 
     ,L.[Name] StoreName 
     ,ISNULL(C.Name,'All Cities') AS CityName 
     ,ISNULL(S.Name, 'All States') AS StateName 
FROM [StoreCity] LC 
    INNER JOIN [Store] L ON L.[StoreID] = LC.[StoreID] 
    INNER JOIN [StoreLocation] LL ON LL.StoreID=LC.StoreID 
    LEFT OUTER JOIN [City] C ON LC.CityID = C.CityID 
    LEFT OUTER JOIN [State] S ON C.StateID = S.StateID 
WHERE StoreLocationID = 71 
AND (
    /* current row is 'All City Types' */ 
    C.CityTypeName IS NULL 
    OR 
    /* current row is not 'All City Types', 
     but there does not exist another row that is. */ 
    NOT EXISTS (
     SELECT 1 
     FROM [StoreLocation] LL2 ON LL2 
     INNER JOIN [StoreCity] LC2 ON LC2.[StoreID] = LL2.[StoreID] 
     INNER JOIN [City] C2 ON C2.[CityID] = LC2.[CityID] 
     WHERE LL2.StoreLocationID = LL.StoreLocationID 
     AND C2.CityTypeName IS NULL 
    ) 
) 
+0

從你的問題,我不能確定哪些表包含,或者基數關係,它們之間是什麼(除了城市和州當然)每家商店都有很多商店的大賣場?或者是多個地點的一家商店?或兩者?什麼是關係結構? –

+0

這些'NULL'存儲在'CityTypeName'中嗎?還是它們是從左邊的外連接生成的? –

+0

你能解釋WHERE子句中子查詢的用途嗎?它過濾出什麼標準? –

回答

0

我想你正在嘗試查找DISTINCT並可能找到相應的GROUP BY。

事情是這樣的:

SELECT DISTINCT ISNULL(C.CityTypeName,'All City Types') CityTypeName FROM ... 
0
SELECT final .StoreLocationID 
     ,final .[StoreID] 
     ,final .CityID 
     ,final .CityTypeName 
     ,final .RegionName 
     ,final .StoreName 
     ,final .CityName 
     ,final .StateName from (SELECT LL.StoreLocationID 
     ,LC.[StoreID] 
     ,ISNULL(LC.CityID,0) CityID 
     ,ISNULL(C.CityTypeName,'All City Types') CityTypeName 
     ,ISNULL(LL.RegionName,'All Regions') RegionName 
     ,L.[Name] StoreName 
     ,ISNULL(C.Name,'All Cities') AS CityName 
     ,ISNULL(S.Name, 'All States') AS StateName FROM [StoreCity] LC 
    INNER JOIN [Store] L ON L.[StoreID] = LC.[StoreID] 
    INNER JOIN [StoreLocation] LL ON LL.StoreID=LC.StoreID 
    LEFT OUTER JOIN [City] C ON LC.CityID = C.CityID 
    LEFT OUTER JOIN [State] S ON C.StateID = S.StateID WHERE StoreLocationID = 71 AND ( /* current row is 'All City Types' */ C.CityTypeName IS NULL OR  /* current row is not 'All City Types', 
     but there does not exist another row that is. */ NOT EXISTS (
     SELECT 1 
     FROM [StoreLocation] LL2 ON LL2 
     INNER JOIN [StoreCity] LC2 ON LC2.[StoreID] = LL2.[StoreID] 
     INNER JOIN [City] C2 ON C2.[CityID] = LC2.[CityID] 
     WHERE LL2.StoreLocationID = LL.StoreLocationID 
     AND C2.CityTypeName IS NULL )) ORDER BY FIELD("All City Types", CityTypeName) DESC) as final group by final.StoreLocationID 

試試這個,沒有測試...