Solved

Join of sql query

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RAISERROR WITH NOWAIT 2 18
how to restore or keep sql2000  backups useful... 2 18
Help With SQL Query 9 33
SQL Recursion 6 20
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

828 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