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?

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.

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')

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')
commented: Thanks, that will help clean up my statements. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.