We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Multiple Column Values in One Query

Hi All,

I'm having a go at SQL queries and have the following:

SELECT     b.vSeries_Table_Number, b.vSeries_Number, b.vSeries_Geography, b.vSeries_Type,b.vSeries_Unit_Type, a.Variable_Date, a.Variable_Value
FROM         tblPopulationSC AS a INNER JOIN
                      tblVSeriesList AS b ON a.vSeries_ID = b.vSeries_ID
                      and b.vSeries_Geography='Manitoba' --and b.vSeries_Unit_Type=' 15 to 64 years'
                      and b.vSeries_Type=' Both sexes'
                      and b.vSeries_Unit_Type=' 65 years and over'

What I want to do is include more than one vSeries_Unit_Type. As you can see I have had to comment out the "15 to 64 years" type in order to have the "65 years and over" type. I want both in one query (as well as others) but when I include both in the query, I get no results back. If I comment out "65 years and over" and then use the "15 to 64 years", the query runs properly. As I said, it won't with both. How can I over come this?

3
Contributors
3
Replies
3 Hours
Discussion Span
2 Months Ago
Last Updated
18
Views
Question
Answered
Stuugie
Posting Whiz
330 posts since Jun 2012
Reputation Points: 30
Solved Threads: 3
Skill Endorsements: 0

I obviously have a lot to learn with queries. For my problem I created 2 separate views and then a query that includes both views. I don't particularily like the output format but it's a start.

Stuugie
Posting Whiz
330 posts since Jun 2012
Reputation Points: 30
Solved Threads: 3
Skill Endorsements: 0

My solution has been to use OR instead of AND like in the following:

SELECT DISTINCT 
                      b.vSeries_Table_Number, b.vSeries_Number, b.vSeries_Geography, b.vSeries_Type, b.vSeries_Unit_Type, a.Variable_Date, a.Variable_Value
FROM         dbo.tblPopulationSC AS a INNER JOIN
                      dbo.tblVSeriesList AS b ON a.vSeries_ID = b.vSeries_ID AND b.vSeries_Geography = 'Manitoba' AND b.vSeries_Type = ' Both sexes' AND 
                      (b.vSeries_Unit_Type = ' 15 to 64 years' OR
                      b.vSeries_Unit_Type = ' 65 years and over')
Stuugie
Posting Whiz
330 posts since Jun 2012
Reputation Points: 30
Solved Threads: 3
Skill Endorsements: 0
Question Self-Answered as of 2 Months Ago

You could also write it as:

SELECT DISTINCT 
                      b.vSeries_Table_Number, b.vSeries_Number, b.vSeries_Geography, b.vSeries_Type, b.vSeries_Unit_Type, a.Variable_Date, a.Variable_Value
FROM         dbo.tblPopulationSC AS a INNER JOIN
                      dbo.tblVSeriesList AS b ON a.vSeries_ID = b.vSeries_ID AND b.vSeries_Geography = 'Manitoba' AND b.vSeries_Type = ' Both sexes' AND 
                      b.vSeries_Unit_Type in ( ' 15 to 64 years', ' 65 years and over')
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0663 seconds using 2.68MB