How to get the correct table as a Fact Table with relevant keys?(Star Schema) - data-warehouse

I have a problem to select the suitable table for the fact table.
I have problem with following two tables
OrderData Table :
OrderID
CustomerID
OrderStatus
OrderPurchaseAt
OrderAprovedAt
OrderDeliveredCarrier
OrderDeliveredCustomer
OrderestimatedDelivered
OrderItems Table :
OrderID
OrderItemID
ProductID
SellerID
ShippingLimitDate
Price
Frieghtweight
What is the suitable table for fact Table? my data source is https://www.kaggle.com/olistbr/brazilian-ecommerce
Please give me a support.

Let's imagine that you have CustomerID=200 who ordered 2 products ProductID=15 and ProductID=18 with different Sellers, Prices and FreightWeight and same ShippingLimitDates.
Your will have two fact tables one for purchasing :
OrderID CustomerSK OrderItemID ProductSK SellerSK ShippingLimitDate Price Frieghtweight
100 200 1 15 1001 15/02/2020 100 12.20
100 200 2 18 1001 15/02/2020 100 12.20
and one for shipping :
OrderID CustomerSK OrderStatus OrderPurchaseAt OrderAprovedAt OrderDeliveredCarrier OrderDeliveredCustomer OrderestimatedDelivered
100 100 Delivered 14/02/2020 14/02/2020 15/02/2020 16/02/2020 16/02/2020
You model will be like below :

Related

SQLPlus - Stored procedure that displays the number of items sold in descending order

I'm in a bit of a bind - I want to make a stored procedure that displays the number of each item sold and orders them from most to least. The relevant data comes from two different tables:
ITEMS | itemid | itemname
SALES | itemdid
I can display the itemid and count of each item sold with this query:
SELECT * FROM (
SELECT ITEMID,
COUNT (ITEMID) AS COUNTOF
FROM SALES GROUP BY ITEMID
ORDER BY COUNTOF DESC);
I can display the itemname of each item sold with this one:
SELECT ITEMS.ITEMNAME
FROM ITEMS
JOIN SALES
ON SALES.ITEMID = ITEMS.ITEMID;
However, I can not seem to get the itemname to display next to the count of the item sold. I'm really struggling with this. Thanks
What you are looking for is a JOIN or INNER JOIN.
SELECT s.ITEMID,i.ITEMNAME, COUNT (s.ITEMID) AS COUNTOF FROM SALES AS s
INNER JOIN ITEMS AS i ON i.itemid=s.itemid
GROUP BY s.ITEMID,i.ITEMNAME
ORDER BY COUNTOF DESC

Join multiple FK values in one table

I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.

How to query table to get one row requiring two joins to two separate ID?

Oracle 11g
PERSON table contains both seller and buyer ID. How can I get buyer and seller into single result set? I can get results to list either the buyer or the seller but not both.
Person
=================
PersonID First Last
1 Joe Camel
2 Ronald McFly
3 Barbara Wawa
SalesDetail
=========================
TransID Amount SellerID CustomerID
98 500 1 2
99 700 3 1
Desired Result
===========================================
SellerID SellerLast BuyerID BuyerLast Amount
1 Camel 2 McFly 500
3 Wawa 1 Camel 700
Just join to the Person table twice
SELECT sd.sellerID,
seller.last sellerLast,
sd.buyerID,
buyer.last buyerLast,
sd.amount
FROM salesDetail sd
JOIN person seller ON (sd.sellerID = seller.personID)
JOIN person buyer ON (sd.buyerID = buyer.personID)
You may want outer joins if it is possible that either the buyer or the seller is unknown.
try this
select seller.sellerid,
seller.last,
buyer.buyerid,
buyer.last,
amount
from
person buyer
inner join salesdetail on buyer.personid = salesdetail.cutomerid
inner join person seller on salesdetail.sellerid = seller.personid
unable to test myself at the moment

Entity Framework: Storing all contact details in the one table (association,compound FKs)

My database stores three types of user: Agent, Programmer and Employee. All three types of user will have an address, a telephone number, a fax number, etc..., so to minimize table count, I want to create a table tbContactDetails, and use this to store the contacts details of these 3 types of user.
My tbContactDetails, which looks like this:
tbContactDetails
int ContactId (PK, FK)
int ContactType (FK) (1=Agent, 2=Programmer, 3=Employee)
char (50) Addresses etc...
The field ContactType determines the type of owning user.
The Agent table would look like this:
tbAgent
int AgentId (PK)
int ContactId (FK)
int ContactType (FK) (which will always be 1 for an agent)
char(50) Name etc...
So the three tables, tbAgent, tbProgrammer and tbEmployee would all have a FK that matches the tbContactDetails FK.
I can now create a Diagram in Server Explorer, linking the tbContactDetails FK to the tbAgent (FK), the tbProgrammer (FK) and the tbEmployee (FK).
My question is, and some of you may have seen this coming, how do I get Framework4 to recognise this FK2FK relationship, and creating associations for it?
Sorry this question is so long,
Terry.
It sounds like you will be using Table Per Type Inheritance. Have a look at this blog post, especially steps 5 and 6. In your case, tbContactDetails would be the base entity and tbAgent, tbProgrammer, and tbEmployee would be derived entities.

Many-to-many insert using LINQ to SQL and ASP.Net MVC

I have two Tables: Products(ProductID, ProductName) and Categories(CategoryID, CategoryName)
with a many-to-many relationship between Products and Categories ProductsCategories(ProductID, CategoryID)
How Can I Implement a view that enables a user to Add and Edit one product with many categories using Asp.Net Mvc and LINQ to SQL?
Since LINQ to SQL only supports a strict 1:1 mapping with your database you would have to make a link table. This would look something like...
[ProductCategories]
ID
ProductID (FK)
CategoryID (FK)
Then for every relationship of product to categories you would just make a new ProductCategories entity and insert it. So given the data...
Products
ID Name
1 Apple
2 XBOX 360
Categories
ID Name
1 Produce
2 Electronics
3 Game Systems
Your link table would look like
ProductCategories
ID ProductID CategoryID
1 1 1
2 2 2
3 2 3

Resources