Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 10th, 2004, 11:34 AM
Authorized User
 
Join Date: Nov 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to write SQL ..short way

Hello,
I need to build a SQL..i know the long way...need a short way

itemid Routeto
1 NB
1 IP
1 IQ
4 NB
4 IP
4 IQ
5 NB
5 JY
6 NB
6 AB
2 NB
3 IS

.
i want only those itemid which had routeto = NB and never had routeto = IQ or IP i.e 5 ,6 , 2

so i dont want 1,4 to ever come in the resultset!

Thks in advance

 
Old December 10th, 2004, 12:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

SELECT R1.itemid FROM tbRouteTo R1 WHERE R1.Routeto = 'NB' AND NOT EXISTS
(SELECT R2.itemid FROM tbRouteTo R2 WHERE R1.itemid = R2.itemid AND R2.Routeto IN ('IQ','IP'))

 
Old December 10th, 2004, 12:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

The first part of the query finds itemids that have a Routeto = 'NB' and don't have any matches in the second part (NOT EXISTS). It uses a table-alias: R1.

The second part finds itemids that have a Routeto = 'IQ' or 'IP' and the same itemid as the one in the first part (R1.itemid = R2.itemid) - this part uses the alias R2.

I hope this makes some kind of sense ;)
 
Old December 10th, 2004, 12:55 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jonax!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Short cut mateenmohd Access 2 November 20th, 2007 05:30 AM
Help with using a StoredProcedure to write to SQL garryg ASP.NET 1.0 and 1.1 Basics 1 February 17th, 2006 03:42 PM
want to get short date noor ASP.NET 1.0 and 1.1 Basics 3 May 8th, 2005 01:24 AM
plsssssssssssss help to write sql query sush_blr Beginning VB 6 4 February 1st, 2005 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.