Insert all combinations of two tables in intermediate table in SQL Server - sql-server

My situation
I'm making a site where people can reserve meeting rooms. On the reservation form for a meeting room, I've some optional field sets like below:
ID | Name
-- | ------------
1 | Catering
2 | Coffee break
3 | Drinks
Here are some example meeting rooms:
ID | Name | Location
-- | ------- | ---------
1 | Dog | Brussel
2 | Cat | Antwerpen
3 | Chicken | Brugge
4 | Cow | Gent
I'm using Microsoft SQL server 2016.
Database structure
The fieldsets from the first code block stands inside my database in the table reservationFieldsets.
The meeting rooms stands in the meetingRooms table.
There is an intermediate table named meetingRoomsReservationFieldsets.
Question
Now I'll fill meetingRoomsReservationFieldsets with all rooms and all the fieldsets like below:
RoomID | FieldsetID
------ | ----------
1 | 1
2 | 1
3 | 1
4 | 1
1 | 2
2 | 2
3 | 2
4 | 2
1 | 3
2 | 3
3 | 3
4 | 3
I've tried
I've tried to do it manually but there are a lot of rooms and too much to do that manual.

I've found it by the comments on the question. I use this code:
INSERT INTO meetingRoomsReservationFieldsets
SELECT meetingRooms.id, reservationFieldsets.id
FROM reservationFieldsets
CROSS JOIN meetingRooms

Related

Product Based Inventory Implementation

I'm working on a small inventory database project on Access 2016.
I have a problem implementing a Product table and a ProductUnit table where the following business rules are true:
One, two, or many products may be put in a special package to form a new product.
Each special package may be composed of a single product or another special-package product which itself may contain many products.
My implementation of the tables is the following:
Product
| ProductID | ProductName | ... |
| 1 | A | ... |
| 2 | A(2set) | ... |
| 3 | B | ... |
| 4 | A(2set) + B | ... |
| 5 | C | ... |
ProductUnit
| ProductID | ProductUnitID | Qty |
| 2 | 1 | 2 |
| 4 | 2 | 1 |
| 4 | 3 | 1 |
The ProductUnitID is the product or special product that makes up a specific ProductID.
Recall that a product may be composed of many products. Therefore, there may be multiple ProductUnitID's for each ProductID in the ProductUnit table.
Now, let's say that I have a Sample table where I keep a record of my products in stock.
Sample
| ID |ProductID | Qty |
| 1 | 5 | 3 |
| 2 | 4 | 1 |
| 3 | 1 | 1 |
How would I generate a query to count my inventory products assuming that I want products such as ProductID = 2, ProductName = A(2set) to be count as two products of ProductID = 1, ProductName = A?
The same would apply for ProductID = 4, ProductName = A(2set) + B where I would have one A(2set) product and one B product and the one A(2set) product will have two A products.
Additional Notes: You may disregard the following
I'm thinking of recursive queries(if there is such a thing) that would just keep breaking each special product into products until each product is just a single product. A single product does not appear in the ProductUnit table.
However, I highly doubt this is possible.

oracle database, Omitting records from a table which are subset of records in same table based on two columns

I have some customers which are clustered into groups by cluster_id and the amount of data is huge (So performance is a matter here). The simplest form of what I have is the following table:
cust_id | cluster_Id
---------- | -----------
1 | 1
2 | 1
3 | 1
2 | 2
1 | 2
2 | 3
4 | 3
1 | 4
I want those clusters with greatest number of customers such that no costumer be removed. In other words, I want to delete records of a cluster that is subset of another cluster.
In above example, the output table should look like this:
cust_id cluster_Id
-------- | ----------
1 | 1
2 | 1
3 | 1
2 | 3
4 | 3

records exist or not based on conditions in sql server 2008

