How do I make a shopping list from multiple recipes in an SQL database? - sql

I'm working on using a recipe database in SQLw, like the one in this question (which has helped a lot already) Structuring a recipe database , to combine the ingredients of several user selected recipes to a shopping list.
Also, the items on this shopping list are to be divided in two categories (eg: "groceries" and "check pantry")
Example case:
User can select 7 recipes to make a weekly mealplan in a form (almost got this part)
The given output is a shopping list of all the ingredients marked as "groceries" and a "check stock" list of all the ingredients marked as "pantry".
Any help at all would be much appreciated!

I had just posted a full solution, but given the subject here looks like it may be homework, I'm just going to point you in the right direction. If this isn't homework, leave a comment and I'll put the full solution back.
Since you have multiple recipes, a normal selection based on joins would give you back multiple rows per ingredient. You want some way to roll up all of the rows for a given ingredient into a single row and show a total of the quantity that you need.

Related

Excel - extracting values from data table for list validation

Background:
I have a workbook/tool that performs exactly as I wish.
The tool includes two main components:
A list of ingredients grouped by category (i.e., dairy, produce, etc.)
a sheet with recipe tables
On the ingredient sheet, all ingredients of a specific category are grouped together as part of a named range (i.e. "dairy_list").
[Named Rage: Dairy_list]
[Ingredient] [Unit] [Price]
Milk, 2% 1 litre $2.50
Milk, Whole 1 litre $3.50
[/Named Rage: Dairy_list]
[Named Rage: Fruit_list]
[Ingredient] [Unit] [Price]
Apples pound $0.99
Bananas pound $1.99
[/Named Rage: Fruit_list]
Recipe tables include a row for each ingredient (note: there may be hundreds of ingredient rows once the model is complete). In each row are two columns, from the first contains a list validated drop down menu containing the names of all categories, the second is a list validated drop down containing all ingredients that reside in the selected category. I do this using the named ranges and the INDIRECT([category cell]&"_list") function for the list validation. So if the category "Dairy" were selected, the validation list would be the range "Dairy_list".
This works; however, some challenges exist from a usability stand point.
Challenges:
The users of this tool are not experienced with Excel. Users must be able to easily add rows to each category and add categories to the model.
Explaining how to define a named range, insert rows within a range and ensuring ranges' naming conventions are valid are all relatively simple tasks for experienced users but not for many of the intended users of the tool.
Users also may wish to sort the ingredient list in different ways (using auto filter), which is not possible when the table is divided into subgroups and categories.
I am rebuilding the model template to allow for this usability and to simplify the user experience.
Objective:
The format of the ingredient data table will be single, sortable table:
[Category] [Ingredient]
Fruit Apple
Fruit Banana
Vegetable Carrot
Fruit Melon
A named range of possible categories will be contained on a separate sheet.
Ideally, the user need only add a category to the category list or add an ingredietn to the ingrdient list, with no further steps required other than to populate recipes.
The user should be able to add (within reason) as many categories as they want. Similarly, on the ingredient sheet, the user may add (within reason) as many ingredients for each category and in any order they wish. This is important because the solutions I have found on this site and others for dynamic validation lists typically involve creating an additional list/named range that groups list items by category then applies a named range to each list.
[Fruit] [Vegetables]
Apple Carrot
Banana
Melon
This approach, while functionally relevant, is essentially a less elegant version (the data must reside in multiple locations) of what my current model uses and one which I am trying to avoid (I want to avoid as many steps as possible for the user).
What I wish the model to do in the recipe tables is, wherever the category cell contains [Fruit] the ingredient cell only lists items from the ingredient list where the [category] is Fruit. But not require the data to be manually extracted or grouped within the ingredient list.
I've experimented with VBA auto-filtering, but I do not want to alter the filter, format or ordering settings on the ingredient list if the user has sorted a certain way for usability.
If I were constructing this in another programming language that referenced a database, the equivalent functionality would be a SQL statement like "SELECT [ingredient] FROM [Ingredient_List] WHERE [Category] = 'Fruit'".
I am open to VBA or non-VBA solutions (preferably ones that are relatively backward compatible as I do not have control over Excel version).
Thank you in advance for any thoughts/direction/resources/solutions.

Searching products using details.name and details.value using the Best Buy API

The Best Buy Search allows to search products specifying a criterion on details.name and details.value fields.
http://api.remix.bestbuy.com/v1/products(details.name="Processor Speed" & details.value="2.4Ghz")?apiKey=YOURKEY
However details is a collection. The query above actually returns all products has a detail entry named "processor" and a detail entry whose value is "2.4Ghz" but not necessarily in the same details entry. Is there a way to create a query that will return only products for which those value and name are for the same details entry ?
Unfortunately there is no way to do this unless the particular detail you are interested in has been exposed as a top level attribute (processor speed has not). To accomplish this you will need to run your query as you have described, and then comb through the results and remove the irrelevant products in your own code.

