calculating the totals of multiple items - calculator

How would I display the total price of selecting multiple items and how to assign that totals to that specific items? eg. when purchasing 2 textbooks at $12ea.
Example code:
books = """
Magazines
Novels
Cooking Books
Textbooks
"""
order = input("what would you like from our book list " + books)
quantity = input("How many " + order + " would you like?\n")
def cal(textbook = 16):
print(textbook * int(quantity))
cal()

Related

Find The Nearest Station To Every Customer

I have two datasets :
A "customer" dataset with customer names and geographical coordinates (x,y)
A "stations" dataset with stations names and geographical coordinates (x,y)
What I need to do :
Find for each customer, the nearest station from the "stations" dataset
At the end, i need a dataset with :
customer_name, customerX, customerY, nearest_station_name, nearest_station_x, nearest_station_y
Nearest Definition :
For example for customer "c":
s1 is the station 1
s2 is the station 2
if ((Xs1-Xc)² + (Ys1-Yc)²) < ((Xs2-Xc)² + (Ys2-Yc)²) Then the Nearest station is S1
if ((Xs1-Xc)² + (Ys1-Yc)²) = ((Xs2-Xc)² + (Ys2-Yc)²) Then the Nearest stations is either
if ((Xs1-Xc)² + (Ys1-Yc)²) > ((Xs2-Xc)² + (Ys2-Yc)²) Then the Nearest station is S2
That mean i need to know for each customer and each station, the result of (Xsi-Xc)² + (Ysi-Yc)²
Do you know if i can do that in spark scala or spark sql or bigquery without having to code a UDF?
Thank you for your help.
I tried, for every customer, to loop thru the stations list in order to find the nearest but its too complicated and should be a UDF, which i dont want if not mandatory ...
Double nearestStationDistance = Double.MAX_VALUE;
Station nearestStation = null;
for(Station station : stations){
Double distance = ((station.x - customer.x)² + (station.y - customer.y)²);
if(distance < nearestStationDistance ){
nearestStationDistance = distance;
nearestStation = station
}
}
return nearestStation;
And after extract informations from the "Station" object to get the name and the coordinates in order to complete the customer dataset.
I wrote couple posts about doing it in BigQuery:
https://mentin.medium.com/nearest-neighbor-in-bigquery-gis-7d50ebd5d63
https://mentin.medium.com/nearest-neighbor-using-bq-scripting-373241f5b2f5
The solution is simple to express in SQL:
SELECT
a.id,
ARRAY_AGG(b.id ORDER BY ST_Distance(a.geog, b.geog) LIMIT 1)
[ORDINAL(1)] as neighbor_id
FROM people_table a JOIN restaurant_table b
GROUP BY a.id
But that solution does not scale when tables are large, and the posts discuss options to speed things up.

libreoffice base create a list filtered by another list's value

