dbTalk Databases Forums  

SQL Query Help Needed

comp.databases.xbase.fox comp.databases.xbase.fox


Discuss SQL Query Help Needed in the comp.databases.xbase.fox forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gene Wirchenko
 
Posts: n/a

Default SQL Query Help Needed - 07-03-2004 , 12:42 AM






I am making some fairly severe modifications to an app I have
written. I want to do some checks on the data before I make the
changes.

One of the checks I wanted to make was comparing certain similar
work orders. I wanted to do it in one SQL query, but I could not
figure out how. I ended up querying to find where there was more than
one of the type of work order and then iterating through that to get
the individual work orders. My code follows:

set talk off

select;
clcode,wccode,;
left(dtos(trndtlow),6) as low,left(dtos(trndthi),6) as high,;
count(*) from cwko;
group by clcode,wccode,low,high;
where wostatus#"V";
having count(*)>1;
into cursor mults

select mults
goto top
do while !eof()

select * from cwko where;
clcode=mults.clcode and wccode=mults.wccode and;
left(dtos(trndtlow),6)=mults.low and;
left(dtos(trndthi),6)=mults.high

select mults
skip

enddo

use in mults

return

Checking and closing each browse is awkward as is joining the
subqueries together. Is there any way to do this in one SQL query?

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.

Reply With Quote
  #2  
Old   
Roger Ansell
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-03-2004 , 11:11 AM






Hi Gene,

At first glance, I thought this was a self-join candidate but
on closer examination, I don't think it can be done with
a single query.

However, having established the number of "duplicates" in
cursor "mults", you could simply obtain a cursor containing all the
"duplicates" by running a second query joining mults to cwko.

Something like ...

Select * from mults join cwko;
on mults.clcode=cwko.clcode ;
and mults.wccode=cwko.wccode ;
and mults.low=left(dtos(cwko.trndtlow),6);
and mults.high=left(dtos(cwko.trndthi),6) ;
where cwko.wostatus<>"V" ;
into cursor c_dupes

If I'm missing the point, please feel free to call me an idiot ;-)

-Roger
--
Roger Ansell
Adelaide, Australia

My real email address is ransell at senet dot com dot au

Gene Wirchenko <genew (AT) mail (DOT) ocis.net> wrote:
Quote:
I am making some fairly severe modifications to an app I have
written. I want to do some checks on the data before I make the
changes.

One of the checks I wanted to make was comparing certain similar
work orders. I wanted to do it in one SQL query, but I could not
figure out how. I ended up querying to find where there was more than
one of the type of work order and then iterating through that to get
the individual work orders. My code follows:

set talk off

select;
clcode,wccode,;
left(dtos(trndtlow),6) as low,left(dtos(trndthi),6) as high,;
count(*) from cwko;
group by clcode,wccode,low,high;
where wostatus#"V";
having count(*)>1;
into cursor mults

select mults
goto top
do while !eof()

select * from cwko where;
clcode=mults.clcode and wccode=mults.wccode and;
left(dtos(trndtlow),6)=mults.low and;
left(dtos(trndthi),6)=mults.high

select mults
skip

enddo

use in mults

return

Checking and closing each browse is awkward as is joining the
subqueries together. Is there any way to do this in one SQL query?

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.




Reply With Quote
  #3  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-03-2004 , 09:37 PM



"Roger Ansell" <notmyreal (AT) emailaddress (DOT) com> wrote:

Quote:
At first glance, I thought this was a self-join candidate but
on closer examination, I don't think it can be done with
a single query.
I was thinking so. I believe it could be done in one query if
VFP supported
select ... from (<subquery>)

Quote:
However, having established the number of "duplicates" in
cursor "mults", you could simply obtain a cursor containing all the
"duplicates" by running a second query joining mults to cwko.

Something like ...

Select * from mults join cwko;
on mults.clcode=cwko.clcode ;
and mults.wccode=cwko.wccode ;
and mults.low=left(dtos(cwko.trndtlow),6);
and mults.high=left(dtos(cwko.trndthi),6) ;
where cwko.wostatus<>"V" ;
into cursor c_dupes

If I'm missing the point, please feel free to call me an idiot ;-)
That would be rude, and no, you did get it.

A few quibbles:

1) "high" is a reserved word, so I changed it. This was my error.

2) There should be an order clause.

3) I did not want any of the columns from mults to show. I managed
to solve that one with a bit of thought.

I ended up:

