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

x
?
Solved

Join of sql query

Posted on 2012-03-10
4
Medium Priority
?
317 Views
Last Modified: 2012-03-10
Hi,

I have this table looking like this:

stores
storeID int
storeName nvarchar
regionID int
storeAddress nvarchar
storePhone nvarchar
storeTypeID

The data stored in the database table could look like this:
storeID      storeName      regionID      storeAddress              storePhone  storeTypeID
1               VD                         -1              Stensätravägen 3B      08554455     5
110               Region Öst          1              Olavägen 3              08667894     2
111               Kolargatan          110      Kolargatan 2              08574123     1

I would like the output of my query to look like this:
storeID    storeName  Region          storeAddress  storePhone
111          Kolargatan   Region Öst   Kolargatan 2    08574123

Now the problem is how I can join the columns storeID and regionID so that I can show the region for the particular store. Also, if regionID is -1 as in  the case of the store with ID 1 then the Region column should be blank.

How can I achieve this?

Thanks for help!

Peter
0
Comment
Question by:peternordberg
[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
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37704437
I think you need to put a left join in there, but I wonder about the data sample ...
you seem to have mixed store names and region names?
please clarify
0
 

Author Comment

by:peternordberg
ID: 37704458
There are different storeTypes. A usual store have storeType = 1, a region storeTypeID = 2 and so on.

So for the example I would like the output to show both the storeID and the storeName for the store as well as the storeName for the regionID for that particular store.

Hope that claryfies. The thing is that both regions and usual stores are stored in the same table with different storetypes.

Peter
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 37704516
so, you want to start with this:
select s.StoreID, s.StoreName, r.StoreName RegionName
     , s.storeAddress , s.storePhone
   from stores s
   left join stores r
      on r.storeID = s.RegionID
    and r.storeTypeID = 2
  where s.storeTypeID = 1

Open in new window

0
 

Author Closing Comment

by:peternordberg
ID: 37704696
Perfect! Thanks!
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

670 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