Help with Grocery List Database design - sql-server

I want to create a Grocery List Table that will accept both the Category and the Specific Products.
For Example: I add to my grocery list:
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Product)
I have Product Table w/ Manufacturer(reference table), Category Table w/ SubCategoryId.
I want the user to enter either the Category or Product into the Grocery List, but have a way knowing that item entered is either a Category or Product. Please advise.
Any help is much appreciated.

Bogdan solution is a very good solution and if i have your problem i would implement his solution, but if you insist on one column in one table for your grocery list, i guess you have to use substring
like e.g
CREATE TABLE grocery_list (
[groceryItem] varchar(100)
);
Insert into grocery_list ([groceryItem]) values ('(c)Fruit')
Insert into grocery_list ([groceryItem]) values ('(p)Fuji Apple')
Insert into grocery_list ([groceryItem]) values ('(c)Shampoo')
Insert into grocery_list ([groceryItem]) values ('(c)Dove Energize Shampoo')
and to access your table substring the first 2 chars to check the type of your item
Select [item] = case when substring(groceryItem,2,1) = 'c' then
right(groceryItem,len(groceryItem) - 3) + ' (Category)'
when substring(groceryItem,2,1) = 'p' then
right(groceryItem,len(groceryItem) - 3) + ' (Product)'
end
from grocery_list
This will give you the below result
**item**
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Category)

I would create two separate columns, one for categories and one for products:
CREATE TABLE grocery_list (
'category_id' INTEGER NULL FOREIGN KEY categories 'id',
'product_id' INTEGER NULL FOREIGN KEY products 'id',
...
);
Entries will only put a value in one of the two columns, leaving the other as NULL. Then you can query your grocery list like this:
SELECT * FROM grocery_list, categories, products
WHERE categories.id=grocery_list.category_id
AND products.id=grocery_list.product_id;

Related

SQL - Remove Duplicates

I've inherited data from another provider and have 2 tables to clean up
Rooms
Items
Items are within a Room and are linked via Rooms.RoomID / Items.RoomID
Each time an Item has been inspected it has created a Duplicate of the Room & Item, so I have 6 records for each Room and each Item (Each with a unique RoomID, so 1 to 1)
My aim is to remove the Duplicate Rooms and keep only the latest set of records, then update the Items table with the RoomID (So I have 1 to Many i.e. 1 Room with 6 Item records)
I can GROUP the Rooms and obtain the MAX RoomID but don't know how to do the UPDATE
A kind of suedo would be this:
UPDATE a
SET a.RoomID = MAX(b.RoomID)
FROM [dbo].[tmp_Items] a
inner join [dbo].[tmp_Rooms] b on b.PropertyID = a.PropertyID
and b.FloorLevel = a.FloorLevel
and b.Reference = a.Reference
The combination of PropertyID, FloorLevel, and Reference provides a unique link between Items and Rooms as these columns are in both tables and Reference is unique to each floor of a property (Each Room of each Floor is numbered starting from 1)
Any help or guidance appreciated :)
Use a subquery to aggregate before joining:
UPDATE i
SET i.RoomID = r.max_roomid
FROM [dbo].[tmp_Items] i JOIN
(SELECT PropertyID, FloorLevel, Reference, MAX(r.RoomID) as max_roomid
FROM [dbo].[tmp_Rooms] r
GROUP BY PropertyID, FloorLevel, Reference
) r
ON r.PropertyID = i.PropertyID AND
r.FloorLevel = i.FloorLevel AND
r.Reference = i.Reference;

Use many to many relationship or multiple columns?

I have a product, which can belong to one (or more) of 5 possible categories.
The number/name/structure of categories will not change
Should I be using a many to many relationship and insert 5 records in the category table, add a join table and add foreign keys etc? Or just add 5 fields to the product table? I feel the latter seems more efficient, but goes against the principles of normalization.
i.e.
Product:
- id
- name
- is_cat_a : bool
- is_cat_b : bool
- is_cat_c : bool
- is_cat_d : bool
- is_cat_e : bool
OR
Product
- id
- name
Category
- id
- name
ProductCategories
- product_id
- category_id
As you suggested:
A product, which can belong to one (or more) of 5 possible categories only.
Then, in my opinion, create two tables:
**1. Category**
Id int,
Name varchar,
IsActive bool and other regular columns
**2. Product**
- id int,
- name varchar,
- cat_a : int,
- cat_b : int,
- cat_c : int,
- cat_d : int,
- cat_e : int
Keep all category column's default as zero.

How to get value from many2one-One2many in (new) product for a calculation in odoo 8?