select cwko.* from mults;
join cwko;
on mults.clcode=cwko.clcode and mults.wccode=cwko.wccode and;
mults.dlow=left(dtos(cwko.trndtlow),6) and;
mults.dhigh=left(dtos(cwko.trndthi),6);
where cwko.wostatus#"V";
order by mults.clcode,mults.wccode,mults.dlow,mults.dhigh;
into cursor c_dupes

Thank you for the help.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.


Reply With Quote
  #4  
Old   
Bill Browne
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-10-2004 , 08:56 AM



Untested speculation, but it seems like something like this has worked for
me in the past:

Assuming all your fields are fixed length strings (apply str() if not)

select * from cwko where;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6)
in;
(select;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6) as anything, ;
count(*) from cwko;
group by anthing;
where wostatus#"V";
having count(*)>1)

--
Bill Browne
www.edgefinderstudios.com


"Gene Wirchenko" <genew (AT) mail (DOT) ocis.net> wrote

Quote:
I am making some fairly severe modifications to an app I have
written. I want to do some checks on the data before I make the
changes.

One of the checks I wanted to make was comparing certain similar
work orders. I wanted to do it in one SQL query, but I could not
figure out how. I ended up querying to find where there was more than
one of the type of work order and then iterating through that to get
the individual work orders. My code follows:

set talk off

select;
clcode,wccode,;
left(dtos(trndtlow),6) as low,left(dtos(trndthi),6) as high,;
count(*) from cwko;
group by clcode,wccode,low,high;
where wostatus#"V";
having count(*)>1;
into cursor mults

select mults
goto top
do while !eof()

select * from cwko where;
clcode=mults.clcode and wccode=mults.wccode and;
left(dtos(trndtlow),6)=mults.low and;
left(dtos(trndthi),6)=mults.high

select mults
skip

enddo

use in mults

return

Checking and closing each browse is awkward as is joining the
subqueries together. Is there any way to do this in one SQL query?

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.



Reply With Quote
  #5  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-10-2004 , 10:02 PM



"Bill Browne" <bill (AT) excalibur-dbf (DOT) com> wrote:

Quote:
Untested speculation, but it seems like something like this has worked for
me in the past:

Assuming all your fields are fixed length strings (apply str() if not)
clcode and wccode are fixed-length strings.

Quote:
select * from cwko where;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6)
in;
(select;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6) as anything, ;
count(*) from cwko;
group by anthing;
where wostatus#"V";
having count(*)>1)
Thank you. Interesting solution. I had not thought of
concatenating the fields. It is ugly though. (I prefer to avoid
mixing domains where possible.) Can you think of a way that does not
involve the concatenation? To me, it does not appear possible. That
aggregation makes the difference. I do wish VFP supported a
select ... from <subquery>

I ran this problem by the instructor I had for my databases
course, and he did not see any better. I wonder what he would think
of your approach.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.


Reply With Quote
  #6  
Old   
Fred Taylor
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-11-2004 , 02:07 AM




"Gene Wirchenko" <genew (AT) mail (DOT) ocis.net> wrote

Quote:
I do wish VFP supported a
select ... from <subquery

Have you checked out the VFP9 beta? It has exactly what you're looking for.

http://msdn.microsoft.com/vfoxpro

Fred
Microsoft Visual FoxPro MVP




Reply With Quote
  #7  
Old   
Bill Browne
 
Posts: n/a

Default Re: SQL Query Help Needed - 07-13-2004 , 05:35 PM



Would probably send him screaming into the night. But if it works...

--
Bill Browne
www.edgefinderstudios.com


"Gene Wirchenko" <genew (AT) mail (DOT) ocis.net> wrote

Quote:
"Bill Browne" <bill (AT) excalibur-dbf (DOT) com> wrote:

Untested speculation, but it seems like something like this has worked
for
me in the past:

Assuming all your fields are fixed length strings (apply str() if not)

clcode and wccode are fixed-length strings.

select * from cwko where;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6)
in;
(select;
clcode + wccode;
+ left(dtos(trndtlow),6) + left(dtos(trndthi),6) as anything, ;
count(*) from cwko;
group by anthing;
where wostatus#"V";
having count(*)>1)

Thank you. Interesting solution. I had not thought of
concatenating the fields. It is ugly though. (I prefer to avoid
mixing domains where possible.) Can you think of a way that does not
involve the concatenation? To me, it does not appear possible. That
aggregation makes the difference. I do wish VFP supported a
select ... from <subquery

I ran this problem by the instructor I had for my databases
course, and he did not see any better. I wonder what he would think
of your approach.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.



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 - 2012, Jelsoft Enterprises Ltd.