I have a table of provinces and a table of cities with ProvienceID. In a form, I want to create a list of cities filtered by selected value of provience list.
How can I do that?
I can create both lists but Cities list shows all cities from all provinces but i want to show only cities from the province that I have selected in Provinces list.
I have another table "Users" with "CityID" and "ProvinceID" that my form edits it and I need to save selected values of Province and City Lists in it, not only show it in the form.
Create two example tables named "Provinces" and "Cities".
ProvinceID Name
~~~~~~~~~~ ~~~~
0 South
1 North
2 Large Midwest
3 Southeast
4 West
CityID Name ProvinceID
~~~~~~ ~~~~ ~~~~~~~~~~
0 Big City 2
1 Very Big City 2
2 Rural Village 1
3 Mountain Heights 0
4 Coastal Plains 4
5 Metropolis 2
Create a query called "ProvinceNames":
SELECT "Name" AS "Province"
FROM "Provinces"
ORDER BY "Province" ASC
Create a query called "Province of City":
SELECT "Provinces"."Name" AS "Province", "Cities"."Name" AS "City"
FROM "Cities", "Provinces" WHERE "Cities"."ProvinceID" = "Provinces"."ProvinceID"
ORDER BY "Province" ASC, "City" ASC
In the form, create a table control based on the query "ProvinceNames".
Using the Form Navigator (or the Form Wizard), create a subform for query "Province of City".
Right-click on subform and choose Properties. Under Data tab:
Link master fields "Province"
Link slave fields "Province"
Create a table control for the subform as well. Now, the cities shown in the subform control depend on the province selected in the main form control.
EDIT:
Here is an example using a filter table to store the current value of the list box. Create two more tables named "Users" and "FilterCriteria".
UserID Name ProvinceID CityID
~~~~~~ ~~~~~~~ ~~~~~~~~~~ ~~~~~~
0 Person1 1 2
1 Person2 2 0
RecordID ProvinceID CityID
~~~~~~~~ ~~~~~~~~~~ ~~~~~~
the only 0 0
We'll also need two Basic macros which can be stored in the document or in My Macros. Go to Tools -> Macros -> Organize Macros -> LibreOffice Basic.
Sub ReadProvince (oEvent as Object)
forms = ThisComponent.getDrawPage().getForms()
mainForm = forms.getByName("MainForm")
cityForm = forms.getByName("CityForm")
listboxProvince = mainForm.getByName("listboxProvince")
listboxCity = cityForm.getByName("listboxCity")
selectedItemID = listboxProvince.SelectedValue
If IsEmpty(selectedItemID) Then
selectedItemID = 0
End If
conn = mainForm.ActiveConnection
stmt = conn.createStatement()
strSQL = "UPDATE ""FilterCriteria"" SET ""ProvinceID"" = " & selectedItemID & _
"WHERE ""RecordID"" = 'the only'"
stmt.executeUpdate(strSQL)
listboxCity.refresh()
lCityCol = mainForm.findColumn("CityID")
currentCityID = mainForm.getInt(lCityCol)
cityForm.updateInt(cityForm.findColumn("CityID"), currentCityID)
listboxCity.refresh()
End Sub
Sub CityChanged (oEvent as Object)
listboxCity = oEvent.Source.Model
cityForm = listboxCity.getParent()
mainForm = cityForm.getParent().getByName("MainForm")
lCityCol = mainForm.findColumn("CityID")
selectedItemID = listboxCity.SelectedValue
If IsEmpty(selectedItemID) Then
selectedItemID = 0
End If
mainForm.updateInt(lCityCol, selectedItemID)
End Sub
Now we need to set up the form like this. In this example, I used two top-level forms instead of a subform. ProvinceID and CityID text boxes are not required but may be helpful in case something goes wrong.
To start creating this form, use the form wizard to create a new form and add all fields from the Users table.
Now, in the Form Navigator, create a form called "CityForm". Content type is SQL command, and Content is:
SELECT "RecordID", "ProvinceID", "CityID" FROM "FilterCriteria"
WHERE "RecordID" = 'the only'
Next, create the "listboxProvince" list box under MainForm. Data Field is "ProvinceID", and List content is the following Sql.
SELECT "Name", "ProvinceID" FROM "Provinces" ORDER BY "Name" ASC
Finally, create the "listboxCity" list box under CityForm. Data Field is "CityID", and List content is the following Sql.
SELECT "Name", "CityID" FROM "Cities" WHERE "ProvinceID" = (
SELECT "ProvinceID" FROM "FilterCriteria"
WHERE "RecordID" = 'the only')
Macros are linked under the Events tab of each control.
Assign "After record change" of the MainForm to ReadProvince().
Assign "Changed" of listboxProvince to ReadProvince().
Assign "Changed" of listboxCity control to CityChanged().
The result allows us to select the Province to filter the list of Cities. Provinces and Cities that are selected are saved in the Users table.
There is another approach which may be better that I have not had time to explore. Instead of the "FilterCriteria" table, apply a filter to the Cities list. The relevant code in ReadProvince() would look something like this.
cityForm.Filter = "ProvinceID=" & selectedItemID
cityForm.ApplyFilter = True
cityForm.reload()
cityForm.absolute(0)
Whatever approach is taken, a complete solution requires complex macro programming. To make it easier, you may decide to use a simpler solution that is not as powerful. For more information, there is a tutorial at https://forum.openoffice.org/en/forum/viewtopic.php?t=46470.
EDIT 2
A solution that requires fewer queries is at https://ask.libreoffice.org/en/question/143186/how-to-use-user-selected-value-from-combobox1-in-combobox2-select-statement/?answer=143231#post-id-143231. The second list box is based on a list of values instead of an SQL query.