Hi I have doubt in sql server
Trantable:
empid | deptid | Projectname | Transactionid
1 |10 | test | 1
2 |11 | test1 | 2
2 |10 | jai | 3
2nd table: dimemp ....> here dimemp is scdtype2 dimension.its all ready done
empkey | empid | ename | flag
1 | 1 | a | 1
2 | 2 | b | 1
3 | -1 | na | 1
3rd table: dimdept------>here dimdept is scdtype2 dimension.implementaion allready done.
deptkey | deptid | deptname | flag
1 | 10 | hr | 1
2 | 11 | ceo | 1
3 | -1 | NA | 1
Here I want load trantable data into facttran table with corresponding keys. here transactionid is unique column
to identiy unique record.
Facttran table structure look like below and factran we need to maintain scd type1 data.
empkey | deptkey | projectname |transactionid
I tried like below query
merge into facttran target
using (select ISNULL(a.empkey, (select empkey from Dimemp where empid = -1)) empkey,ISNULL(b.deptkey, (select deptkey from dimdept where deptid = -1)) deptkey, c.projectname, c.transactionid
from trantable c
left join dimemp a on a.empid=c.empid and a.flag=1
left join dimdept b on b.deptid=c.deptid and b.flag=1)source
on target.transactionid=source.transactionid
when not matched
then insert ([deptkey],[empkey],[projectname],[transactionid])
values(source.deptkey,source.empkey,source.projectname,source.transactionid)
when matched
then update set target.empkey=source.empkey ,
target.deptkey=source.deptkey,
target.projectname=source.projectname,
target.transactionid=source.transactionid ;
then I got output like below
Table :facttran
empkey | deptkey | projectname |transactionid
1 | 1 | test | 1
2 | 2 | test1 | 2
2 | 1 | Jai | 3
upto now its working fine.
2nd day in my trantable few records updated and few records insert in sourc trantable.based on below table data I want update in facttable with corresponding key.
2nd table: dimemp ....> here dimemp is scdtype2 dimension
empkey | empid | ename | flag
1 | 10 | a | 0
2 | 11 | b | 1
3 | -1 | na | 1
4 | 10 | aaa | 1
3rd table: dimdept------>here dimdept is scdtype2 dimension.implementaion allready done.
deptkey | deptid | deptname | flag
1 | 10 | hr | 0
2 | 20 | ceo | 1
3 | -1 | NA | 1
4 | 10 |hrdept | 1
Trantable:
empid | deptid | Projectname | Transactionid
1 |11 | test | 1 ------record updated in source side here deptid changed from 10 to 11
1 |11 | test123 | 2 -------Here empid changed from empid 2 to 1 and projectname changed test1 to test123
2 |10 | jai | 3 ------here no records are not changed
1 |10 | cod | 4 ----------new rocrd is came
based on above trantable.I want facttran table data look like below.
Table :facttran
empkey | deptkey | projectname |transactionid
1 | 2 | test | 1
1 | 2 | test123 | 2
2 | 1 | Jai | 3
4 | 10 | cod | 4
when I ran 2nd time with same query.I am not able get to expected result.
here mainily source trantable related transactioni id is exist or not in facttran table .if not exist then we need to insert correspondig dimensionkeys with lates flaf=1
values.if we found transactionid exist in fact table then we need to updated existing dimension corresonding key.
suppose if we take transactionid=1 records here only chnaged deptid not empid that time we donot need update empid corresponding lates flag=1 corresondingkey
we need check exisig transaction id is updated each dimension need to check exist or not if not exist latest flag=1 related corresponding key.
if exist we donot need to updated that one.if new reocrds came then we need to insert with latest flag=1 corresponding keys in factran table.
please tell me how to write query to achive this task in sql server.

Database design for eCommerce Site with filters

