Solved

Join of sql query

Posted on 2012-03-10
4
307 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 500 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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