Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with SQL query

Posted on 2011-03-17
5
Medium Priority
?
275 Views
Last Modified: 2012-05-11
Hello, thank you for helping me with this MSSQL problem:

Table Cities:  CITYNAME, CITYOPEN  (1 means it is open, 0 means it is closed)
Seattle 1
Miami 1
Chicago 0
Phoenix 1
Columbus 1
Atlanta 0
Portland 1

Table USERS:   USERID, City  
1000  Portland
1001  Seattle
1001  Miami
1002  Atlanta
1002  Portland
1002  Phoenix


What I want to do is, given a USERID, say userID 1002, I want to run a check to see if any of that user's cities are closed.   So if USERID 1002 has even a single closed city, then return "CLOSED + CITY NAME(s)", otherwise "OPEN"

How can I do this in a SQL statement?

Examples:
UserID:
1000 = OPEN
1001 = OPEN
1002 = CLOSED ATLANTA
0
Comment
Question by:arthurh88
[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
5 Comments
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 1000 total points
ID: 35162919
Hi,

check this

SELECT A.*
FROM USERS A INNER JOIN Cities B
ON A.City = B.CityName
WHERE B.CITYOPEN = 0
0
 
LVL 24

Accepted Solution

by:
jimyX earned 1000 total points
ID: 35162936
Try this one please:
select U.UserID, (Case C.CITYOPEN when 0 then 'Closed '+ C.CITYName ELSE 'Open' End)
From Cities C join USERS U on U.City = C.CITYNAME
Where U.UserID = 1002;
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35162942

Hi.

Thanks Jim...


Sory author, didnt read Q fully...

between you want all closed city in one row?
0
 

Author Closing Comment

by:arthurh88
ID: 35162949
hi thank you.  Between both of you i got it working.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35162966
Cheers
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 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