Hi,

I'm having a hard time figuring out the best way to write an SQL query to do
the following.

I want to search a whole table (every field) for a word like "software" and
filter it by multiple clients and vendors that the user has chosen.

This is my SQL so far, but it doesn't work
SELECT Etlpws.unqentry, Etlpws.category, Etlpws.client, Etlpws.vendor,;
Etlpws.desc, Etlpws.wsaddy, Etlpws.wsfolder, Etlpws.wstaticip,;
Etlpws.swprodname, Etlpws.swprodkey, Etlpws.swpurdate, Etlpws.swcategory,;
Etlpws.swtype, Etlpws.swusers;
FROM ;
etlpws;
WHERE ( ( ( Etlpws.category = ( "software key" );
AND Etlpws.client = ( "External Client" ) );
AND Etlpws.client = ( "My Company" ) );
AND Etlpws.vendor = ( "Microsoft" ) );
AND Etlpws.vendor = ( "IBM" );
AND Etlpws.desc LIKE "%software%";
ORDER BY Etlpws.client, Etlpws.fullname;
INTO CURSOR Sw_query

So I have 2 problems.

1. I can't figure out a way of telling the above SQL to search every field
for my search term. I'm currently only testing using one field which is the
'desc' field. But need this to search all the fields.

2. The multiple where statements "AND Etlpws.client" end up displaying
every record that matches the client name which is correct, but it's
including records that don't have a matching "software" result found in the
"DESC" field. So instead of getting 2 results, I end up with 58 records
with the wrong data in it. I've tried changing it to say OR instead of AND
but then this displays 0 results.

Can anyone point me in the right direction?
Thanks
Tristan

Re: SQL help needed by RandyBosma

RandyBosma
Mon Sep 17 14:03:00 PDT 2007

Hello Tristan,

First, a brief comment: the extra parentheses aroung the string literals is
not necessary, and makes the WHERE clause more difficult to read (for a human)
. However, they do not cause an error as you've written it here.

The part of the WHERE clause that reads:
...... Etlpws.client = "External Client" ) ;
AND Etlpws.client = "My Company" ) ;
AND Etlpws.vendor = "Microsoft" ) ;
AND Etlpws.vendor = "IBM" ...
seems to eliminate all records. Can the Client be ""External Client" and "My
Company" at the same time? Can the Vendor be "Microsoft" and "IBM" at the
same time?

Tristan McElhinney wrote:
>1. I can't figure out a way of telling the above SQL to search every field
>for my search term. I'm currently only testing using one field which is the
>'desc' field. But need this to search all the fields.

Here's one fast off-the-cuff idea:
AND Etlpws.unqentry + Etlpws.category + Etlpws.client + Etlpws.vendor, ;
Etlpws.desc + Etlpws.wsaddy + Etlpws.wsfolder + Etlpws.wstaticip, ;
Etlpws.swprodname + Etlpws.swprodkey + Etlpws.swpurdate + Etlpws.swcategory,
;
Etlpws.swtype + Etlpws.swusers LIKE "%software%" ;

>2. The multiple where statements "AND Etlpws.client" end up displaying
>every record that matches the client name which is correct, but it's
>including records that don't have a matching "software" result found in the
>"DESC" field. So instead of getting 2 results, I end up with 58 records
>with the wrong data in it. I've tried changing it to say OR instead of AND
>but then this displays 0 results.

Without knowing more about the table and data you're working with, my first
guess is that the progblem lies in either the ANDs (maybe one should be an OR)
, -or- in the nesting of the parentheses. You've got a fairly complex
statement here. Try sketching it out on paper taking care to understand the
implications of every AND/OR and the nesting of every pair of ().

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums.aspx/foxpro-general/200709/1


Re: SQL help needed by Tristan

Tristan
Mon Sep 17 14:18:08 PDT 2007

Hi,

Thanks for your quick reply

Table looks something like this:

Category: Client: Vendor: Desc:
Software ClientB IBM "software"
User login ClientB Microsoft
Software ClientA Symantec "software"
Software ClientA Adobe "other
description"
Software ClientC Adobe "Second
description"
User login ClientC IBM "Third
description"


The first thing I want to do is show only the records that have a
Category="Software"
then with those records returned I want to filter by the selected clients
and vendors.

So say for example the user selects to just filter by ClientA and ClientB

