473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Complex Conditonal SQL Statement - how?

Hi,

I have a database as such:
-------------------------------------------------------------------------------------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
-------------------------------------------------------------------------------------------------------------------
ProductOne | X | 25.50 S
ABC123
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | X | 12.00 S BBB667

ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543
and now I want to say this in an SQL query:
"If for the same Brand, if there is a record with PACKAGE_IND = "P" or
PACKAGE_IND = "L" then DONT display any of the other records of that
same brand where Code is in this list ('ABC123', 'BBB667') and
PACKAGE_IND = "S"

So the output would be:
-------------------------------------------------------------------------------------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
-------------------------------------------------------------------------------------------------------------------
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543

So only two got eliminated:
ProductOne X - becuase ProductY package_ind is L, so productOne X got
cut cuase it was "S" and was in that list

Similarly, ProductTwo X - becuase ProductTwo-Y got "P" and so
ProductTwo X has S and is in list. Althought ProductTwo Z is in the
same condition, it wasn' t in the list, so its ignored.
I hope someone out there can help...
please please please!

Jul 10 '06 #1
2 1498
Your question appears to be you want to get all records where 'Code' is not
equal to "ABC123" and "BBB67".
In that case the following SQL would produce the same result as shown in your
output.

SELECT tblName.*
FROM tblName
WHERE (((tblName.Code)<>"ABC123" And (tblName.Code)<>"BBB667"));

Am I correct ?

Surendran

ja********@gmail.com wrote:
>Hi,

I have a database as such:
-------------------------------------------------------------------------------------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
-------------------------------------------------------------------------------------------------------------------
ProductOne | X | 25.50 S
ABC123
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | X | 12.00 S BBB667

ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543

and now I want to say this in an SQL query:
"If for the same Brand, if there is a record with PACKAGE_IND = "P" or
PACKAGE_IND = "L" then DONT display any of the other records of that
same brand where Code is in this list ('ABC123', 'BBB667') and
PACKAGE_IND = "S"

So the output would be:
-------------------------------------------------------------------------------------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
-------------------------------------------------------------------------------------------------------------------
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543

So only two got eliminated:
ProductOne X - becuase ProductY package_ind is L, so productOne X got
cut cuase it was "S" and was in that list

Similarly, ProductTwo X - becuase ProductTwo-Y got "P" and so
ProductTwo X has S and is in list. Althought ProductTwo Z is in the
same condition, it wasn' t in the list, so its ignored.

I hope someone out there can help...
please please please!
--
Message posted via http://www.accessmonster.com
Jul 10 '06 #2
You don't say what the nameofyour table is so I've called it tblBrand in the
example below.

Something like:-

SELECT a.Brand, a.Model, a.UNT_RATE, a.PACKAGE_IND, a.Code
FROM tblBrand AS a LEFT JOIN (SELECT
Brand,
Model,
UNT_RATE,
PACKAGE_IND,
Code
FROM
tblBrand
WHERE
Brand IN(
SELECT Brand
FROM tblBrand
WHERE (((tblBrand.PACKAGE_IND) In ('P','L')))
)
AND
PACKAGE_IND = 'S'
AND
Code IN ('ABC123', 'BBB667')
) AS b ON (a.Code = b.Code) AND (a.PACKAGE_IND = b.PACKAGE_IND) AND (a.Brand
= b.Brand)
WHERE (((b.Brand) Is Null));
--

Terry Kreft
<ja********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi,

I have a database as such:
--------------------------------------------------------------------------
-----------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
--------------------------------------------------------------------------
-----------------------------------------
ProductOne | X | 25.50 S
ABC123
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | X | 12.00 S BBB667

ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543
and now I want to say this in an SQL query:
"If for the same Brand, if there is a record with PACKAGE_IND = "P" or
PACKAGE_IND = "L" then DONT display any of the other records of that
same brand where Code is in this list ('ABC123', 'BBB667') and
PACKAGE_IND = "S"

So the output would be:
--------------------------------------------------------------------------
-----------------------------------------
Brand | Model | UNT_RATE PACKAGE_IND Code
--------------------------------------------------------------------------
-----------------------------------------
ProductOne | Y | 82.10 L
DEFG789
ProductTwo | Y | 3.00 P
ASDF321
ProductTwo | Z | 8.20 S
ZZZ543

So only two got eliminated:
ProductOne X - becuase ProductY package_ind is L, so productOne X got
cut cuase it was "S" and was in that list

Similarly, ProductTwo X - becuase ProductTwo-Y got "P" and so
ProductTwo X has S and is in list. Althought ProductTwo Z is in the
same condition, it wasn' t in the list, so its ignored.
I hope someone out there can help...
please please please!

Jul 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: entoone | last post by:
I have a php page that displays some results, and contains a button with each row (record) that links to a more detailed page. The typical usrl for this button link is ...
3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
8
by: J.Haan | last post by:
Hi all. I'm currently coping with a problem on which I hope you could shed some light. Imagine the following: I have table in DB2 8.1 (.5) which is defined as: table test { t1 smallint,...
6
by: Hardy | last post by:
One of my customers have a sql statement totaled more than 400 lines, about 40KB. when excuted, error arrised saying "SQL0101N The statement is too long or too complex". I tried one of his...
3
by: Susie Swint | last post by:
I have the following IIf statement which worked in Access 95 but will not work in Access 2002. The error message I get is that the expression is typed incorrectly, or is too complex to be...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
6
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
1
by: Rahul Babbar | last post by:
Hi, I ran the scripts in a file from Command Line Processor and it gave the error for all the constraints being added, but not the indexes being added. For a simple statement like Alter...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.