Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL rewrite

Posted on 2011-09-08
7
Medium Priority
?
364 Views
Last Modified: 2012-05-12

I have the SQl query below which list the stores

Whenever i get "All city types" in the citytypename fields , i DO NOT need to list the other rows for the
particular storelocationid

Result

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      


how i will rewrite my query for this?

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

Open in new window

0
Comment
Question by:PeteEngineer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36503131
Hi PeteEngineer,

How about the code attached

HTH,

Mark.
SELECT  LL.StoreLocationID
       ,LC.[StoreID]
       ,ISNULL(LC.CityID,0) CityID
       ,ISNULL(C.CityTypeName,'All City Types') CityTypeName
       ,CASE( WHEN (C.CityTypeName IS NULL) THEN (ISNULL(LL.RegionName,'All Regions')) ELSE NULL END) RegionName
       ,CASE( WHEN (C.CityTypeName IS NULL) THEN (L.[Name]) ELSE NULL END) StoreName
       ,CASE( WHEN (C.CityTypeName IS NULL) THEN (ISNULL(C.Name,'All Cities')) ELSE NULL END) AS CityName
       ,CASE( WHEN (C.CityTypeName IS NULL) THEN (ISNULL(S.Name, 'All States')) ELSE NULL END) 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

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36503243
You can try a NOT EXISTS. Let's start with an OR in single query. Other option might be to use UNION where you select just the 'all city types' rows and then join in those other cities where there does not exist an 'all city types' row for that store location ID.

Let me know how this works:
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
   )
)

Open in new window

0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 36507804
:OnALearning…:

I am getting this error

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'WHEN'.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 8

Author Comment

by:PeteEngineer
ID: 36507813
mwvisa1: I am getting the following error :


Msg 156, Level 15, State 1, Line 23
Incorrect syntax near the keyword 'ON'.
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 36507816
I have remove the ON statement,so the query is working .....


but there is no change in the query results i posted above
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36507821
Sorry, I started to type this with [City] table first, then thought it may not be needed and in my shifting around I did not get rid of an ON clause. Just delete it.

i.e., run like this:

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
	  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
   )
)

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36507827
If that is the case, then there is probably something in the NOT EXISTS joins that is not right. The basics is you need to pick rows that qualify as 'All City Types' or choose those rows where there doesn't exist another row over whatever criteria you set that doesn't qualify as an 'All City Types' row. Maybe it can't be linked by StoreLocationID?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question