It should just return these records, based on (Category=software,
Clients=clienta,clientb, Desc="software"
Software ClientA Symantec "software"
Software ClientB IBM "software"

If i could get an SQL statement to handle that without returning those other
records I should be on my way.

P.S I didn't know you could to the Etlpws.unqentry + Etlpws.category +
Etlpws.client , so will give that a try.

Thanks



"RandyBosma via DBMonster.com" <u17281@uwe> wrote in message
news:785e853de72ae@uwe...
> Hello Tristan,
>
> First, a brief comment: the extra parentheses aroung the string literals
> is
> not necessary, and makes the WHERE clause more difficult to read (for a
> human)
> However, they do not cause an error as you've written it here.
>
> The part of the WHERE clause that reads:
> ...... Etlpws.client = "External Client" ) ;
> AND Etlpws.client = "My Company" ) ;
> AND Etlpws.vendor = "Microsoft" ) ;
> AND Etlpws.vendor = "IBM" ...
> seems to eliminate all records. Can the Client be ""External Client" and
> "My
> Company" at the same time? Can the Vendor be "Microsoft" and "IBM" at the
> same time?
>
> Tristan McElhinney wrote:
>>1. I can't figure out a way of telling the above SQL to search every field
>>for my search term. I'm currently only testing using one field which is
>>the
>>'desc' field. But need this to search all the fields.
>
> Here's one fast off-the-cuff idea:
> AND Etlpws.unqentry + Etlpws.category + Etlpws.client + Etlpws.vendor, ;
> Etlpws.desc + Etlpws.wsaddy + Etlpws.wsfolder + Etlpws.wstaticip, ;
> Etlpws.swprodname + Etlpws.swprodkey + Etlpws.swpurdate +
> Etlpws.swcategory,
> ;
> Etlpws.swtype + Etlpws.swusers LIKE "%software%" ;
>
>>2. The multiple where statements "AND Etlpws.client" end up displaying
>>every record that matches the client name which is correct, but it's
>>including records that don't have a matching "software" result found in
>>the
>>"DESC" field. So instead of getting 2 results, I end up with 58 records
>>with the wrong data in it. I've tried changing it to say OR instead of
>>AND
>>but then this displays 0 results.
>
> Without knowing more about the table and data you're working with, my
> first
> guess is that the progblem lies in either the ANDs (maybe one should be an
> OR)
> , -or- in the nesting of the parentheses. You've got a fairly complex
> statement here. Try sketching it out on paper taking care to understand
> the
> implications of every AND/OR and the nesting of every pair of ().
>
> --
> Message posted via DBMonster.com
> http://www.dbmonster.com/Uwe/Forums.aspx/foxpro-general/200709/1
>


Re: SQL help needed by RandyBosma

RandyBosma
Tue Sep 18 06:46:31 PDT 2007

Good morning Tristan,

Tristan McElhinney wrote:
>It should just return these records, based on (Category=software,
>Clients=clienta,clientb, Desc="software"
>Software ClientA Symantec "software"
>Software ClientB IBM "software"
>
>If i could get an SQL statement to handle that without returning those other
>records I should be on my way.

Here are two WHERE clauses that may do the job for you:

WHERE Etlpws.category="Software" AND ;
( Etlpws.client="ClientA" OR Etlpws.client="ClientB" ) AND ;
Etlpws.desc="software"

and this one substituting the INLIST() function for the OR

WHERE Etlpws.category="Software" AND ;
INLIST(Etlpws.client,"ClientA","ClientB") AND ;
Etlpws.desc="software"

>P.S I didn't know you could to the Etlpws.unqentry + Etlpws.category +
>Etlpws.client , so will give that a try.

Yes, you can, assuming that they are all Character fields.

Enjoy!
Randy Bosma

--
Message posted via http://www.dbmonster.com


Re: SQL help needed by Anders

Anders
Tue Sep 18 15:32:16 PDT 2007

INLIST () is an Xbase function. The SQL standard function is called IN ().
Since VFP9 IN() is no longer mapped to INLIST(). It also, since VFP9,
accepts many more arguments (hundreds) than INLIST which is limited to 25.
-Anders

"RandyBosma via DBMonster.com" <u17281@uwe> wrote in message
news:786748630ecfa@uwe...
> Good morning Tristan,
>
> Tristan McElhinney wrote:
>>It should just return these records, based on (Category=software,
>>Clients=clienta,clientb, Desc="software"
>>Software ClientA Symantec "software"
>>Software ClientB IBM "software"
>>
>>If i could get an SQL statement to handle that without returning those
>>other
>>records I should be on my way.
>
> Here are two WHERE clauses that may do the job for you:
>
> WHERE Etlpws.category="Software" AND ;
> ( Etlpws.client="ClientA" OR Etlpws.client="ClientB" ) AND ;
> Etlpws.desc="software"
>
> and this one substituting the INLIST() function for the OR
>
> WHERE Etlpws.category="Software" AND ;
> INLIST(Etlpws.client,"ClientA","ClientB") AND ;
> Etlpws.desc="software"
>
>>P.S I didn't know you could to the Etlpws.unqentry + Etlpws.category +
>>Etlpws.client , so will give that a try.
>
> Yes, you can, assuming that they are all Character fields.
>
> Enjoy!
> Randy Bosma
>
> --
> Message posted via http://www.dbmonster.com
>