I am doing a project on eCommerce website, but I am unable to figure out, how to design the database schema for it. In this website, there will be products from several categories and each category has a unique set of filters for it.
Example: The 'Laptop' category has filters such as 'RAM', 'Hard disk', 'Graphics Card', whereas, in the 'Shirt' category filter is 'Fabric Type' and many more.
I have thought of 2 different schema, but I am not sure of which one to be used.
Design #1
categories
---------
id
category
filters (Here filters will be stored as hard-coded JSON)
products
--------
id
category_id
name
price
specifications (Stored as JSON which will be filters using values from categories.filters)
Design #2
categories
----------
id
category
table_name
products
--------
id
category_id
name
price
laptops
-------
id
product_id
ram
hard_disk
shirts
------
id
product_id
fabric
sleeve
Which design should I choose? or is there any better design for it?
EDIT
Here, my problem is not about multiple categories but, to integrate filters. For example if I check 'RAM 4 GB' in Laptop category, then it should show all products from Laptop category with 4 GB RAM. I can't make one table for each filter, or category as there will be more than 100 of them.
I am following #Tim Biegeleisen's design, but rather than putting everything in one table. I modified it to separate categories, products and filters.
categories
+----+----------+-----------+
| id | category | parent_id |
+----+----------+-----------+
| 1 | Computer | NULL |
+----+----------+-----------+
| 2 | Laptop | 1 |
+----+----------+-----------+
| 3 | Desktop | 1 |
+----+----------+-----------+
| 4 | Clothing | NULL |
+----+----------+-----------+
| 5 | T-Shirt | 4 |
+----+----------+-----------+
| 6 | Shirt | 4 |
+----+----------+-----------+
products
+----+-------------+-----------------+-------+
| id | category_id | name | price |
+----+-------------+-----------------+-------+
| 1 | 2 | Acer Aspire | 600 |
+----+-------------+-----------------+-------+
| 2 | 3 | Dell All-in-One | 750 |
+----+-------------+-----------------+-------+
| 3 | 6 | Lee Marc Shirt | 50 |
+----+-------------+-----------------+-------+
| 4 | 5 | Nike T-Shirt | 100 |
+----+-------------+-----------------+-------+
filters
+----+-------------+-----------------+------------+-------------+
| id | filter | value | product_id | category_id |
+----+-------------+-----------------+------------+-------------+
| 1 | RAM | 4 GB | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 2 | Battery | Li-ion | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 3 | HDD | 500 GB | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 4 | RAM | 16 GB | 2 | 3 |
+----+-------------+-----------------+------------+-------------+
| 5 | HDD | 1 TB | 2 | 3 |
+----+-------------+-----------------+------------+-------------+
| 6 | Fabric | Cotton | 3 | 6 |
+----+-------------+-----------------+------------+-------------+
| 7 | Sleeve | Full | 3 | 6 |
+----+-------------+-----------------+------------+-------------+
| 8 | Size | M | 4 | 5 |
+----+-------------+-----------------+------------+-------------+
| 9 | Color | Black | 4 | 5 |
+----+-------------+-----------------+------------+-------------+
To get all filters for a category:
SELECT DISTINCT `filters`.`filter` FROM `filters` WHERE `filters`.`category_id` = 2
To know all available RAM sizes for Laptop category:
SELECT DISTINCT `filters`.`value` FROM `filters` WHERE `filters`.`category_id` = 2 AND `filters`.`filter`='RAM'
category_id in filters table can be excluded as we can get that from products table. I have added it because every time I have to get all filters, I don't have to get through products table.
I would approach your problem by treating your entire catalog as a tree, where each record represents a relationship between a parent category and a child category/product.
Products:
+----+-----------+-----------+
| id | name | child_id |
+----+-----------+-----------+
| 1 | laptops | 2 |
| 1 | laptops | 6 |
| 2 | RAM | 3 |
| 2 | RAM | 4 |
| 2 | RAM | 5 |
| 3 | RAM - 1GB | null |
| 4 | RAM - 2GB | null |
| 5 | RAM - 4GB | null |
| 6 | hard disk | null |
| 7 | shirts | 8 |
| 8 | fabrics | null |
| 9 | desktops | 2 |
| 9 | desktops | 6 |
+----+-----------+-----------+
The "root" categories are those which have no parents. Here is how you would query all the root categories:
SELECT DISTINCT(name)
FROM Products
WHERE id NOT IN (SELECT DISTINCT child_id FROM Products)
Using my contrived example this would return three id values, 1, 7, and 9, for laptops, shirts, and desktops, respectively. If you then wanted to get all parts for laptops you could use this query:
SELECT child_id FROM Products WHERE id = 1
This would return 2 and 6 for the RAM and hard disk categories. You can continue querying deeper into the tree as needed. For example, if you wanted to get all RAM products, you could do this query:
SELECT id, name FROM Products WHERE id IN (3,4,5) AND child_id IS NULL
Note carefully the use of IS NULL to identify that the record be a product and not a category, since it has no more children.
I believe that even massive e-commerce sites like Amazon and Overstock do not have a hierarchy which is more than 5-10 levels, even in the deepest cases.

Do Subselects make SQlite ignore distincts?

we are facing a strange behaviour in SQLite (Version 3).
We have a table for Vehicles with two columns referencing an engine and a gear.
Of course there could be more than one vehicle with the same engine gear combination.
I now want to find the distinct combination of engines and gears of the vehicles (and use it for an insert => thats why randomblob(36)).
Example:
Vehicle | EngineId | GearId
-----------------------------
1 | 1 | 1
1 | 1 | 2
1 | 2 | 1
1 | 2 | 2
1 | 1 | 2
1 | 1 | 2
The following select statement results in too many rows:
Select randomblob(36), tmp.EngineId, tmp.GearId from (Select distinct EngineId, GearId from tblVehicle order by EngineId, GearId) as tmp;
RandomId| EngineId | GearId
-----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
5 | 1 | 2
6 | 1 | 2
But the expected result would just be:
RandomId| EngineId | GearId
-----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
If I replace the randomblob(36) with a constant, the result is as expected (of course without a random Id).
Select 2, tmp.EngineId, tmp.GearId from (Select distinct EngineId, GearId from tblVehicle order by EngineId, GearId) as tmp;
Can someone explain me this behaviour of SQLite? Is this the expected behaviour?
This is a bug.
I can reproduce this with SQLite 3.6.23.1 but not with 3.7.15, so it has been fixed already.

Resources