I am busy with my SQL Final exam preparation
My question is this, I have to create a View to display products bought Today and I must display the product name and price and customer name
my code is
USE pre_prac
GO
CREATE VIEW vw_Today
AS
SELECT Customer.custName, Product.prodName, Product.prodPrice, Invoice.invDate
FROM Customer
JOIN Product
ON Customer.custName = Product.prodIden
JOIN Invoice
ON Product.prodName = Invoice.invDate
WHERE Invoice.invDate = GETDATE()
GO
Is my code wrong or what am I missing
I think you need proper join with the corresponding table column(i mean relation with customer table to others table). it would be easy to submit write code if you provide your table schema. so far from my understanding below code may be your helpful
USE pre_prac
GO
CREATE VIEW vw_Today
AS
SELECT Customer.custName, Product.prodName, Product.prodPrice, Invoice.invDate
FROM Customer
JOIN Product
ON Customer.prodIden = Product.prodIden
JOIN Invoice
ON Product.invoiceId = Invoice.invoiceId
WHERE Invoice.invDate = GETDATE()
GO
The problem is with the JOIN How can Customer.custName = Product.prodIden and Product.prodName = Invoice.invDate
This is the error, you should have a relation based on an Integer column, for example Table Product should contain a FK from table Customer called CustomerId , etc!
Related
Create a view named customer_mobile_details which has the following attributes. Display customer id,customer name,mobile number, sales id, net amount,model name and manufacturer name of the mobiles, they have purchased. Sort the records based on customer id,customer name,sales id in ascending order.enter image description here
My code is as below.
create view customer_mobile_details
as( select Customer_Info.Customer_ID, Customer_Info.Customer_Name,
Distributor.Mobilenumber,
Sales_Info.Salesid, Sales_Info.Net_Amount,
Mobile_Master.Model_Name, Mobile_Master.Manufacturer
from Customer_Info
inner join Sales_Info
on Customer_Info.Customer_ID = Sales_Info.Customer_ID
inner join Mobile_Master
on Sales_Info.Price = Mobile_Master.Price
inner join Distributor
on Mobile_Master.Distributor_ID = Distributor.Distributor_ID)
order by Customer_Info.Customer_ID, Customer_Info.Customer_Name, Sales_Info.Salesid asc;
But i'm getting some error. It says
Failed Test
Test Case 2: Check the attribute name,constraints,sorting etc.
Can someone help me to figure it out where I made the mistake?
You can try this one. Works for me.
This is pretty self explanatory.
create view customer_mobile_details as
(select customer_id,
customer_info.customer_name,
customer_info.mobile,
sales_info.salesid,
sales_info.net_amount,
mobile_master.model_name,
mobile_master.manufacturer
from customer_info
join sales_info using(customer_id)
join mobile_master using(ime_no))
order by customer_id,
customer_info.customer_name,
sales_info.salesid;
The issue is actually in the question as the mobile number to be taken is the attribute named "Mobile" from the table 'Customer_info' and not the "mobilenumber" attribute from table 'Distributor'.
I have two tables:
A Billing table, and a Customer table.
The Billing table and customer table both share a common attribute of Customer Number.
Billing Table
I'm trying to create a view that will retrieve the customer code and bill number for the most recent invoice date. I'm having trouble ordering my query.
This is what I have so far.
CREATE VIEW RECENT_ORDER
AS
SELECT
c.Customer_Num, b.Bill_Num
FROM CUSTOMER c
INNER JOIN BILLING b ON c.Customer_Num = b.Customer_Num
WHERE c.Fname='Jess' AND c.Lname='Hanks'
HAVING MAX(b.Bill_Date);
I have also tried putting the 'HAVING' portion as a WHERE statement.
This should get your answer:
CREATE VIEW RECENT_ORDER
AS
SELECT c.customer_num, b.bill_num
FROM customer c
JOIN billing b ON c.customer_num = b.customer_num
WHERE b.bill_date =
(SELECT MAX(bill_date) FROM billing WHERE customer_num = b.customer_num)
AND c.Fname='Jess' AND c.Lname='Hanks'
Though normally I wouldn't expect to create a view that limits results to just one customer!? Remove the last line AND c.Fname ... if you intend to get the most recent result for any/all customers. Note that you may get multiple results for a customer if they have more than one invoice on the same date.
I'm trying to create a view for my report for
the Staff Sales.
My Staff Fields are
Staff_ID - PRIMARY KEY
Staff_Name
My Sale Fields are
Sale_Date
Payment_ID#
Staff_ID#
Sale_ID - PRIMARY KEY
I'm trying to calculate how much sales each member of staff has made, How would I go about this? I have tried this but as you can see I'm not very good at views in sql.
SELECT tblSale.Staff_ID,
SUM(tblSale.Sale_ID*tblSale.Staff_ID) AS
SalesPerStaff FROM tblSale
INNER JOIN tblStaff ON tblSale.Sale_ID = tblStaff.Staff_ID
GROUP BY tblSale.Sale_ID
A view is just the same as a standard piece of T-SQL, it's just stored in the database ready to be called when you need it
CREATE VIEW MyViewName
AS
SELECT
st.Staff_ID
,st.Staff_Name
,COUNT(sa.Sale_ID) Sales
FROM Staff st
LEFT JOIN Sale sa
ON st.Staff_ID = sa.Staff_ID
GROUP BY
st.Staff_ID
,st.Staff_Name
That way, you can just call MyViewName and it will give you results. I've used a LEFT JOIN in case you have staff with no sales.
If your Sales table has a value column, you can change COUNT(Sale_ID) to SUM(Sales_Value) or whatever the field is called.
Just read your reply to main question. If your Sales table has something like 'Item_ID' then just add this after st.Staff_Name in the select list (remember to add to the end of the GROUP BY too).
Sorry for the bad title, if you can think of a better one, let me know.
Many-to-many relationship using tables.
Product
ProductCategory
Category
In the ProductCategory table i have boolean column primarycategory
Each product must have a primary category.
I want to find all products in my database which don't have a primarycategory.
Note: I have assumed field names in tables other than the one you specified.
This should return a distinct list of product IDs that have no primary category. Bit fields in SQL server are numeric, so you can give them to the max() function.
select
pc.product
from
ProductCategory pc
group by
pc.product
having
max(pc.primarycategory) = 0
The above query assumes that all products have at least one category. If not, try the following:
select
pc.product
from
Product p
left join
ProductCategory pc on p.id = pc.product
group by
pc.product
having
max(isnull(pc.primarycategory, 0)) = 0
Assuming true = value 1, try this:
Select Product From Product p
Where Not Exists (Select * From ProductCategory
Where Product = p.Product
And primarycategory = 1 )
but if you have control over this database, Move the PrimaryCategory column to the Products table, (and populate it with the category identifier itself, not a boolean), that is where this belongs in a properly normalized schema...
i have a complex query to be written but cannot figure it out
here are my tables
Sales --one row for each sale made in the system
SaleProducts --one row for each line in the invoice (similar to OrderDetails in NW)
Deals --a list of possible deals/offers that a sale may be entitled to
DealProducts --a list of quantities of products that must be purchased in order to get a deal
now im trying to make a query which will tell me for each sale which deals he may get
the relevant fields are:
Sales: SaleID (PK)
SaleProducts: SaleID (FK), ProductID (FK)
Deals: DealID (PK)
DealProducts: DealID(FK), ProductID(FK), Mandatories (int) for required qty
i believe that i should be able to use some sort of cross join or outer join, but it aint working
here is one sample (of about 30 things i tried)
SELECT DealProducts.DealID, DealProducts.ProductID, DealProducts.Mandatories,
viwSaleProductCount.SaleID, viwSaleProductCount.ProductCount
FROM DealProducts
LEFT OUTER JOIN viwSaleProductCount
ON DealProducts.ProductID = viwSaleProductCount.ProductID
GROUP BY DealProducts.DealID, DealProducts.ProductID, DealProducts.Mandatories,
viwSaleProductCount.SaleID, viwSaleProductCount.ProductCount
The problem is that it doesn't show any product deals that are not fulfilled (probably because of the ProductID join). i need that also sales that don't have the requirements show up, then I can filter out any SaleID that exists in this query where AmountBought < Mandatories etc
Thank you for your help
I'm not sure how well I follow your question (where does viwSaleProductCount fit in?) but it sounds like you will want an outer join to a subquery that returns a list of deals along with their associated products. I think it would go something like this:
Select *
From Sales s Inner Join SaleProducts sp on s.SaleID = sp.SaleID
Left Join (
Select *
From Deals d Inner Join DealProducts dp on d.DealID = dp.DealId
) as sub on sp.ProductID = sub.ProductID
You may need to add logic to ensure that deals don't appear twice, and of course replace * with the specific column names you'd need in all cases.
edit: if you don't actually need any information from the sale or deal tables, something like this could be used:
Select sp.SaleID, sp.ProductID, sp.ProductCount, dp.DealID, dp.Mandatories
From SaleProducts sp
Left Join DealProducts as dp on sp.ProductID = dp.ProductID
If you need to do grouping/aggregation on this result you will need to be careful to ensure that deals aren't counted multiple times for a given sale (Count Distinct may be appropriate, depending on your grouping). Because it is a Left Join, you don't need to worry about excluding sales that don't have a match in DealProducts.