I have to create a view with following attributes - sql

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'.

Related

selecting different data types to create a view

I am trying to create a view called customersummary that will have the customerID, firstname,lastname and the paper descriptions they are subscribed to but it gives me the error conversion failed when converting the varchar value 'Journal'to data type smallint. I am not sure how to fix this
any help would be appreciated.
Create view CustomerSummary
as
Select distinct customer.customerID,firstname,lastname from customer
inner join customerpaper on customer.customerID = CustomerPaper.PaperID
inner join paper on customerPaper.PaperID = Paper.Description
The datatypes of the tables you are joining on don't match. You can convert or cast them in your view though.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15
But it sounds like your joining on odd fields. I think you should check what the foreign keys are, I would imagine it would look something like this:
Create view CustomerSummary
as
Select distinct Customer.customerID, firstname, lastname from Customer
inner join CustomerPaper on Customer.customerID = CustomerPaper.CustomerID
inner join Paper on CustomerPaper.PaperID = Paper.ID
The error comes out from the last line of query:
customerPaper.PaperID = Paper.Description
I think the Paper.Description Column should replace with some columns like Paper.Id. Or, if the Paper.Description Column really stores id for papers, you need to convert it to the "smallint" data type. Try CONVERT(SMALLINT, Paper.Description).

SQL Create View displaying items sold Today

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!

Using two tables to get one report in SQL Server

I have two tables, product and download as follows.
product (product_id (pk), product_name)
download (download_date(pk), download_version(pk), product_id(pk,fk))
I need a report to show how many downloaded, form which version of what product took place in each month.
SELECT
[product_name],
[version],
MONTH(download_date) AS [Month],
COUNT(MONTH(download_date)) AS [Count]
FROM
product
INNER JOIN
download ON product.product_id = download.product_id
GROUP BY
MONTH(download_date)
and I get this error
Column 'product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Use alias names for the tables for better readability.
Mention the aliasname.columnname in the SELECT to avoid fetching the wrong values.
You missed the other columns except the aggregate values in the GROUP BY.
So the query below will return the result.
SELECT P.[product_name],
P.[version],
MONTH(D.download_date) AS [Month],
COUNT(MONTH(D.download_date)) AS [Count]
FROM product P
INNER JOIN download D ON D.product_id = P.product_id
GROUP BY P.[product_name], P.[version], MONTH(D.download_date)
You have some issue with tables and primary key.
create the table like this.
product(product_id(PK),name,verion)
download(date,product_id)
and run this query
SELECT product.name,product.version,COUNT(download.product_id)
FROM product INNER JOIN download ON product.product_id=download.download_id
Group BY(download._productid);
i think this is what you want if not post replay i will answer when i get back to stack.

Querying records that meet muliple criteria

Hi I’m trying to write a query and I’m struggling to figure out how to go about it.
I have a suppliers table and a supplier parts table I want to write a query that lists suppliers that have specified related Parts in the supplier parts table. If a supplier doesn’t have all specified related parts then they should not be listed.
At the moment I have written a very basic query that lists the supplier if they have a related supplier part that meets the criteria.
SELECT id ,name
FROM
efacdb.dbo.suppliers INNER JOIN [efacdb].[dbo].[spmatrix] ON
id = spmsupp
WHERE spmpart
IN ('ALUM_5083', 'ALUM_6082')
I only want to show the supplier if they have both parts related. Does anyone know how I could do this?
Use a subquery with counting distinct occurences:
select * from suppliers s
where 2 = (select count(distinct spmpart) from spmatrix
where id = spmsupp and spmpart in ('ALUM_5083', 'ALUM_6082'))
As a note, you can modify your query to get what you want, just by using an aggregation:
SELECT id, name
FROM efacdb.dbo.suppliers INNER JOIN
[efacdb].[dbo].[spmatrix]
ON id = spmsupp
WHERE spmpart IN ('ALUM_5083', 'ALUM_6082')
GROUP BY id, name
HAVING MIN(spmpart) <> MAX(spmpart);
If you know there are no duplicates, then having count(*) = 2 also solves the problem.

Creating views in SQL and receiving error

I know that this is a simple fix, but I can't seem to figure out how to change the code so I don't get this error:
ORA-00918: column ambiguously defined
Here is the code:
CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS
SELECT BRANCH_NUM, ON_HAND
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;
Thanks in advance for the help.
I'm guessing that you need to specify from which table you want BRANCH_NUM like:
CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS
SELECT BRANCH.BRANCH_NUM, ON_HAND
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;
Also, you might want to consider using the post ANSI SQL-92 syntax for joins like so:
CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS
SELECT BRANCH.BRANCH_NUM, ON_HAND
FROM BRANCH
INNER JOIN INVENTORY ON BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;
See [Bad habits to kick : using old-style JOINs][1] for some reasoning about it.
[1]: https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins
you are making SELECT BRANCH_NUM, ON_HAND without determine those columns from which tables. May be one of them exists in both tables, modify it for example to
SELECT INVENTORY.BRANCH_NUM, INVENTORY.ON_HAND
-- ...
of those columns from inside INVENTORY table).
CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS
SELECT BRANCH.BRANCH_NUM, INVENTORY.ON_HAND
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM, INVENTORY.ON_HAND
ORDER BY BRANCH.BRANCH_NUM;
PS this query does not make much sense - why are you grouping? Also you are joining two tables for no apparent reason. The only thing this does is exclude invalid BRANCH's