Crystal report - Grouping from Formula and Running Totals

I am trying to create a Crystal Report. One of the parameters that it has, is a Boolean flag which change the groupings of the Report.
What I'm trying to do is to add 3 levels of grouping from a formula.
The code that I wrote is:
if {?summarized_detailed} = true then
'Store Code : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.WhsCode}
else
if {?season_supplier} = true then
'Store Code : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.WhsCode} + ChrW(10) + 'Season : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.SEASON} + ChrW(10) + 'Brand : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.BRAND}
else
'Store Code : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.WhsCode} + ChrW(10) + 'Supplier : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.CardCode} + ChrW(10) + 'Brand : ' + {SNV_SP_ProfitabilityAndBreakEvenPerStore;1.BRAND}
{?summarized_detailed} is the parameter (the flag for grouping).
if he chooses true, the report must have the columns -> Store Code , Season or Supplier (it based on another flag-parameter) and Brand.
Output Layer that I have :
Name of Col1 Name of Col2 Name of Col3
Store: Value for Store Code, Season or Supplier: Value for Season or Supplier, Brand: Value for Brand | total Brand Col1 total Brand Col2 total Brand Col3 ....
With this Code, I take the result that I want. All database's rows are separated according to those groups.
The problem is, that I want to have total sum for every column that I have in my report. But I can't do this, because the above formula, created only ONE union group. So, I can have a summary only for the details within every brand.
Output Layer that I want :
Name of Col1 Name of Col2 Name of Col3
Store: Value for Store Code |
Seas or Sup: Value for Season or Supplier |
Brand: Value for Brand | total Brand Col1 total Brand Col2 total Brand Col3
//when supplier or season within all groups changes (not included in report)
--------------------------------------------------------------------------------------------
Total Supplier or Season: total Sup/Season Col1 total Sup/Season Col2 total Sup/Seas Col3
//when store within all groups changes (not included in report)
Total Store: total Store Col1 total Store Col2 total Store Col3
---------------------------------------------------------------------------------------------
With total Brand Col1 I want to do a summary for all rows of details for each field of the same brand.
With total Supplier Col1 I want to sum all total Brands for each field of the same supplier.
With total Store Col1 I want to sum all total Suppliers for each field for every shop.
How can I compute those totals and display them only when Season or Supplier changes, and when Store changes with no regarding the change of the union Group ?
Or is there an easier and better way to make those 3 groups from formula according to the parameter but let them be separated so that I can control them and do my summarizes ?
This is how I handle this type of requirement:
If you want a maximum of 3 groups, but potentially less, then you need to create two different formula fields. In the report design, you set grouping to group on Store_Code first, then the other two formula fields.
If the refresh/runtime parameter will be FALSE for when you want the three-level grouping, then the formulas should look something like this:
Group1_Formula1: if {?param} = TRUE then '' else {Season}
Then the next:
Group2_Formula: if {?param} = TRUE then '' else {Brand}
If you want totals/subtotal the just place them as summary fields in the group headers, with conditional display to suppress them as necessary.
The result will be that, even though grouping on all three levels is always taking place, for the subgroups below the always-active "Store_Code" each subgroup will have only a single group of all records if the {?Param} = TRUE, effectively meaning there is no grouping happening.
One caveat: This presumes you aren't using print-time evaluation, e.g. before/after printing, etc., in any of the related formulas necessary to perform your grouping.

