dbTalk Databases Forums  

Re: Query Help Requested

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Re: Query Help Requested in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Query Help Requested - 07-27-2003 , 06:34 PM






Try:
declare @v_lnggroupid int
select @v_lnggroupid = 2
select lngID, strProduct
from products a
where not exists
(select 1 from groupproducts b
where
b.lngproductid = a.lngID
b.lnggroupid = @v_lnggroupid)

--
-Vishal
Lou Zucaro <lou (AT) netspecialists (DOT) com> wrote

Quote:
I'm having trouble figuring out the proper SQL statement
to use for this. Any help would be greatly appreciated:

Three tables are involved. Example data:

Table: Products
lngID strProduct
1 Toy
2 Book
3 Fruit
4 CD
5 Game

Table: Groups
lngID strGroup
1 Sales
2 Marketing
3 Executive

Table: GroupProducts
lngID lngProductID lngGroupID
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 2 2
7 4 2

I'm going to let people add new products to any
particular group, but obviously I don't want to show them
products that are already assigned to their group, so...

What I need is a query that will return the ID and name
of any product that's NOT already assigned to a
particular group. For this example, let's use group 2. So
the query should return products 1, 3 and 5.

I've tried various LEFT and RIGHT joins but obviously I'm
just not getting something because I keep getting back
basically the entire contents of the Products table, even
though some of the items have already been assigned to
the group in question.

Thanks in advance...

LZ



Reply With Quote
  #2  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Query Help Requested - 07-27-2003 , 11:05 PM






your SQL statement looks correct . what i 've posted is from the view point
of T-SQL.
First two lines are T-SQL syntaxes. hence there must be something equivalent
in ASP as well.
(I think you are through with it.)

Quote:
It all makes sense to me except the 'SELECT 1' in the
second part of the query...what does that provide? Does
it make sure the query doesn't return duplicates?
The "SELECT 1...." is a subquery which checks for the existance of the row
on the basis
of joins "sqlB.lngProductID = sqlA.ProductID" . so if match is found it will
return a row(here 1) else no
rows will be returned by the subquery, on the basis of the rows returned by
the subquery
your outer table will return the matching rows. It is no way related to the
returning duplicates.
However if your subquery returns two rows for a single "lngProductID" and if
your outer table
ie Products has only one row for the corresponding "lngProductID" then
outer query will
return only one row. but if Products table has more than one row a
particular "productid"
then you will have to use DISTINCT clause in SELECT statment to ignore
duplicates.

HTH

--
-Vishal
Lou Zucaro <lou (AT) netspecialists (DOT) com> wrote

Quote:
Hi again,

Actually, I think I figured it out...I got rid of the
first two lines (since I already have the variable set up
in my ASP code) and added an AND between the last two
lines. So the final sql statement looks like this
(modified a bit with actual field names):

sql = "SELECT ProductID, ProductName FROM Products sqlA
WHERE NOT EXISTS (SELECT 1 FROM GroupProducts sqlB WHERE
sqlB.lngProductID = sqlA.ProductID AND sqlB.lngGroupID
= " & lngGroupID & ");"

It all makes sense to me except the 'SELECT 1' in the
second part of the query...what does that provide? Does
it make sure the query doesn't return duplicates?

Thanks again for your help...VERY much appreciated!!

LZ

-----Original Message-----
Try:
declare @v_lnggroupid int
select @v_lnggroupid = 2
select lngID, strProduct
from products a
where not exists
(select 1 from groupproducts b
where
b.lngproductid = a.lngID
b.lnggroupid = @v_lnggroupid)

--
-Vishal
Lou Zucaro <lou (AT) netspecialists (DOT) com> wrote in message
news:038801c3540c$874c2140$a101280a (AT) phx (DOT) gbl...
I'm having trouble figuring out the proper SQL
statement
to use for this. Any help would be greatly appreciated:

Three tables are involved. Example data:

Table: Products
lngID strProduct
1 Toy
2 Book
3 Fruit
4 CD
5 Game

Table: Groups
lngID strGroup
1 Sales
2 Marketing
3 Executive

Table: GroupProducts
lngID lngProductID lngGroupID
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 2 2
7 4 2

I'm going to let people add new products to any
particular group, but obviously I don't want to show
them
products that are already assigned to their group,
so...

What I need is a query that will return the ID and name
of any product that's NOT already assigned to a
particular group. For this example, let's use group 2.
So
the query should return products 1, 3 and 5.

I've tried various LEFT and RIGHT joins but obviously
I'm
just not getting something because I keep getting back
basically the entire contents of the Products table,
even
though some of the items have already been assigned to
the group in question.

Thanks in advance...

LZ


.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.