Combine Two SQL Queries With Separate Where Statements

Last post 06-20-2008 2:16 PM by sliprix. 9 replies.

Sort Posts:

  • Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:31 AM
    • Loading...
    • sliprix
    • Joined on 05-21-2008, 2:58 PM
    • Posts 37

    I have two SQL queries that I would like to combine.  Each query is dependent on the same table, and the same rows, but they each have their own WHERE statements. I've thought about using some JOIN statements (left outer join in particular) but then I run into the problem of not having two separate tables, and don't see where I can put in two separate WHERE statements into the final query.  I've read into aliasing tables, but I'm not quite sure how that works (how to put it into code or a JOIN statement) , or if it would solve my question.  Do you have any ideas or examples of how to solve this scenario? 

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:41 AM
    • Loading...
    • Gaucho
    • Joined on 04-21-2008, 11:54 AM
    • Corona, California
    • Posts 183

    Use the UNION operator, as below: 

    SELECT DataName, DataNotes AS Whatever FROM
    Table_X
    WHERE DataName = 'Blue'
    UNION
    SELECT DataName, DataLocation AS Whatever FROM
    Table_X
    WHERE DataName = 'Red'

    ...when you use UNION, each WHERE statement must return an equal number of columns. As in the above example, where each "unioned" WHERE returns two columns of data. The return set will appear as one table, so you may need to name your columns accordingly... 

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:41 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 1,004

    If you want to combine two queries into a single resultset you can use UNION. There's more info on it here - http://www.w3schools.com/sql/sql_union.asp

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:48 AM
    • Loading...
    • sliprix
    • Joined on 05-21-2008, 2:58 PM
    • Posts 37

    Gaucho:
    ...when you use UNION, each WHERE statement must return an equal number of columns

    This poses a problem, I have three columns being returned by one query and two by the other.  Is there a way to do this?  Also I am wondering if there is a way to sum a column from the frist query with a column of the second query in the union?  Also if possible I would like to avoid doing five subqueries, it seems very inefficient.

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:58 AM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 507

    You could  add an empty column to that query with two columns, such as:

    select column1, column2, column3 from table1 where column1 = 'value'

    union

    select column1, column2, '' from table1 where column1 = 'value2'

    Lynn

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 9:59 AM
    • Loading...
    • Gaucho
    • Joined on 04-21-2008, 11:54 AM
    • Corona, California
    • Posts 183

    When I've run into this, I do something like add a third column to my additional WHERE statement to allow the UNION operator to do it's job.  It's not exactly elegant, but it works.  As far as using the SUM aggregator to total up two columns as you refer to, you start getting into GROUP BY clauses which are going to conflict with your UNION operator.  You might want to explore combining these statements using some type of subquery.

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 10:24 AM
    • Loading...
    • smetzger
    • Joined on 08-23-2007, 7:02 PM
    • Posts 90

    You could use a user defined function on the server.  User defined functions can return a 'table' and can be used in an sql statement just like a table.  This should allow you to do a sum in your function and then in your calling sql use a union.

    or 

    You could use two separate Gridviews one directly below the other one and without a header.

     

     

     

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 10:27 AM
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,308
    • Moderator
      TrustedFriends-MVPs

    You can look into another option with CASE to solve your problem. You can post some sample data and your table structure along with your expexted result. Thanks.

    Limno

  • Re: Combine Two SQL Queries With Separate Where Statements

    06-18-2008, 4:14 PM
    • Loading...
    • LockH
    • Joined on 03-25-2007, 2:58 PM
    • Scotland, where whisky has no e.
    • Posts 575

    As previously advised, add a dummy third column  to one of the queries.

    Create a view containing the UNIONed queries.

    Query the view, which will look like a single table, and you can sum and group as required.

     

    If a post helps to solve your problem, please click the Answer button on that post.

    I'm still confused, but now I'm confused on a higher plane.
  • Re: Combine Two SQL Queries With Separate Where Statements

    06-20-2008, 2:16 PM
    Answer
    • Loading...
    • sliprix
    • Joined on 05-21-2008, 2:58 PM
    • Posts 37

    I actually ended up using the LEFT OUTER JOINs with a few subqueries as well.  Everything worked out in the end.  Thanks for the help!

Page 1 of 1 (10 items)
Microsoft Communities
Page view counter