Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
CREATE TABLE DimProduct1 (
Prod_ID INT NOT NULL PRIMARY KEY,
Prod_Name VARCHAR(255) NOT NULL,
Prod_Quantity INT NOT NULL,
Prod_Cost NUMERIC(18,2) NOT NULL );

INSERT INTO DimProduct1 (Prod_ID, Prod_Name, Prod_Quantity, Prod_Cost)
VALUES
(201, 'Hammer', 10, 20.00),
(202, 'Nails', 20, 5.00),
(203, 'Lumber', 50, 5.00),
(204, 'Premium Paintz', 10, 25.00);

Select * From DimProduct1;


CREATE TABLE DimSupplier (
Sup_ID INT NOT NULL PRIMARY KEY,
Prod_ID INT NOT NULL REFERENCES DimProduct1(Prod_ID),
SupAmountDue NUMERIC(18,2) NOT NULL,
Sup_DueDate DATE NOT NULL,
Items_Ordered INT NOT NULL );

INSERT INTO DimSupplier (Sup_ID, Prod_ID, SupAmountDue, Sup_DueDate, Items_Ordered)
VALUES
(301, 201, 150, '2022-06-05', 10),
(302, 202, 250, '2022-06-15', 15),
(303, 203, 100, '2022-06-30', 10),
(304, 204, 160, '2022-07-05', 10);

Select * From DimSupplier;


CREATE TABLE DimCustomer1(
Cust_ID INT NOT NULL PRIMARY KEY,
Cust_FirstName  VARCHAR(255) NOT NULL,
Cust_LastName VARCHAR(255) NOT NULL,
Cust_Address VARCHAR(255) NOT NULL,
Cust_City VARCHAR(255) NOT NULL,
Cust_State VARCHAR(255) NOT NULL,
Cust_Zip INT NOT NULL, 
Cust_Phone BIGINT NOT NULL,
Cust_Balance NUMERIC(18,2) NOT NULL );

INSERT INTO DimCustomer1 (Cust_ID, Cust_FirstName, Cust_LastName, Cust_Address, Cust_City, Cust_State, Cust_Zip, Cust_Phone, Cust_Balance)
VALUES
(1111, 'John', 'Smith', '123 FLUSHING AVE', 'FLUSHING','NY', 10018, 7182095454, 1500.00),
(2222, 'Virat', 'Kohli', '1456 Springfield Avenue', 'Treeview', 'NY', 10018, 7188123789, 1500.00),
(3333, 'Jessica', 'Rodriguez', '702 Oak Drive', 'Glenhead','NJ', 02159, 4153551709, 1800.00),
(4444, 'Henry', 'Tom', '455 Grace Ave', 'Bayside', 'NY', 11784, 7138238000, 2500.00);

Select * From DimCustomer1;


CREATE TABLE DimJob(
Job_ID INT NOT NULL PRIMARY KEY,
Cust_ID INT NOT NULL REFERENCES DimCustomer1(Cust_ID),
JobStartDate DATE NOT NULL,
JobEndDate DATE NOT NULL,
HoursSpent NUMERIC(18,2) NULL,
HourlyRate NUMERIC(18,2) NOT NULL );

INSERT INTO DimJob (Job_ID, Cust_ID, JobStartDate, JobEndDate, HoursSpent, HourlyRate)
VALUES
(801, 1111, '2022-04-01', '2022-04-20', 100, 50),
(802, 2222, '2022-04-21', '2022-05-10', 80, 40),
(803, 3333, '2022-05-11', '2022-05-30', 70, 50),
(804, 4444, '2022-06-01', '2022-06-20', 100, 75);

Select * From DimJob;


CREATE TABLE DimTime (
Time_ID INT NOT NULL PRIMARY KEY,
OrderDate DATE NOT NULL,
Quarter_Of_Date INT NOT NULL,
Month_Of_Year INT NOT NULL,
Year_Of_Date INT NOT NULL);

INSERT INTO DimTime (Time_ID, OrderDate, Quarter_Of_Date, Month_Of_Year, Year_Of_Date)
VALUES
(1279, '2022-02-01', 1, 2, 2022),
(1280, '2022-04-15', 2, 4, 2022),
(1281, '2022-07-30', 3, 7, 2022),
(1282, '2022-06-15', 2, 6, 2022),
(1283, '2022-07-18', 3, 7, 2022),
(1284, '2022-04-28', 2, 4, 2022);
 
