MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 13th, 2004, 05:06 AM
sql asp sql asp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: UK
Posts: 259 sql asp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 51 sec
Reputation Power: 5
help with query please

Hello,

I have these tables:

Code:
CREATE TABLE Item (	
	cat_ref VARCHAR(5)PRIMARY KEY,
	descrip VARCHAR(50), 
	cat_type VARCHAR(20),
             notes VARCHAR(500))

CREATE TABLE CItem ( 
	contract_nm varchar(10)FOREIGN KEY 
REFERENCES Contract(contract_nm), 
cat_ref varchar(5) FOREIGN KEY REFERENCES Item(cat_ref) 
CONSTRAINT pk_ConItem PRIMARY KEY (contract_nm, cat_ref))


CREATE TABLE Contract (
	contract_nm VARCHAR(10) PRIMARY KEY)


What I want to do is select an item with a specific cat_ref that is listed against every contract. My query for selecting a specific contract goes like this:

Code:
SQLStmt = "SELECT * "
SQLStmt = SQLStmt & "FROM item, citem WHERE "&selection&" = '"&textentered&"'and citem.contract_nm = '"&contract_nm&"' and citem.cat_ref = item.cat_ref"


So that selects a specific item that is valid for a specific contract, but I need to find an item valid for ALL contracts? I've tried various joins etc but it ends up incredibly messy and I get the item listed 5 times (for 5 contracts)

If anyone can help it would be very much appreciated. Thanks

Reply With Quote
  #2  
Old September 13th, 2004, 12:37 PM
timseal timseal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: Philadelphia
Posts: 8 timseal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by sql asp
I've tried various joins etc but it ends up incredibly messy and I get the item listed 5 times (for 5 contracts)


SELECT DISTINCT on that messy query should help.

or (untested),

select catref from item where catref not in
(select distinct i.catref from item i left join citem ci on i.catref=ci.catref where ci.catref is null)

The left join includes all records from item, and where there is no match on the other side you get nulls. So any item with a null cannot be one that matches every time. All the others do match every time.

tim

Reply With Quote
  #3  
Old September 14th, 2004, 08:14 AM
sql asp sql asp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: UK
Posts: 259 sql asp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 51 sec
Reputation Power: 5
Quote:
select catref from item where catref not in
(select distinct i.catref from item i left join citem ci on i.catref=ci.catref where ci.catref is null)


This returns all items that are valid on any contract, not items valid for all contracts.

Reply With Quote
  #4  
Old September 15th, 2004, 03:54 PM
timseal timseal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: Philadelphia
Posts: 8 timseal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by sql asp
This returns all items that are valid on any contract, not items valid for all contracts.


Sorry, got them switched in my brain. Just put the right tables/fields in, if you get the idea of the query then you shouldn't have a problem.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > help with query please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway