Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select from a DBF table
Message
From
17/05/2019 12:56:33
 
 
To
16/05/2019 16:52:50
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01668631
Message ID:
01668688
Views:
63
>>>>Hi,
>>>>
>>>>Can I SQL Select from a DBF table and increase a number of records according to a column?
>>>>
>>>>Here is an example.
>>>>Table of the following structure:
>>>>
>>>>PART #      PART DESCR                QTY
>>>>'ABC'         Abc Part                       1
>>>>'CBS'         Cbs Part                       2
>>>>'NBC'        Nbc Part                       5
>>>>'ESPN"      ESPN Part                      0
>>>>NFL           NFL Part                      10   
>>>>
>>>>
>>>>Then the resulting cursor should have the same structure (resulting cursor of temporary name). And the number of records would increase according to the QTY column. For example, part "NBC" would be entered 5 times (5 records). And ESPN 0 records.
>>>>
>>>>TIA
>>>
>>>With the dbf alone no.
>>>With a second dbf yes.
>>>
>>>create cursor x (qty int)
>>>for k=1 to 100 && max(qty)
>>>  insert into x values (m.k)
>>>next
>>>select a.* from table a join x on x.qty <= a.qty into ...
>>>
>>
>>No time to check, but perhaps using the same table again under a different alias, and then joining them as you said.
>
>feasable if reccount()>=max(qty)

I like Naomi's suggestion.

Here is one way in VFP to get your result with a single line of sql using only your base table to start with.
It may be cheating but user defined functions and a union clause seem to get the result your are looking for.
Local jsel
jsel=Select()

*-- create the test cursor or table
Create Cursor part_test (part_numb c(10), part_desc c(20), qty i)

Insert Into part_test (part_numb,  part_desc, qty) Values ('ABC','Abc Part', 1)
Insert Into part_test (part_numb,  part_desc, qty) Values ('CBS','Cbs Part', 2)
Insert Into part_test (part_numb,  part_desc, qty) Values ('NBC','Nbc Part', 5)
Insert Into part_test (part_numb,  part_desc, qty) Values ('ESPN','ESPN Part', 0)
Insert Into part_test (part_numb,  part_desc, qty) Values ('NFL','NFL Part', 10)


*-- one line of sql with a couple of UDF and union clause
SELECT part_test.* ;
FROM part_test ;
WHERE ;
.f. AND ;
part_test.part_numb in (SELECT part_expand_create() as xtest FROM part_test WHERE .f.) ;
UNION ALL ;
Select part_test.* ;
FROM part_test ;
join part_expand ;
	ON part_test.part_numb=part_expand.part_numb ;
WHERE ;
part_test.part_numb in (SELECT part_udf(part_numb) as test FROM part_test) ;
INTO Cursor tresult


Browse




Select(jsel)
Return

*-- first select returns zero records but creates cursor for adding records
PROCEDURE part_expand_create
	Select * From part_test Where .F. Into Cursor part_expand  Readwrite
    RETURN ''
endpro

*-- UDF loops through each record of the select 
Procedure part_udf
Lparameters tpart_numb
LOCAL orecord
Local array tempexpand[1,1]


Select part_test
GO bott
Locate For part_numb = m.tpart_numb

If Found()

	SCATTER NAME orecord 

	Local i
	If orecord.qty>=1
		For i = 1 To orecord.qty
			
			*-- test, prior to adding
			SELECT COUNT(part_numb) FROM part_expand WHERE part_numb=m.orecord.part_numb inTO ARRAY tempexpand
			IF _tally>0 AND tempexpand[1]<orecord.qty
				Insert Into part_expand From Name orecord
			ENDIF
			RELEASE tempexpand
			
		Endfor
	ENDIF
	
	RELEASE orecord

	Select part_expand
	
	RETURN part_expand.part_numb

Endif



Endproc
Thanks,

Stacy



Black Mountain Software, Inc.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform