Solved

Filtering Table Records from a view

Posted on 2007-11-19
6
179 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:Saxitalis
[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
6 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 250 total points
ID: 20315706
Create view View1
As
Select T1.Product, T1.Weight
From Table1 T1 Left Join Table2 t2 ON T1.Product = T2.Product And T2.weight = 0
Where T2.Product IS NULL
0
 

Author Comment

by:Saxitalis
ID: 20317111
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!




0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20317868
Create view myView
As
Select a.Product, a.Weight
From Table1 a Left Join Table2 b ON [a.Sample#] = [b.Sample#] and a.Product = b.Product And b.weight = 0
Where b.Product IS NULL
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 20317883
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...


0
 
LVL 25

Expert Comment

by:imitchie
ID: 20317973
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
0
 

Author Comment

by:Saxitalis
ID: 20328499
Thanks Daniel and imitchie - that was it!!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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