Select * From DimTime;


CREATE TABLE Job_Fact (
Job_ID INT NOT NULL REFERENCES DimJob,
Sup_ID INT NOT NULL REFERENCES DimSupplier(Sup_ID),
Prod_ID INT NOT NULL REFERENCES DimProduct1(Prod_ID),
Time_ID INT NOT NULL REFERENCES DimTime(Time_ID),
Num_ItemsOrdered NUMERIC(18,2) NOT NULL,
Total_Cal_Amnt NUMERIC(18,2) NOT NULL );

INSERT INTO Job_Fact (Job_ID, Sup_ID, Prod_ID, Time_ID, Num_ItemsOrdered, TotaL_Cal_Amnt)
VALUES
(801, 301, 201, 1279, 0, 0),
(802, 302, 202, 1280, 0, 0),
(803, 303, 203, 1281, 0, 0),
(804, 304, 204, 1282, 0, 0);

Select * From Job_Fact;

/*
List the names of the customers who were provided bids last month.
My Query is below showing all the customers had bids but it needed customer who had bids last 
month only. What I am Doing wrong here? 

SELECT DATEADD(MONTH, -1, Bids.BidStartDate) AS Cust_total, S=DimCustomer1.Cust_FirstName, S=DimCustomer1.Cust_LastName, Bids.Cust_ID 
FROM Bids
INNER JOIN Customer1 ON Customer1.Cust_ID = Bids.Cust_ID
ORDER BY DATEADD(MONTH, -1, Bids.BidStartDate), Customer.Cust_FirstName, Customer.Cust_LastName
*/

What I have tried:

<pre>List the names of the customers who were provided bids last month.
My Query is below showing all the customers had bids but it needed customer who had bids last 
month only. What I am Doing wrong here? 

SELECT DATEADD(MONTH, -1, Bids.BidStartDate) AS Cust_total, S=DimCustomer1.Cust_FirstName, S=DimCustomer1.Cust_LastName, Bids.Cust_ID 
FROM Bids
INNER JOIN Customer1 ON Customer1.Cust_ID = Bids.Cust_ID
ORDER BY DATEADD(MONTH, -1, Bids.BidStartDate), Customer.Cust_FirstName, Customer.Cust_LastName
*/

/*
List the names of the suppliers and the total amount owed to any unpaid suppliers
Then write a separate query to display the days past due for each supplier.
Again My query below is showing all the suppliers

SELECT SUM(Sup_AmtDue) AS Total_Amount,Sup_Name
FROM DimSupplier
GROUP BY (Sup_Name);

SELECT Sup_Name,Sup_DueDate 
FROM DimSupplier 
WHERE  Sup_DueDate < GETDATE();
*/

/*
Write an insert statement or series of insert statements if necessary,
to insert data into the required tables when a bid is entered.

I M NOT SURE WHAT TO QUERY TO WRITE here

Write a delete statement(s) to delete a particular customer 
from the customer table (make sure to delete any corresponding rows in other tables).

DELETE FROM DimCustomer1 WHERE Cust_ID = 1111;
*/
Posted
Updated 5-Aug-22 19:01pm

1 solution

Quote:
SELECT DATEADD(MONTH, -1, Bids.BidStartDate) AS Cust_total, S=DimCustomer1.Cust_FirstName, S=DimCustomer1.Cust_LastName, Bids.Cust_ID
FROM Bids
INNER JOIN Customer1 ON Customer1.Cust_ID = Bids.Cust_ID
ORDER BY DATEADD(MONTH, -1, Bids.BidStartDate), Customer.Cust_FirstName, Customer.Cust_LastName
You need to add a WHERE clause to check the Bid Date is with the last month - just an ORDER BY won;t do that as it doesn't exclude any rows.
SQL WHERE Clause[^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Aug-22 4:01am    
S=DimCustomer1.Cust_FirstName, S=DimCustomer1.Cust_LastName
Are those "S=" sequences supposed to be there?
OriginalGriff 6-Aug-22 4:27am    
Nope - I thought I told it to put it as a quote, it's a copy from the OP homework instructions, not a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900