Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculated fields in SQL
Message
 
To
01/03/2006 11:45:00
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01095969
Message ID:
01100751
Views:
20
LOCAL lbtableUsed, lcSourcetable
m.lbtableUsed   = USED([MyTable]) && Don't use full path name here, just alias
m.lcSourcetable = [d:\mydir\mytable]
SELECT ... FROM (m.lcSourcetable) ...
IF NOT m.lbtableUsed
   USE IN SELECT ([MyTable])
ENDIF
>Borislav -
>
>Your help has been great. I've learned a lot about SQL SELECT in the last few days. If I SELECT from a table that is not currently open into a cursor, the source table is opened in the process. If the source table is in a variable, how can I then close it so it is available to other programs?
>
>lcSourcetable = 'd:\mydir\mytable'
>SELECT * from (lcsourcetable) into cursor crsNew
>** release the source table
>use in (lcSourceTable) && doesn't seem to work
>
>
>
>
>
>
>>My mistake
>>
>>SELECT Links.*, aNodes.x AS x1, aNodes.y as y1,;
>>                bNodes.x AS x2, bNodes.y as y2,;
>>       ABS(aNodes.x-bNodes.x) AS dx1, ABS(aNodes.y-bNodes.y) AS dy;
>>FROM LINKS;
>>LEFT JOIN Nodes aNodes ON Links.A == aNodes.N; && Field in Nodes table was N not A
>>LEFT JOIN Nodes bNodes ON Links.B == bNodes.N; && Field in Nodes table was N not B :o)
>>INTO CURSOR crsTest
>>
>>
>>BTW there is no need to open tables BEFORE select, just issue this select, but make sure that you have a path to tables you refer in it.
>>
>>
>>>Borislav -
>>>Based on Sergey's last post I was able to write the following code which seems to work.
>>>
>>>close tables all
>>>use linkdata in 0 alias links
>>>use xydata in 0 alias nodes
>>>select links.*, nodes.x as x1, nodes.y as y1 ;
>>>	from nodes ;
>>>	right outer join links ;
>>>	on nodes.n = links.a ;
>>>	where st_name <> ' ' ;
>>>	into cursor dt1
>>>
>>>select dt1.*, nodes.x as x2, nodes.y as y2, ;
>>>	abs(nodes.x - dt1.x1) as dx, abs(nodes.y - dt1.y1) as dy ;
>>>	from nodes ;
>>>	right outer join dt1 ;
>>>	on nodes.n = dt1.b ;
>>>	where st_name <> ' ' ;
>>>	order by st_name
>>>
>>>
>>>I was not able to get your code to work. I references nodes, anodes, and bnodes. Was it your intent to open the file 3 times? Even if I do just that it still doesn't work because anodes.A doesn't exist, etc.
>>>
>>>
>>>>I forgot, just put NVL() across all aNodes and bNodes fields, just in case the values you have in links table didn't exists in nodes one.
>>>>
>>>>>Sure.
>>>>>Nodes table contains 3 fields.
>>>>>N X Y
>>>>>3467 172653 298723
>>>>>2173 172550 298834
>>>>>2257 172403 298653
>>>>>
>>>>>The Links table only has 3 fields that Matter.
>>>>>A B ST_Name
>>>>>3467 2173 1st St.
>>>>>2173 2257 2nd St.
>>>>>2257 3467 3rd St.
>>>>>
>>>>>The result would look like this:
>>>>>A B St_Name X1 Y1 X2 Y2 DX DY
>>>>>3467 2173 1st St. 172653 298723 172550 298834 103 111
>>>>>2173 2257 2nd St. 172550 298834 172403 298653 147 181
>>>>>2257 3467 3rd St. 172403 298653 172653 298723 250 70
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>Don,
>>>>>>Can you post some simple data from both tables and desired result?
>>>>>>
>>>>>>
>>>>>>
>>>>>>>Thanks Sergy and Borislav -
>>>>>>>I guess my original question did not fully explain what I am trying to do. Sergey's answer gets me close but not quite there. Say there are 2 tables Links and Nodes. Each record of the Links table contains the the node numbers for the endpoints of a line. The Nodes table contains the x,y coordinates for each of the nodes. The task is to select all the records from the Links table and add the x and y coordinates for the 2 ends of the line. Thus X1 and Y1 define the first endpoint and X2 and Y2 define the second endpoint. We also want to compute the deltaX and delta Y for the 2 endpoints so we can tell if the line is mostly horizontal or mostly verticle. Based on Sergey's response I have coded:
>>>>>>>
>>>>>>>cd \foxpro\networkupdate
>>>>>>>close tables all
>>>>>>>use linkdata in 0 alias links
>>>>>>>use xydata in 0 alias nodes
>>>>>>>use xydata in 0 alias nodes2 again
>>>>>>>	>select dt1.*, nodes2.x as x2, nodes2.y as y2, ;
>>>>>>>abs(dt1.x1-nodes2.x) as dx, abs(dt1.y1-nodes2.y) as dy ;
>>>>>>>from ( ;
>>>>>>>	select links.*, nodes.n, nodes.x as x1, nodes.y as y1 ;
>>>>>>>	from nodes ;
>>>>>>>	right outer join links ;
>>>>>>>	on nodes.n = links.a ;
>>>>>>>	where st_name <> ' ' ;
>>>>>>>	) dt1 ;
>>>>>>>right outer join dt1 ;
>>>>>>>on dt1.n = nodes2.n ;
>>>>>>>where dt1.st_name <> ' '	
>>>>>>>
>>>>>>>Which gets me almost there but not quite because the outside SELECT wants to see dt1 as a preexisting table. Can either of you tell me where to go from here?
>>>>>>>
>>>>>>>Thanks
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>>That what we get when assuming anything. :)
>>>>>>>>Now lets wait and see who's assumption is correct.
>>>>>>>>
>>>>>>>>>But I am not referencing X2 and Y2, I am referencing nodes.x and nodes.y.
>>>>>>>>>If they are calculated fields, you are right, but they are not (if the example SELECT is correct).
>>>>>>>>>
>>>>>>>>>>It's not going to work. You cannot reference calculated fields in consequent calculations in the same list. You can use derived table
>>>>>>>>>>SELECT *, ABS(dt1.x2-dt2.y2) AS dx ;
>>>>>>>>>>  FROM ( ;
>>>>>>>>>>select temp1.*,nodes.x as x2, nodes.y as y2,
>>>>>>>>>>	from UPDNETATR!xydata nodes ;
>>>>>>>>>>	right outer join temp1 ;
>>>>>>>>>>	on  nodes.n = temp1.b;
>>>>>>>>>>	where st_name <> ' ' ;
>>>>>>>>>>	order by temp1.st_name ;
>>>>>>>>>>       ) dt1
>>>>>>>>>>
>>>>>>>>>>>Yes it is:
>>>>>>>>>>>BTW the result contains x2 and Y2 :-)
>>>>>>>>>>>of course if you didn't have X1 in Temp1
>>>>>>>>>>>
>>>>>>>>>>>select temp1.*,nodes.x as x2, nodes.y as y2, ABS(nodes.x-nodes.y) AS dx;
>>>>>>>>>>>	from UPDNETATR!xydata nodes ;
>>>>>>>>>>>	right outer join temp1 ;
>>>>>>>>>>>	on  nodes.n = temp1.b;
>>>>>>>>>>>	where st_name <> ' ' ;
>>>>>>>>>>>	order by temp1.st_name
>>>>>>>>>>>
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform