Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
To use calculated field or not?
Message
From
15/01/2012 12:00:08
 
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01532936
Message ID:
01532973
Views:
24
>>>>>Are you talking about view in SQL Server or VFP? And how would a view help in this case?
>>>>>
>>>>I was thinking of SQL Server view, although it will work for VFP as well. We don't want to have calculated field in a table using UDF (as we need to base calculation on another table). The better solution is to use a view where this will be calculated.
>>>
>>>I have never, yet, used views in SQL Server. I will make a point to read and learn. Right now I am trying to make my app work on either SQL Server or VFP, using pretty much the same table structures and code. Thank you.
>>
>>
>>Dmitry,
>>
>>This is an example how to add po_Freight and po_SalesTax calculated fields to PO, summing them from the invoice table
>>
>>	create cursor po ;
>>	(	po_id		I, ;
>>		po_vendor		c(30), ;
>>		po_date		D, ;
>>		po_amount		Y ;
>>	)
>>	
>>	insert into po values (1, 'UT', date(), $100)
>>	insert into po values (2, 'Google', date()+1, $200)
>>	
>>	insert into po values (3, 'Microsoft', date()+2, $200)
>>	
>>	create cursor invoice ;
>>	(	inv_id		I, ;
>>		inv_po_id		I, ;
>>		inv_Freight		Y, ;
>>		inv_SalesTax	Y ;
>>	)
>>	
>>	insert into invoice values(1, 1, $20, $20)
>>	insert into invoice values(2, 1, $20, $20)
>>	
>>	
>>	insert into invoice values(3, 2, $20, $20)
>>	insert into invoice values(4, 2, $30, $30)
>>	
>>	
>>	
>>	select	;
>>			po_id, ;
>>			po_vendor, ;
>>			po_date, ;
>>			po_amount, ;
>>			nvl(po_Freight, $0)		as po_Freight, ;
>>			nvl(po_SalesTax, $0)	as	 po_SalesTax;
>>		from po ;
>>			left join ;
>>				( select ;
>>						inv_po_id, ;
>>						sum(inv_Freight) as po_Freight, ;
>>						sum(inv_SalesTax) as po_SalesTax ;
>>					from invoice ;
>>					group by 1 ;
>>				) X;
>>				on ( po_id == inv_po_id) 
>>
>
>Gregory, thank you very much!


You're welcome. This would be the view. If only one PO is needed, you can add a parameter
	po_needed = 3
	
	
	select	;
			po_id, ;
			po_vendor, ;
			po_date, ;
			po_amount, ;
			nvl(po_Freight, $0)		as po_Freight, ;
			nvl(po_SalesTax, $0)	as	 po_SalesTax ;
		from po ;
			left join ;
				( select ;
						inv_po_id, ;
						sum(inv_Freight) as po_Freight, ;
						sum(inv_SalesTax) as po_SalesTax ;
					from invoice ;
					where	( inv_po_id == ?po_needed ) ;
					group by 1 ;
				) X ;
				on ( po_id == inv_po_id) ;
		where ( po_id == ?po_needed ) ;
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform