Complicated query - help NEEDED! - sql

really hope someone can help me on this one!
I have 6 tables:
Products
prodid
Prodequipment
prodid
equipclassmain
equipclassor
Equipclasses
classid
Equipfunctions
equipid
classid
Equipment
equipid
Workshopequipment
workshopid
equipid
Products – a list of some products
Equipment – a list of some equipment
Prodequipment – lists what equipment is needed to do a product. You can use equipment listed in equipclassmain or replace it by equipment in equipclassor. Table Products has one to many relation to table Prodequipment, i.e. you will use many different tools (equipment) to produce one product, but you can choose to use anyone in the pair equipclassmain/equipclassor. For instance to frame a photo you can use a wooden frame or plastic frame (one pair) and a cover glass or cover plastic (second pair). You can combine it as you wish, but both pairs should be used: wooden frame with cover glass or plastic frame with cover glass or wooden frame with plastic cover or plastic frame with plastic cover.
Equipfunctions and Equipclasses – Because one piece of equipment can be used in different ways it is not linked directly to table Prodequipment. I have created table Equipclasses where all single use of every possible equipment is listed and table Equipfunctions where I list those single uses for every equipment.
Workshopequipment – lists workshops and equipment they are using.
Now I need a list of products which can be manufactured by two different given workshops (let's say workshopid = 1 and workshopid = 4), i.e. both those workshops have all equipment needed to produce those products. Bear in mind that those workhops don't have to use the same equipment to do so as I described above.
I'm trying with this query:
SELECT prodid FROM Products JOIN (
SELECT workshopid, prodlist, equipclassmain, equipclassor,
if( LOCATE( equipclassmain, prodlist ) >0
AND LOCATE( equipclassor, prodlist ) >0, 1, 0 ) AS pairstatus FROM Prodequipment JOIN
(
SELECT classid FROM Equipclasses JOIN (
SELECT classid FROM Equipfunctions JOIN (
SELECT workshopid, GROUP_CONCAT( equipid ) AS prodlist FROM Workshopequipment
GROUP BY workshopid
)
equipfunclist GROUP BY equipid
) equipclasslist GROUP BY classid
) WorkshopequipmentList HAVING pairstatus = 1 AND workshopid in (1, 4)
) prodbyworkshops ON classid = equipclassmain OR classid = equipclassor
But I get an "Column classid in field list is ambiguous". Any idea what's wrong here?
THANK YOU!

Your query references multiple tables that have a classid column.
Therefore, when you reference classid in the HAVING clause, it doesn't know which table to get the classid for.
You need to write tablename.classid where tablename is the name of the table containing the classid column. (Probably equipclasslist; I didn't look at the query)

It's from the part of the query with:
...
SELECT classid
FROM Equipclasses JOIN (
SELECT classid
FROM Equipfunctions...
Both EquipClasses and the inner query have a classId. You need to specify which you're selecting.

Related

Query to select person on a table that has a class name on another table

I've been at this for a while.
Basically it's a query that needs to identify:
"In which courses do people who only drink one type of beer teach?
(Name of course (s))"
Table 1 : The relation K contains information about courses: their course code (KKOD), course name (NAME), number of points (POINTS) and course abbreviation (NICKNAME).
Table 2: The relationship PB contains information on people's total beer consumption divided into different varieties: the person's name (NAME), Systembolaget's article number (SYSNR) and the number of liters consumed (LITER).
There's more tables but I believe only those two are needed to respond to the question.
First I wanted to write a query that identifies the people who only have drunken one type of beer.
select * from pb group by namn having count(*)=1;
Then I tried to write a query combining the above query to list the courses but here's where nothing happens or if I do some variations, all of the courses will list, not just the ones I need.
Try 1: select namn from pb group by namn having count(*)=1 where exists(select knamn from K where pb.namn = k.knamn);
Try 2: select knamn from k where exists(select namn from pb by group by namn having count(*)=1);
Doesn't work, any recommendations?
Thank you
The description of the tables seem to be lacking an ability to join them. Let's just assume you have a person_id or name in both tables.
This is the question:
"In which courses do people who only drink one type of beer teach? (Name of course (s))"
You can use a CTE to first obtain the names of people who drink only one type of beer, and use in a query such as this:
with beer_types as
(
select person_name
from beers
group by person_name
having count(distinct beer_type) = 1
)
select c.person_name, c.course_code
from courses c
join beer_types bt
on c.person_name = bt.person_name
DB-Fiddle found here.

Draw data from two tables

I'm learning SQL on Test sites and can not get past this question. (Test Dome)
Each item in a web shop belongs to a seller. To ensure service quality, each seller has a rating.
The data are kept in the following two tables:
TABLE sellers
id INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL,
rating INTEGER NOT NULL
TABLE items
id INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL,
sellerId INTEGER REFERENCES sellers(id)
Write a query that selects the item name and the name of its seller for each item that belongs to a seller with a rating greater than 4. The query should return the name of the item as the first column and name of the seller as the second column.
I've tried the following which has gotten me closest of my attempts:
SELECT items.name, sellers.name
FROM sellers, items
WHERE sellers.rating > 4;
Thanks in advance for bothering with my noob question.
You were almost there but you forgot to specify the join condition, without which would be a cross join (all possible combinations of both table rows)
SELECT items.name, sellers.name
FROM sellers, items
WHERE sellers.rating > 4
AND items.sellerId = sellers.id;
In a more extended form the above should be the same as below
SELECT items.name, sellers.name
FROM sellers
INNER JOIN items
ON sellers.id = items.id
WHERE sellers.rating > 4;
You want to join the tables.
Here's a link to the tutorial on w3s, this topic is explained quite well here: https://www.w3schools.com/sql/sql_join.asp

linking child tables of two related parent-child tables

I have two parent child tables and a couple of supporting tables. I am struggling to figure out the right relationships between them.
Orders, a table that holds a list of Orders including:Order Id, Supplier, Order Date etc..
OrderDetails, a Table holds a list of products that have been ordered, and is a child of Orders, linked on OrderId. Key fields are the ProductId and the Quantity Ordered. Each order containes one or many OrderDetails that outline the products and quantities being ordered.
Shipments, a table that holds a list of Shipments, including Tracking Number and Shipper. Links to Orders through Order Id. An Order may have multiple shipments. For example, I ordered 100 lightbulbs. They were dispatched in 5 shipments.
ShipmentDetails, holds a list of product and shipped quantities and is linked to the Shipments table by ShippingId. Each Shipment may have multiple ShipmentDetails. i.e. One shipment may have 30 Lightbulbs and 10 Door Knobs.
Products is a table that holds a list of Products that need to be both ordered and shipped.
So the logic is that I enter details about the products that are to be ordered in Products. For example, 100 CREE LED Lightbulbs, and 50 Door Handles.
When I place an order, I create an Order in Orders.
i.e. amazon.com, order #45454.
Then I add child rows to that order in OrderDetails. i.e. 30 CREE LED Lightbulbs.
When the order Ships, I create an entry in the Shipments table. i.e. Tracking #46464646464, linked to OrderId in Orders. And then I enter what is in that shipment in ShipmentDetails. For example, only 20 or the 30 CREE LED Lightbulbs may be associated with this Shipping entry.
I am struggling with figuring out how to relate the Shipping Detail records to the Order Detail Table. Lets say the Shipping Detail Table has 4 fields.
ShippingID - a link to the Shipments table parent.
TrackingNum - a field that holds a tracking number.
Product - this should be a drop down, that says, the shipment I belong to is related to a defined Order (because ShipmentDetail is a child of a Shipment record which in turn holds a key to the Orders table, which in turn has OrderDetails that reference products).
Quantity - this should have a default (overridable) value that is the "Quantity Ordered" number from the OrdersDetail record that matches the Order Id and Product Id. Where OrderId is in the Shipments table (linked to the Orders table) and ProductID comes from #3 above. The reason it must be overridable is that a shipment may be a partial shipment.
I am stuck with the preceding #3 and #4. I hope I have explained this in a vaguely understandable way! The pictures below may help!
The ask:
What is the correct join between the ShipmentDetails and the OrderDetail Table
How do I create a field in the ShipmentDetail table with a default value pulled from the Quantities Field of the OrderDetail table, where
Shipments!OrderId = Orders!Id and ShipmentDetail!ProductID = OrderDetails!Product ID
I am working in MS Access 2016 - but I suspect this is a fairly generic SQL question...rather than MS Access specific.
I think the naming is confusing.
What I would rather have is (each first id is autoincrement, forgot how to say this on access):
// Product doesn't contain any information about quantity/price etc ...
create table Products(product_id int, name text, description text ...);
// Orders:
create table Orders(order_id int, client_name text, description text ...);
create table OrderDetails(order_detail_id int, order_id int, product_id int, quantity double, unit_price double, ...);
// Shipments
create table Shipments(shipment_id int, company text, description text ...);
create table ShipmentDetails(shipment_detail_id int, shipment_id int, product_id int, quantity double, price double default 0, ...);
// inserting shipments per product (defaulting to total quantity per product), assuming we have shipment_id SID
insert into ShipmentDetails(shipment_id, order_id, product_id, quantity)
select SID, order_id, product_id, SUM(quantity)
from OrderDetails
group by order_id, product_id;
Then you can of course have some filters (date, customer etc ...).
For the first question, I am not clear what exaclty you want to return.
Here is a comparison of quantities:
select t.order_id, t.product_id, sum(t.quantity) as product_quantity, sum(u.quantity) as shipment_quantity
from OrderDetails t inner join ShipmentDetails u
on t.order_id = u.order_id and t.product_id = u.product_id;

Correlated Sub queries using MAX and GROUP BY

I have a database with the tables: VEHICLES (a Blue Honda Civic costing $30k), BRAND (Honda) AND MODEL (Civic).
I am trying to create a correlated sub query which will give me the VEH_IDs for the cars with the highest price from each BRAND.
Simply, I want to know for all the brands which car costs the most.
I've attached a picture with the SQL im trying but it's not working. Most likely because I have no idea what im doing.
SELECT
BRAND.BRAND_ID, BRAND_NAME, VEHICLE.MODEL_ID, VEH_ID
FROM BRAND, MODEL, VEHICLE
WHERE VEH_PRICE =
(
SELECT MAX(VEH_PRICE)
FROM VEHICLE
)
GROUP BY BRAND.BRAND_ID, BRAND_NAME, VEHICLE.MODEL_ID, VEH_ID;
create a new query on the vehicles table grouping by brandID to determine the max(price)
SELECT brandID, MAX(VEH_PRICE)
FROM VEHICLE
group by brandID
then create another query that uses the first one joined back to vehicles to determine the related vehicleID
SELECT
v.BRAND_ID, MODEL_ID, VEH_ID
FROM VEHICLE v inner join
( SELECT brandID, MAX(VEH_PRICE) as max_veh_price
FROM VEHICLE
group by brandID) m on
v.brandid = m.brandid and
v.veh_price = m.max_veh_price
then, to get the brand_name, join again to your brand table on the brandID field, and yes, if max(price) returns more than one vehicle, you'll have to go with Top 1 at Roman suggests.
sorry, couldn't read your images at first.
yes, you need to join the model table to vehicle to get to the brandID. I'm assuming this is an exercise and you're supposed to learn about joins as a result? don't just take a solution, then, understand each piece individually.

SQL Access with Table

It wont let me upload image but columns are OrderID, CustomerName, CustomerAddress, ProductNumber, SellDate, ProductDescription
I am trying to teach myself SQL. Could someone please help me identify a few things?
1) I want to write a SQL statement that retrieves the customer name and address of the customer that placed order 7.
Is this right?
Select CustomerName, Address
From Order
Where OrderID = ‘7’
2)Next I want to write an SQL statement that adds a new order to the Order table.
Is this right?
INSERT INTO order(OrderID, CustomerName, CustomerAddress, ProductNumber, SellDate, ProductDescription)
VALUES (8, 'Ben C', '12 Kents Road', 01/15/2012, Clay :));
3) What is wrong with this data model and how would you redesign it? I really need help here. Does it need to be sorted? How could I describe a new high level design?
4) How would I move this data from an old model to a new model?
5)Using the new data model, I need to write a JOIN that retrieves the customer name and address of the customer that placed order 7. I have not gotten here yet because I am not sure why the old data model is bad.
First, you need to answer a question:
Can a customer place more than one order? If your answer is 'yes', would you like to have a customer catalog?
In this scenario, you need to normalize your database. First of all, you need to separate the data into logical sets; in this case, Customers, Products and Orders... I will asume that an order can have one or more products.
Then, design your tables (I will use MySQL style for the code):
Your customers catalog:
create table tbl_customers (
customerId int not null primary key,
customerName varchar(100),
customerAdress varchar(200)
);
Your products catalog:
create table tbl_products (
productNumber int not null primary key,
productName varchar(100),
);
Your orders catalog:
create table tbl_orders (
orderId int not null primary key,
orderDate date,
customerId int unsigned not null
);
For each order, you will need to know how many 'units' of which products you will be ordering:
create table tbl_orders_products (
orderProductId int not null primary key,
orderId int not null,
productNumber int not null,
units int,
);
After this, you will populate your tables with your data, and then you can perform whichever query fits you.
A few notes:
tbl_orders is related with tbl_customers... your customer's data will have to be inserted in tbl_customers before he can place an order.
Before you insert the order's details, you will need to create the order
Before you insert the order's details, you will need to populate tbl_products
This is just a way to solve it.
Hope this helps you
Now, if you want to move to this model, you have some work to do:
Populate your products catalog: insert into tbl_products values (1,'productA'), (2, 'productB'), ...
Populate your customers catalog
Then you can start placing your orders. I'll asume that you have the following customers:
customerId | customerName | customerAdress
---------------------------------------------
1 | John Doe | 31 elm street
2 | Jane Doe | 1223 park street
... and products:
productNumber | productName
------------------------------
1000 | Pencil
2000 | Paper clip
3000 | Bottled water
Now, placing an order is a two-step process: first, create the order record, and then insert the order details:
The order (Customer John Doe): insert into tbl_orders values (1, '2012-10-17', 1);
The order details (one pencil, ten paper clips): insert into tbl_orders_products values (1, 1, 1000, 1), (2, 1, 2000, 10);
Now, to select the customer for order seven (as stated in your question), you can use the following query:
select c.*
from tbl_customers as c
inner join tbl_orders as o on c.customerId = o.customerId
where o.orderId = 7;
This is just a start point. You should look for good books or online tutorials (w3 tutorials can be a good online 'place' to start).
Although I don't quite like MS Access, it's a good program to learn the basics of sql, and there're a lot of books and learning resources for it. Note: I don't like Access, and I don't mean to advertise it, but it might be a good learning tool.
First you need to normalise, there 's a lot of stuff around that, but loads of tutorials that try and take some common sense and make it really obscure
Looking at your column names I see three tables
Customers(CustomerID, CustomerName, CustomerAddress)
CustomerOrders(OrderID, CustomerID, SellDate, ProductNumber) Try not to name your tables and columns and such the same as Sql keywords.
Products(ProductNumber, ProductDescription)
Normalisation says things like, you should be able to uniquely identify any records in the table, you had that with OrderId. When I split the tables up I added CustomerID, because you could have more than one customer with the same name.
Another simple rule is in your structure, if you had more than one order for a customer, you would be storing their name and address more than once, which is wasteful, but the real problem, is what if that customer changes address? How do you find which rows to change, you could do Where name = "Fred" and Address = "Here", but you don't know if you have more than one customer called Fred with an address of Here.
So you first query would be a join
Select Customers.CustomerName,Customers.CustomerAddress From Customers
Inner join CustomerOrders On Customers.CustomerID = CustomerOrders.CustomerID
Where CustomerOrders.OrderID = 7
Or if you want to skip past learning joins for now, you could do it with two queries
Select CustomerID From CustomerOrders Where OrderID = 7
then
Select CustomerName,CustomerAddress From Customers Where CustomerID = ?
You should be using joins, but you might find sub-query a little easier to get your head round. You can do both queries at once with
Select CustomerID From CustomerOrders
Where CustomerID In (Select CustomerID From CustomerOrders Where OrderID = 7)
Don't know far you've got with sql table creation, but Primary and Foreign keys is two things to look at. That will let you put some rules in the database. A primary Key on CustomerOrders will stop you having two orders with the same ID, which would be bad.
A foreign Key would stop you creating a CustomerOrder for a customer that did not exist, and one to products for a product that doesn't.
Assuming you went down that route and you were looking to write an application to order things.
You'd probably have a function to maintain Customers which would add them with something like
Insert Into Customers(CustomerID,CustomerName,CustomerAddress) Values(1,'Fred Bloggs','England')
And one For Products
Insert Into Products(ProductNumber,ProductDescription) Values(1,'A thingamabob')
Then you'd choose a customer, so you have it's id, select a product so you have it's number, so you don't need to mess with CustomerName, CustomerAddress or ProductDescription
and
Insert Into CustomerOrders(OrderID,CustomerID,ProductNumber,SellDate) Values(1,1,1,'20121017')
Note the date format, if you are going to pass dates as strings to sql, (another topic this) do them in YYYYMMDD, when you get them back out with a select, you'll get them in the format your machine/database is set up for which in your case seems to be mm/dd/yyyy. The problem is I deduced that because I know there are only twelve months in the year. (One of the things that makes me a senior dev :) If your example selldate had been 1/5/2012, I'd have read that as the 1st May, because I'm configured for English. Avoid that ambiguity at all costs it will reach out and hurt you on a regular basis.
PS the way you did it 1/15/2012 would be treated as a mathematical expression as in 1 divided by 15 ...
So the reason you couldn't write a join is basically you only had one table. Join is join one table to another. Well actually it's a bit more complex than that, but that's a good way past where you are in the learning curve.
As for moving the data, be quicker to start again I should think. Unlikely you have created two different customers with the same name, but the queries to move the data, would have to take into account that you could have.
To move the data, assuming CustomerID is an Identity (Autonumber) column
Something like
Insert into Customers(CustomerName,CustomerAddress)
Select Distinct CustomerName,CustomerAddress From [Order]
Would do the job for Customers.
Then for products
Insert into Products(ProductDescription)
Select Distinct ProductDescription From [Order]
Then
Insert into CustomerOrders(OrderID,CustomerID,ProductNumber,SellDate)
Select old.OrderID,c.CustomerID,p.ProductNumber,old.SellDate
From [Order] old
Inner Join Products p On p.ProductDesription = old.ProductDescription
Inner Join Customers c On c.CustomerName = old.CustomerName And c.CustomerAddress = old.CustomerAddress
might do CustomerOrders I think
A simple tip. When modelling a data solution, try to write down simple sentences that describe the scenario. For example (ok, it is just a basic one):
An order is made up of many order lines
An order line refers a product
A customer create many orders
Here, the nouns describe the entities of your scenario. Then, for each entity, try to describe its property:
An order is characterized by a unique serial number, a date, a total. It refers a customer.
An order line refers to a product, and is characterized by a quantity, a unit price, a sub total
A customer....
An so on.
Well, in your model you roughly have to create a table for each entity. The table fields are taken from the property of each entity. For each field remeber to define the proper data type.
Ok, this is NOT a modelling tutorial, but it is a starting point, just to approach the solution.