having an order with multiple requests

i want to do a autoshop software... where they keep up the cars they have and what they need(engine and other parts for example) but i dont know how to do the database to accept multiple items at once
example:
a car needs on one visit to the auto shop:
left frontal door
tires
oil change
filters
how to i add this in one go to the database(with prices included) so that i can see it all after and print a bill wheer it shows all... but my main priority is being able to insert all in one go and in one table
Hard to tell without any idea about your db strucutre. Lets assume db isn't constructed yet, you don't want to decrease parts stock or keep any track of wich exact part (i mean with serial etc.) was used. You want it quite simple, just a table with a car bought some parts.
In this case i woulde use a table looking like this : id|date|car_id|parts_used
where parts_used is a string containing parts and prices with separators. For example : "left frontal door=500+tires=100+oil=10" and then split the string when reading db.
I'm not sure it's what you want but your question isn't quite precise :)

Designing database

I am finding it difficult to decide on an efficient design of the database. My application would get a number of ingredients(table) from the user and check with the database to find the recipe that could be prepared from the list of ingredients that the user provides.
My initial design is
Useringredients(ing_id,ing_name..);
the recipe database would be
recipe(rec_id,rec_text,...);
items_needed(rec_id,item_id,...);
items(item_id,item_name);
Is this a good way ? If so how will i be able to query to retrieve the recipes from the list of user ingredients.
Help would be very much appreciated.
This design could work. You have one table recording recipes, one recording items and one recording the many-to-many relationship between the two (though I would work on your naming conventions to keep things consistent).
To get any recipes that contain at least one item in your list, you could use the following:
Select rec.rec_id,
Count(itn.item_id) as [NumMatches]
From recipe as rec
Join items_needed as itn on itn.rec_id = rec.rec_id
Where itn.item_id in (comma-delimited-list-of-itemIDs)
Group By rec.rec_ID
Having Count(itn.item_id) > 0
Order By Count(itn.item_id) desc
This returns any recipes that contain at least some of the items that are selected, sorted with the first recipes having the highest number of matches.
The following query should give you a list of unique recipes using any one of the ingredients the user searches for
select distinct rec_id,rec_text,ii.item_name
from recipe rr
join items_needed itn on itn.rec_id=rr.rec_id
join items ii on ii.item_id=itn.item_id
join userIngredients ui on ui.ing_id=ii.item_id
Yaakov's query looks like it will handle the situation where you want all ingredients. You might be able to replace (comma-delimited-list-of-itemIDS) with (select ing_id from userIngredients)

Need Help Writing SQL To Apply Promotions to Shopping Basket at Checkout

Don't laugh but I'm a Lotus Notes (non-relational database!) developer trying to work with SQL and, although I have the basic concepts nailed, I'm stuck on something I'd consider to be "advanced".
Imagine a user reaches an online checkout having added a set of products to their basket. I need to apply promotions to the basket.
These promotions look at the items in the basket and add "points" for any combination that matches a pre-defined "bundle". The promotions also need to be able to target users in specific countries (information gained at point of registration) and other personal details.
The promotions are entered and maintained by the site admin team and need to be as flexible as possible. So they can reward people for things like "Buy X products of type Y and get 50% extra points" or "3 or more XE-123s and get 500 points added" etc.
Right now I'm looking for general direction. How should I store the criteria that matches the items in a basket to any of the running promotions? Would one big Stored Procedure do or should the C# code that builds the basket loop through all promotions and see which fit?
Right now I don't even have a table schema. Just the knowledge of how it should work and little idea where to start.
Jake
My suggestion is to not use SQL for this sort of business logic.
The database is a good place to keep information about products like whether they are type Y or type X. This keeps the database design pretty straightforward.
What you mention about C# seems like a better direction. There is a lot of searchable information about 3-tier architecture that can help explain the benefits of this strategy well.
'As flexible as possible' is a red flag (IMHO). I'd try to nail that down to:
"Fixed-point and/or percentage (of total basket / bundle points) bonus (three columns in a helper table)
When the basket contains a combination that matches a pre-defined 'bundle', where 'bundle' is contained in a helper table, with multiply rows, with a bundleID and a row for each item in the bundle, containing at least ItemID and Quantity.
And no other kinds of reward possible. This to keep the project / requirement manageable.
Then have a SP which checks for the presence of bundles within the basket and applies relevant promotions (as stored in the first helper table).
Also make sure you know the requirement whether 1 or multiple promotions are possible.