Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
flipfyre
Starting Member
26 Posts |
Posted - 2011-09-14 : 12:04:44
|
I have a problem in which I seem to be missing a key part. I have 2 tables, both with a column called equipment_uid.Table 1 (called equipment) contains a description of all the equipment:equipment_uid | equipment_desc1 | trucks2 | buses3 | cars4 | trains5 | scooters6 | bicycles7 | skateboardsTable 2 (called location) contains the the equipment and the locationslocation | equipment_uidKY | 1KY | 3KY | 4KY | 7TN | 2TN | 3TN | 6FL | 1FL | 2FL | 4Therefore, KY has trucks, cars, trains, skateboards.I need to combine the equipment and location table with a new column (called available) that will display a Yes or No if the equipment is available. I also want to limit this for KY. Like so:equipment_desc | location | availabletrucks | KY | Yesbuses | KY | Nocars | KY | Yestrains | KY | Yesscooters | KY | Nobicycles | KY | Noskateboards | KY | YesI've tried the following code: select e.equipment_desc, l.location, available = case when (e.equipment_uid = l.equipment_uid) then ‘Yes’ else ‘No’ endfrom equipment e, location lwhere location = ‘KY’ but it is looping through every item in the equipment table like so:equipment_desc | location | availabletrucks | KY | Yesbuses | KY | Nocars | KY | Notrains | KY | Noscooters | KY | Nobicycles | KY | Noskateboards | KY | Notrucks | KY | Nobuses | KY | Nocars | KY | Yestrains | KY | Noscooters | KY | Nobicycles | KY | Noskateboards | KY | Notrucks | KY | Nobuses | KY | Nocars | KY | Notrains | KY | Yesscooters | KY | Nobicycles | KY | Noskateboards | KY | Notrucks | KY | Nobuses | KY | Nocars | KY | Notrains | KY | Noscooters | KY | Nobicycles | KY | Noskateboards | KY | YesI've tried using left joins, but it doesn't work right. A little help please? |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-09-14 : 12:17:40
|
| select e.equipment_desc, l.location,[available] = case when l.equipment_uid is not null then 'Yes' else 'No' endfrom equipment eleft join location l on e.equipment_uid = l.equipment_uidwhere location = 'KY'JimEveryday I learn something that somebody else already knew |
 |
|
|
flipfyre
Starting Member
26 Posts |
Posted - 2011-09-14 : 14:38:09
|
Jim,Thanks for the post. My database actually doesn't have any null values for the l.equipment_uid column. It was designed so that it only posts the equipment a location has.One of the left joins I did try is similiar to yours, with the case statement like so:select e.equipment_desc, l.location,available = case when (e.equipment_uid = l.equipment_uid) then 'Yes' else 'No' endfrom equipment eleft outer join location l on e.equipment_uid = l.equipment_uidwhere location = 'KY' However, it only produces a partial return of the data that is a match between the 2 tables, like so:equipment_desc | location | availabletrucks | KY | Yescars | KY | Yestrains | KY | Yesskateboards | KY | YesIt needs to look like this:equipment_desc | location | availabletrucks | KY | Yesbuses | KY | Nocars | KY | Yestrains | KY | Yesscooters | KY | Nobicycles | KY | Noskateboards | KY | YesMore opinions/thoughts are much appreciated. |
 |
|
|
flipfyre
Starting Member
26 Posts |
Posted - 2011-09-15 : 14:05:47
|
| Any other ideas guys? |
 |
|
|
flipfyre
Starting Member
26 Posts |
Posted - 2011-09-15 : 17:06:56
|
Figured it out guys.The problem was with the 'where' clause in the statement.select e.equipment_desc, l.location,available = case when (e.equipment_uid = l.equipment_uid) then 'Yes' else 'No' endfrom equipment eleft outer join location l on e.equipment_uid = l.equipment_uidwhere location = 'KY' Apparently, having the 'where' clause at the end of a join forces the join into an inner join - thereby limiting the data. Replacing the 'where' with an 'and' produces anticipated results.select e.equipment_desc, l.location,available = case when (e.equipment_uid = l.equipment_uid) then 'Yes' else 'No' endfrom equipment eleft outer join location l on e.equipment_uid = l.equipment_uidand location = 'KY' |
 |
|
|
|
|
|
|
|