Retrieve field based on record field

I have 3 tables
items,
item_units,
order_items
First table items has the list of items that can be ordered.
Second table item_units has units for the items as well as the amount of those items in this unit
Third table has items that were ordered... ie... item_code , unit, qty
Here are the columns for items
[item_code]
,[desc]
Here are the columns for item_units
,[item_code]
,[unit]
,[amount]
,[price]
,[default_sprd_sht]
Here are the columns for order_items
,[order_id]
,[item_code]
,[item_desc]
,[unit]
,[qty]
,[price]
Note the [default_sprd_sht]. This field is a boolean. If it's set to true this unit is never put into order_items table. This field will be used as calculation field.
For example:
If 1 customer orders 2 6 packs of bread and another orders 3 dozens of bread, the baker needs to know how many pans of bread to make.
Now a 6 pack unit has 6 breads as amount, meaning 2 * 6 = 12. And a dozen unit has 12 breads.. 12 * 3 = 36. A pan bread unit has 20 breads. So i need to add up all the bread units amounts and divide them by the pan amount like so
((2*6) + (12 * 3)) / 20 = 2.4
So the first thing I did to create a report for the baker was
Create a group for order_items.item_code and then order_item.unit.
This needs to be done since the same item and unit cobination will be repeated in different orders. The baker needs to see how many bagels or breads he needs to bake in total.
in the order_item.unit group header I created a formula field that multiplies the order_item.unit by item.amount
Sum ({order_items.qty}, {order_items.unit}) * {items_units.amount}
That was easy.
But I aslo need to group all order items if there exists a record in the items_units with the same item_code and with [default_sprd_sht] set to true
This would look like so
(Sum ({order_items.qty}, {order_items.unit}) * {items_units.amount}) / (get amount for unit with the same item_code and [default_sprd_sht] = 1)
I have two problems accomplishing this.
How to check if this order item has a unit with same item_code and
[default_sprd_sht] = 1?
How to further group order items only if there is a unit with same
item_code and [default_sprd_sht] = 1?

Crystal Report Parameter Array Index value

I have a Crystal Report with a formula on the page header like this:
dim t as string
t = "DD Waiver Waitlist "
if {rpt_xtab_waitlist.CountyName} = "statewide" then
t = t + {rpt_xtab_waitlist.CountyName}
else
t = t + "for " + {rpt_xtab_waitlist.CountyName} + " County"
end if
formula = t
The problem is not all counties have data for the report, but I still want the page header to contain the county name.
Ultimately, other staff puts a front-end on the report with a combo box for the parameter listing all of the counties, so the parameter UI will not come from Crystal. I can't just change the parameter to dynamic.
I thought I could use the county name from the parameter instead of the data, but that's in an array and I can't figure out how to index it.
I tried:
if {rpt_xtab_waitlist.CountyName} = "statewide" then
t = t + {?County}( RecordNumber)
else
t = t + "for " + {?County}( RecordNumber) + " County"
end if
but it doesn't print the county name corresponding to the selected county in the group tree.
How should I index a parameter array in a formula?
Thanks for any help,
-Beth
Sample county names:
Anoka
Hennepin
Yellow Medicine
Output I get now:
report with page header function suppressed when they select Yellow Medicine county as their parameter from the list of counties
Output I want to get:
report with page header function returning "DD Waiver Waitlist for Yellow Medicine County"
when Yellow Medicine county selection criteria applied returns 0 rows.
I don't think there is a useful index, and for our purposes, they can only select one county as a parameter, so I ended up using the (1) index of the parameter array.
If anyone knows of a way to derive a meaningful index, please post.