Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having a little trouble figuring out this SQL statement. I have three tables employee, job, and job type.
job type is reference table. It has the name of the job, how long that job takes, and how much is charged for that job.
job has records of what job was performed for what client by which employee and the date performed. It links to job type jobname.
employee has name of employee, and employee id. It links to job table by employee id.
I need to display a list of all employees as well as the cheapest and most expensive jobs they did for a given year.
I think I will probably have to have an embedded select and a few joins. Maybe a unionThe complexity is just too far out of my experience. I'm not even sure where to being to be honest.
You should use the employee table as the primary table in your query so you will get one row per employee, even if that employee is associated to 0 jobs. Then use outer joins to the job and job_type tables, group by the employee identifiers, and use the min() and max() aggregate functions on the job table to select the min and max job costs (if any).
Something like this should help you get started:
select employee.employee_id ,
employee.employee_name,
min(job_type.cost),
max(job_type.cost)
from employee
left outer join job on job.employee_id = employee.employee_id
left outer join job_type on job_type.job_type_id = job.job_type_id
group by employee.employee_id,
employee.employee_name
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to select data from one table based on a date range of data from another table.
First table: clients with fields firstname, lastname and telephone.
Second table: orders with a field date
I need to get a list of all clients first name, last name and telephone numbers who have orders placed between 2019-12-1 and 2019-12-16.
I've tried many iterations of this mysql select but get way too many hits.
SELECT clients.firstname, clients.lastname, clients.telephone
from clients
JOIN orders ON date BETWEEN '2019-12-1 00:00:00' AND '2019-12-16 23:59:59'
Any insight would be appreciated.
I would assume that the orders has a foreign key column that references the client each order belongs to, and that this column is called client_id. Without this, your schema would not make sense.
I would solve this with exists and a correlated subquery that ensures that the given client placed at least one order during the concerned date range.
select
c.firstname,
c.lastname,
c.telephone
from clients c
where exists (
select 1
from orders o
where
o.client_id = c.client_id
and o.date >= '2019-12-01'
and o.odate < '2019-12-17'
Notes:
this makes use of the half-open strategy to compare the date, which is a good practice in this kind of situation.
this query would take advantage of an index on `orders(client_id, date)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How many products have multiple prices?
Do not count the same product twice.
This question i am struggling with because it almost contradicts itself. If someone could point me in the right direction. I have tried everything.
i have attached an image because i was not sure how to copy and paste my code from mysql
By not counting the same product twice, I think that means only report each product once regardless of whether it has 2, 3, 5 or a million different prices, so don't report every difference, just a summary of which products have multiples. That's just a guess.
Anyway, something like this will list every product that has more than one price:
SELECT productid
FROM mytable
GROUP BY productid
HAVING COUNT(DISTINCT PRICE) > 1;
This will filter out all products with just a single price, leaving only those with more than one.
And if you want to count them, just jam the query above into a subquery:
SELECT COUNT(*) FROM (
SELECT productid
FROM mytable
GROUP BY productid
HAVING COUNT(DISTINCT PRICE) > 1
) multprod;
multprod above is just an alias for the subquery. I added it because I can't remember if MySQL requires one.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i have two table alamat_penerima and lokasi
alamat penerima table
[this is my record
and then
lokasi table [this is the record
i wont to make two table like this
[view]
I hope you can help me
i have just try this query command :
SELECT COUNT(alamat_penerima.`nama_penerima`) AS penerima, lokasi.`wilayah`
FROM lokasi
INNER JOIN alamat_penerima ON alamat_penerima.`kota_kab` = lokasi.`wilayah`
GROUP BY alamat_penerima.`kota_kab`
but the result is
result
Your question is difficult to understand, so this answer is guesswork. It looks to me like you hope to list the number of recipient addresses in each region, but also show the regions with no addresses.
To do that, you need two subqueries joined together.
This subquery generates the names of all regions.
SELECT DISTINCT wilayah FROM lokasi
This subquery generates a list of regions with the count of addresses in each region. The result set from this subquery, however, omits regions with no addresses in them.
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
You can test these subqueries individually to determine whether they are correct. It's important to do that.
Finally, you join these two together as if they were tables. (That's why it's called structured query language).
SELECT w.wilayah, a.num
FROM (
SELECT DISTINCT wilayah FROM lokasi
) w
LEFT JOIN (
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
) a ON w.wilaya = a.wilaya
This will yield what you want, but showing NULL instead of 0 in rows with no addresses. (That's a result of using LEFT JOIN) You can put the 0 values there by using this as the first line of your query instead.
SELECT w.wilayah, IFNULL(a.num,0) num
The design trick is to make your first subquery determine the number of rows in your result set, then to use LEFT JOIN to put information from subsequent subqueries into your results.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Do tables have to be joined to extract data from them?
I believe you mean in one query. There are two ways I can think of to extract data for more than one table in a single query without "joining" them:
Fist is with union
SELECT A, B, C FROM Table1
UNION
SELECT X, Y, Z FROM Table2
You can also do a "cross join" which does not look like a join (and is probably what you are thinking of)
SELECT Table1.A, Table1.B, Table1.C, Table2.X, Table2,Y, Table2,Z
FROM Table1, Table2
As you can see from the syntax there is not relationship from one table to the other. This means that every row in table1 will be combined with every row in table2!
In my experience this is the most common mistake that programmers new to SQL make. They do a cross join when they mean a join and then they GROUP BY or DISTINCT to get the results they want. This is hugely inefficient!
Cross join can be good however, especially when you one of the table has just one row -- then you are adding those values to every row in other table. You are basically selecting single set of values for columns for every row.
Like if you want every row to have the maximum date (this is often done in reports)
SELECT *
FROM Table1, (SELECT MAX(updatedate) as Max_Update FROM Table1) AS MaxDate
Not necessarily, there are other options like Union:
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees
the above example is take from this.
There are other times you can run multiple queries, etc to get data from multiple sources with out a join. It all depends on what you want to do. However, join is a very common--and probably most usual--approach.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working on an assignment dealing with relational mysql databases and I'd like to be able to use my tables to do some math. However after creating my tables and attempting various queries I'm starting to think I've messed the relations up. I have four tables, customer, address, orders, and product. My schema is as follows:
I'm trying to user orders.o_qty to multiply the values in product.p_price and output the totals I have been trying for a while now and either my tables are linked up incorrectly or I'm just not getting it.
o_qty in DDR.orders is a numerical value, I have been trying to use this value in a query to multiply the value stored in DDI.product under p_price and output the total value.
Example rows can be found here:
First I have been trying to work out the total then I will progress to modifying this query to work out total per customer.
This is could be done with joining the tables.
case 1 : Show total amount per user i.e. total purchased per user without drilling down each product
select c.c_id,
c.c_fname,
c.c_lname,
sum(p.p_price) as total
from orders o
join customer c on c.cid = o.c_id
join product p on p.p_id = o.p_id group by c.c_id;
Case 2 : Break per product purchased per customer
select c.c_id,
c.c_fname,
c.c_lname,
p.p_id,
sum(p.p_price) as total
from orders o
join customer c on c.cid = o.c_id
join product p on p.p_id = o.p_id
group by c.c_id,p.p_id;