Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Join of sql query

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
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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
Avatar of Peter Nordberg

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect! Thanks!