Solved

Join of sql query

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 setup several different housekeeping processes for a SQL Server.
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.

737 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