I've made a custom field in product.template to select a product quality, based on a Many2one relationship with qualities.
qualities_id = field.Many2one(comodel_name="qualities", string="quality")
In the class: qualities there is a One2Many relationship with the class: quality.supplier.
reseller_ids = fields.One2many('supplierinfo', 'product_qual_id', 'Supplier')
Here we can select a quality type e.g. ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26)
product_qual_id = fields.Many2one('qualities', 'Quality Template')
Supplierinfo holds suppliers and each supplier holds more then one quantity and price. There is a relationship between supplierinfo and supplier prices using the field suppinfo_id.
pricelist_ids = fields.One2many('supplierprice', 'suppinfo_id', 'Supplier Price')
Because of this relationship, supplierprice: suppinfo, holds more then one quantity and corresponding price based on supplierinfo_id. Field that holds these records:
suppinfo_id = fields.Many2one('supplierinfo', 'Supplier information')
Qualities = ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26) where Record id: 24 in relation to supplierprice has multi qty and prices:
supplierprice id=30 supplierinfo id=24 qty=200, price=285
supplierprice id=31 supplierinfo id=24 qty=300, price=265
supplierprice id=32 supplierinfo id=24 qty=500, price=248
With additional automation, I need the price from supplierprice, based on (less or more) quantity in the main product. (product.template, price). Can anyone push me in the right direction. How can I get the price from supplierprice into a (new) field in my product.template?
I've added an image of the relationship model.
relationship

Anorm SQL Folding a List into a class result

please pardon the level of detail. I'm not completely sure how to phrase this question.
I am new to scala and still learning the intricacies of the language. I have a project where all the data I need is contained in a table with a layout like this:
CREATE TABLE demo_data ( table_key varchar(10), description varchar(40), data_key varchar(10), data_value varchar(10) );
Where the table_key column contains the main key I'm searching on, and the description repeats for every row with that table_key. In addition there are descriptive keys and values contained in the data_key and data_value pairs.
I need to consolidate a set of these data_keys into my resulting class so that the class will end up like this:
case class Tab ( tableKey: String, description: String, valA: String, valB: String, valC: String )
object Tab {
val simple = {
get[String]("table_key") ~
get[String]("description") ~
get[String]("val_a") ~
get[String]("val_b") ~
get[String]("val_c") map {
case tableKey ~ description ~ valA ~ valB ~ valC => Tab(table_key, description, valA, valB, valC)
}
}
def list(tabKey: String) : List[Tab] = {
DB.withConnection { implicit connection =>
val tabs = SQL(
"""
SELECT DISTINCT p.table_key, p.description,
a.data_value val_a,
b.data_value val_b,
c.data_value val_c
FROM demo_data p
JOIN demo_data a on p.table_key = a.table_key and a.data_key = 'A'
JOIN demo_data b on p.table_key = b.table_key and b.data_key = 'B'
JOIN demo_data c on p.table_key = c.table_key and c.data_key = 'C'
WHERE p.table_key = {tabKey}
"""
).on('tabKey -> tabKey).as(Tab.simple *)
}
return tabs
}
}
which will return what I want, however I have more than 30 data keys that I wish to retrieve in this manner, and the joins to itself rapidly becomes unmanageable. As in the query ran for 1.5 hours and used up 20GB worth of temporary tablespace before running out of disk space.
So instead I am doing a separate class that retrieves a list of data keys and data values for a given table key using the "where data_key in ('A','B','C',...)", and now I'd like to "flatten" the returned list into a resulting object that will have the valA, valB, valC, ... in it. I still want to return a list of the flattened objects to the calling routine.
Let me try to idealize what I'd like to accomplish..
Take a header result set and a detail result set, extract out the keys out of the detail result set to populate additional elements/properties in the header result set and produce a list of classes containing the all the elements of the header result set, and the selected properties from the detail result set. So I get a list of TabHeader(tabKey,Desc) and for each I retrieve a list of interesting TabDetail(DataKey,DataValue), I then extract out the element where the DataKey == 'A' and put the DataValue element in Tab(valA), and do the same for DataKey == 'B', 'C', ... After I'm done I wish to produce a Tab(tabKey, Desc, valA, valB, valC, ...) in place of the corresponding TabHeader. I could quite possibly muddle through this in Java, but I'm treating this as a learning opportunity and would like to know a good way to do this in Scala.
I'm feeling that something with the scala mapping should do what I need, but I haven't been able to track down exactly what.

how to add a drop down list

Report needs option to select multiple super product types
Selection of multiple super product types?
IF #superProductType = 'ALL'
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
ELSE
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND pt_sp_type_c IN (#superProductType)
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
I have a parameter here #super product type,can anyone help me how to change this code
Here in this code i need to make some changes: ---parameter is #superProductType
The above code was for the option to select multiple super product types
when i select All and one value from the drop down list like 'ALL','ASKF'
both the above conditions in the code if else will fail
It should not get selected ALL and other ASKF at the same time
either should has to select
How can we differentiate that ALL not selecting ALL from together
if we select ALL remaining values in the drop down list must be deleted
It should not have two values selected together IF ALL only ALL has to select remaining should discard
I am not sure how to eliminate the rest of values in the drop down list
looking for suitable solution,can anybody see the code abnove and tell me what is the change i have to do in the code.
If you have set the SSRS parameter to Multivalue then you should change the query to use IN (#ParamName) syntax. so your query would become:
IF #superProductType = 'ALL'
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
ELSE
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND pt_sp_type_c IN ( #superProductType )
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'

Resources