I managed to cobble together a join statement to give me a single table that displays my entire database. Now I need to narrow the results down to just the last 30 days, but I cannot for the life of me figure out how to work this restriction into the query.
Right now I have:
select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
from customers
where orderinfo.orderdate = curdate()
inner join orderinfo on customers.id=orderinfo.fk_id
inner join status on orderinfo.fk_code=status.code
order by customers.name;
...in an attempt to just narrow it down to showing me all the records for today, but even that is not working. I see there are a variety of ways of doing the 30-day thing but the part I'm having the most trouble with is just getting the WHERE clause to work at all in this query.
Thanks for any advice!
The WHERE clause needs to go before the ORDER BYand after all joins.
select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
from customers
inner join orderinfo on customers.id=orderinfo.fk_id
inner join status on orderinfo.fk_code=status.code
where orderinfo.orderdate = curdate()
order by customers.name;
You didn't specify the orderinfo in your FROM part.
select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
from customers
inner join orderinfo on customers.id=orderinfo.fk_id
inner join status on orderinfo.fk_code=status.code
where orderinfo.orderdate = curdate()
order by customers.name;
Your inner joins should come before your where clause
select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
from customers
inner join orderinfo on customers.id=orderinfo.fk_id
inner join status on orderinfo.fk_code=status.code
where orderinfo.orderdate = curdate()
order by customers.name;
If you need to do the query with the condition first, then you need subquery
Select * From
(
select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
from customers
where orderinfo.orderdate = curdate()
) customers
inner join orderinfo on customers.id=orderinfo.fk_id
inner join status on orderinfo.fk_code=status.code
order by customers.name;
SELECT customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate
FROM customers
INNER JOIN orderinfo ON customers.id=orderinfo.fk_id
INNER JOIN status ON orderinfo.fk_code=status.code
WHERE orderinfo.orderdate = curdate()
ORDER BY customers.name;
WHERE needs to be after all JOINS and before ORDER BY
Related
I'm using xampp with mysql and have been trying to format a query that uses inner joins on 4 seperate tables. The inner join contains the tables customer, bankaccounts, has and transactions. Has links the bank account and customer tables from M:N to 1:M while transactions is linked to bank accounts. I have attempted the following query:
SELECT t.TransactionID,
t.Description,
t.Amount,
t.isLoan,
t.TransactionDate,
a.AccountID,
a.SortCode,
a.Name,
a.CreationDate,
c.FirstName,
c.LastName
c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a
#inner join will create a new table by combining the values selected based on if they satisfy the on condition below
ON t.TransactionID IN ( SELECT t.TransactionID
FROM transactions
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01'
)
AND t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID ---- > #multiple inner joins can occur on multiple tables
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID;
However this currently only gives an error.
What I need is to link together all the tables while ensuring that only transactions between those specific dates are picked.
Is there any way to solve this?
You should move the condition with the subquery to the WHERE clause:
SELECT t.TransactionID, t.Description, t.Amount, t.isLoan, t.TransactionDate,
a.AccountID, a.SortCode, a.Name, a.CreationDate,
c.FirstName, c.LastName, c.DateofBirth
FROM transactions AS t
INNER JOIN bankaccounts AS a ON t.AccountID = a.AccountID
INNER JOIN has ON has.AccountID = a.AccountID
INNER JOIN customers AS c ON has.CustomerID = c.CustomerID
WHERE t.TransactionDate BETWEEN '2021-11-25' AND '2021-12-01';
I'm pretty new with SQL, and this is giving me trouble. The idea is that I have several tables. Here are the relevant tables and columns:
customers:
customer_id, customer_name
orders:
order_id, customer_id
orderline:
order_id, item_id, order_qty
items:
item_id, unit_price
I need to return customer_name as well as total revenue from that customer (calculated as item_price * order_qty * 2).
Here's what I have written:
SELECT customers.customer_name, sum(revenue)
FROM SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN customers
ON revenue.customer_id = customers.customer_id;
This throws a syntax error and I'm not really sure how to proceed.
This is only one example of this type of problem that I need to work out, so more generalized answers would be helpful.
Thanks in advance!
EDIT:
With help from answers I ended up with this code, which just gets total revenue and puts it next to the first person in the DB's name. What did I get wrong here?
SELECT customers.customer_name, sum(revenue)
FROM(SELECT orderline.order_qty * items.unit_price * 2 AS revenue, orders.customer_id AS CustomerID
FROM( orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN items
ON orderline.item_id = items.item_id)) CustomerOrders
INNER JOIN customers
ON CustomerOrders.CustomerID = customers.customer_id;
A couple issues with your query.
First, you need to scope your subquery and alias it:
(SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id) CustomerOrders
Secondly, you need to select more than the revenue in the subquery since you are joining it to your customers table
(SELECT
orderline.order_qty * items.unit_value * 2 AS revenue,
orders.customer_id AS CustomerId
FROM
orderline
INNER JOIN orders ON orderline.order_id = orders.order_id) CustomerOrders
Then you need to use the subquery alias in the join to the customers table and wrap it all up in a group by customer_id and CustomerOrders.Revenue
I would tend to do it differently. I'd start with selecting from the customer table, because that is the base of what you are looking for. Then I'd do a cross apply on the orders that would all aggregating the order revenue in the subquery. It would look like this (tsql, you could do the same in mysql with a join with some aggregation):
SELECT
customers.customer_name,
ISNULL(customerOrders.Revenue, 0) AS Revenue
FROM
customers
OUTER APPLY (
SELECT
SUM (orderline.order_qty * items.unit_value * 2) AS Revenue
FROM
orders
INNER JOIN
orderline ON orders.order_id = orderline.order_id
INNER JOIN
items on orderline.item_id = items.item_id
WHERE
orders.customer_id = customers.customer_id
) CustomerOrders
In this case, the subquery aggregates all your orders for you and only returns one row per customer, so no extraneous returned data. Since it's an outer apply, it will also return null for customers with no orders. You could change it to a CROSS APPLY and it will filter out customers with no orders (like an INNER JOIN).
SELECT c.customer_name,
sum(COALESCE(ol.order_qty,0) * COALESCE(i.unit_value,0) * 2)
FROM customers c
INNER JOIN orders o
ON o.customer_id = c.customer_id;
INNER JOIN orderline ol
ON ol.order_id = o.order_id
INNER JOIN items i
ON i.item_id = ol.item_id
GROUP BY c.customer_id
select customer_name, sum(item_price * order_qty * 2) as total_revenue
from (
select * from customers
inner join orders using(customer_id)
inner join orderline using(order_id)
inner join items using(item_id)
)
group by customer_name
select
c.customer_name,
r.revenue
from
customers c
inner join
orders ord on
ord.customer_id = c.customer_id
inner join
(select i.item_id, o.order_id, sum(o.order_qty * items.unit_value * 2) as revenue
from orderline o
inner join items i on
i.item_id = o.item_id
group by o.order_id, i.item_id) as r on r.order_id = o.order_id
SelectClassicModels.Orders.CustomerNumber,
ClassicModels.CUSTOMERS.CUSTOMERNAME,
ClassicModels.Employees.LASTNAME,
ClassicModels.Employees.firstNAME,
ClassicModels.employees.EmployeeNumber
from ClassicModels.Orders
join
ClassicModels.Customers
on ClassicModels.Orders.CustomerNumber = ClassicModels.Customers.CUSTOMERNUMBER
join
ClassicModels.EMPLOYEES
on ClassicModels.Employees.EMPLOYEENUMBER = ClassicModels.CUSTOMERS.SalesRepEmployeeNumber
Those three tables when joined work just fine, but when I try and add these modifiers, they don't work
group by ClassicModels.Orders.CustomerNumber
having count(ClassicModels.Orders.CustomerNumber) < 4
First off, welcome to StackOverflow!
I've re-formatted your SQL using my favorite tool, which I can provide a link to if you're interested. I've also added aliases to help make it more "readable". (The aliases are the lowercase bits after the table name in the FROM and JOIN clauses.)
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
Now that we've got that done. Let's add your GROUP BY and HAVING clauses.
GROUP BY clauses have to have ALL of the columns used in the SELECT clause in them. (I'm not sure why. I haven't looked it up, but I just know that's how it works. :) )
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
GROUP BY orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
Now that should work. Then you just have to add your HAVING clause in there.
SELECT orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
FROM CLASSICMODELS.ORDERS orders
JOIN CLASSICMODELS.CUSTOMERS customers
ON orders.CustomerNumber = customers.CustomerNumber
JOIN CLASSICMODELS.EMPLOYEES employees
ON employees.EmployeeNumber = customers.SalesRepEmployeeNumber
GROUP BY orders.CustomerNumber,
customers.CustomerName,
employees.LastName,
employees.FirstName,
employees.EmployeeNumber
HAVING COUNT(orders.CustomerNumber) < 4
I was also looking over your query, and you might get faster (and more efficient results) by using a query like this:
WITH CUSTOMERSWITHLESSTHANFOURORDERS
AS
(
SELECT CUSTOMERNUMBER
FROM CLASSICMODELS.ORDERS
GROUP BY CUSTOMERNUMBER
HAVING COUNT(CUSTOMERNUMBER) < 4
)
SELECT O.CUSTOMERNUMBER,
C.CUSTOMERNAME,
E.LASTNAME,
E.FIRSTNAME,
E.EMPLOYEENUMBER
FROM CUSTOMERSWITHLESSTHANFOURORDERS O
JOIN CLASSICMODELS.CUSTOMERS C
ON O.CUSTOMERNUMBER = C.CUSTOMERNUMBER
JOIN CLASSICMODELS.EMPLOYEES E
ON E.EMPLOYEENUMBER = C.SALESREPEMPLOYEENUMBER;
It uses what is called "common table expression" and basically just isolates a portion of the query. It could be more efficient because it's going to try and group on less data, so it could be faster. Be wary, because there's lots of "could"'s in there because I don't know how various different things are set up in your MySQL database.
Good luck!
I am trying to work out why the following two queries return different results:
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems` it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
and
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
Obviously the difference is the inner join here, but I don't understand why the one with the inner join is returning less results than the one without it, I would have thought since I didn't do any cross table references they should return the same results.
The final query I am working towards is
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems` it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE cf.`fieldid` =5
AND cf.`value`
REGEXP '[A-Za-z]'
AND i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
But because of the different results that seem incorrect when I add the inner join (it removes some results that should be valid) it's not working at present, thanks.
INNER JOIN statement will retrieve rows that are stored in both table of the jion statement.
Try a LEFT JOIN statement. This will return rows that are in first table but not necessary in the second one :
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
LEFT JOIN `tblinvoiceitems` it ON it.userid=i.userid
LEFT JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
INNER JOIN means show only records where the same ID value exists in both tables.
LEFT JOIN means to show all records from left table (i.e. the one that precedes in SQL statement) regardless of the existance of matching records in the right table.
Try LEFT Join instead of INNER JOIN
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
LEFT JOIN `tblinvoiceitems` it ON it.userid=i.userid
LEFT JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
SELECT i.id AS id, i.modify_date as modify_date, s.subscription as subscriptionid, p.paid/i.total AS paidratio
FROM invoices i,
(SELECT p.invoice, sum(amount) AS paid FROM payments p GROUP BY p.invoice) p
LEFT JOIN sub_to_inv s
ON i.id=s.invoice
WHERE p.invoice=i.id
AND i.corporation='3'
AND i.payer=1
The error I get is "unknown column on i.id" which is total bogus - invoices (i) has an id row for sure. They all do.
The purpose of the sub=query is to find out how much of the invoice has been paid. For an invoice that has a "total" column of 1000.00, for example, could have 2 or 3 split payments. What I ultimately wnat to do here is list all the unpaid invoices or partially invoices first. But before I even get to the ORDER BY stage, I need to figure out this error.
Use JOIN syntax for all joins. Don't mix JOIN syntax with the comma-style SQL-89 syntax.
SELECT ...
FROM invoices i
INNER JOIN (SELECT...) p
ON p.invoice=i.id
LEFT OUTER JOIN sub_to_inv s
ON i.id=s.invoice
WHERE
AND i.corporation='3'
AND i.payer=1
Explanation: JOIN has higher precedence than comma-joins. So p JOIN s is evaluated before the query evaluates the join to i. Therefore, in the clause ON i.id=s.invoice, the i table is not yet known and is an invalid reference.
See http://dev.mysql.com/doc/refman/5.1/en/join.html, in the doc following "Join Processing Changes in MySQL 5.0.12".
Can you try this ?
SELECT i.id AS id, i.modify_date as modify_date, s.subscription as subscriptionid, p.paid/i.total AS paidratio
FROM invoices i
LEFT JOIN
(SELECT p.invoice, sum(amount) AS paid FROM payments p GROUP BY p.invoice) p
ON p.invoice=i.id
LEFT JOIN sub_to_inv s
ON i.id=s.invoice
WHERE i.corporation='3'
AND i.payer=1
I think you might be running into issues because of the order of your table joins in your SQL. Have you tried using an inner join. Perhaps try:
SELECT
i.id AS id,
i.modify_date as modify_date,
s.subscription as subscriptionid,
p.paid/i.total AS paidratio
FROM
invoices i
INNER JOIN
(SELECT
p.invoice,
sum(amount) AS paid
FROM
payments p
GROUP BY
p.invoice) p
ON
p.invoice=i.id
LEFT JOIN
sub_to_inv s
ON
i.id=s.invoice
WHERE
i.corporation='3' AND i.payer=1