Link to home
Start Free TrialLog in
Avatar of John Porter
John PorterFlag for United States of America

asked on

Filtering Table Records from a view

Hello,
I have two tables in a SQL SERVER 2005 View. They look like this:

Table1
product, weight
A ,  5.3
B,   2.9
C,   25.3
D,   32.6

Table2
product, weight
A,  0
B,  0

I am trying to create a view that sorts out rows in Table1 based on Table2. Table2 will always have some products with a weight of 0 (It could be any products in the range). I want to sort out all of the product records in Table1 that have a 0 weight in Table2.

I want the view to look like this:

View1
Product, price
C,   25.3
D,   32.6

I have been trying to do this by joining the tables in a view and just can’t quite get these results…

Does anyone know how to do this??

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Porter

ASKER

Thanks for the reply. It works if I only have single records but if it will not work if I am grouping by a subset of records.

I will edit the above example like this:

I have two tables in a SQL SERVER 2005 View. They look like this:

Table1
Sample#, product, weight
1,      A,      5.3
1,      B,      2.9
1,      C,      25.3
1,      D,      32.6
2,      A,      2.3
2,      B,      1.9
2,      C,      53.3
2,      D,      21.6


Table2
Sample#, product, weight
1,      A,       0
1,      B,       0
2,      A,       0
2,      C,       0

I want the view to look like this:

View1
Sample#, Product, price
1,      C,   25.3
1,      D,   32.6
2,      B,   1.9
2,      D,   21.6

Thanks again for your code help but in this case, B gets sorted out from sample 2.  Is there a way to group by sample number so that the products with 0 value weights are only sorted out from the appropriate sample??



Thanks!




SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lowfatspread
in general you want to use a keft outer join between the 2 tables so that your main tables doesn't have to have rows on the second which relate to them...

you specify the full join conditions between the tables (ie the foreign key)

select A.*
  from TableA as A
  Left Outer Join
               (select Key
                    from TableB as B
                   Where weight=0
               ) as B
     on A.Key=B.Key


in your current case
create View View1
as Select A.*
        From TableA as A
        Left Outer Join  TableB as B
            on A.[Sample#]  =B.[Sample#]
          and A.product=B.product
          and B.weight=0
     Where B.[Sample#] is null

this assumes that sample# and product are the actual keys for the tables...


LOWFATSPREAD.
CAN YOU PLEASE explain how your SQL is going to filter OUT ANY rows from A based on
select A.*
  from TableA as A
  Left Outer Join
               (select Key
                    from TableB as B
                   Where weight=0
               ) as B
     on A.Key=B.Key
??
I'd really appreciate it if you think and test your code before you commit what you may regret
Thanks Daniel and imitchie - that was it!!