I have a problem with SQL Select query. I need to count order's which belong to account which has one or more orders which cost equals 1.
Here is the structure:
Could anyone help with select query. The result should be 2. Many thanks for help.
You have to make two nested queries against the table. An outer one that counts the number of orders for an account and an inner one that finds the accounts that have at least one order with cost equals 1.
SELECT Account_ID, COUNT(*)
FROM Orders
WHERE Account_ID IN (SELECT Account_ID FROM Orders WHERE PRODUCT_Cost = 1)
GROUP BY Account_ID
Related
I am trying to calculate in a SQL statement. I am trying to calculate the total amount in the invoice.total column per customer. I created the following statement:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id;
When I run this, I get the total amount in the table. I have 15 different customers in this table but I get only the name of the first customer and the total amount of all customers. What am I doing wrong?
First, you need to Group the data when you want to have aggregate results:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id
GROUP BY customers.firstname, customers.lastname, customers.status;
Second, are you sure you are joining the table by correct fields? Is invoice.id correct column? I would expect invoice.id to be primary key for the table and instead I would expect another column for foreign key, invoice.customerid for example. Please double check it is correct.
UPDATE: As it was mentioned in comments, if you have two customers with same first name, last name and status, the data will be grouped incorrectly. In that case you need to add unique field (e.g. customers.id) to SELECT and GROUP BY statements.
You have to add a group by customer (id property for example). If you want to have first and last name in select then you will have to group by them as well.
Use group by clause in your query or you can use common table expression to get the sum of records by customer and then make a inner join with customer table.
You need to add a Group By with column names in your query.
Have the need to run a bit more complex of a MySQL query. I have two tables that I need to join where one contains the primary key on the other. That's easy enough, but then I need to find the number of occurrences of each ID returned as well, and ultimately sort all the results by this number.
Normally this would just be a group by, but I also need to see ALL of the results (so if it were a group by containing 10 records, I'd need to see all 10, as well as that count returned as well).
So for instance, two tables could be:
Customers table:
CustomerID name address phone etc..
Orders table:
OrderID CustomerID product info etc..
The idea is to output, and sort the orders table to find the customer with the most orders in a given time period. The resultant report would have a few hundred customers, along with their order info below.
I couldn't figure out a way to have it return the rows containing ALL the info from both tables, plus the number of occurences of each in one row. (customer info, individual orders info, and count).
I considered separating it into multiple queries (get the list of top customers), then a bunch of sub-queries for each order programmatically. But that was going to end up with many hundreds of sub-queries every time this is submitted.
So I was hoping someone might know of an easier way to do this. My thought was to have a return result with repeated information, but get it only in one query.
Thanks in advance!
SELECT CUST.CustomerID, CUST.Name, ORDR.OrderID, ORDR.OrderDate, ORDR.ProductInfo, COUNTS.cnt
FROM Customers CUST
INNER JOIN Orders ORDR
ON ORDR.CustomerID = CUST.CustomerID
INNER JOIN
(
SELECT C.CustomerID, COUNT(DISTINCT O.OrderID) AS cnt
FROM Customers C
INNER JOIN Orders O
ON O.CustomerID = C.CustomerID
GROUP BY C.CustomerID
) COUNTS
ON COUNTS.CustomerID = CUST.CustomerID
ORDER BY COUNTS.cnt DESC, CustomerID
This will return one row per order, displayed by customer, ordered by the number of orders for that customer.
I created two tables. One for employees and the other for stores. I want to count the total employees on the employee table and also count the total of stores on the stores table. Since I know there are less stores than employees, I want to get the total from employees and then divide it by the total of stores. To get an average of employees per store.
This is what I have, and it ain't working.
SELECT COUNT (employee_id) from employees / COUNT(store_id) from stores;
Thanks in advance for any help I get.
You need two selects for the counts respectively, and to wrap them in another select for the division.
SELECT a.count / b.count as employees_per_store
FROM (SELECT COUNT(employee_id) count FROM employees) a,
(SELECT COUNT(store_id) count from stores) b
You need subqueries:
SELECT (SELECT count(*) FROM employees) employees, (SELECT count(*) FROM stores) stores
I've got a problem that can't be new, but I can't figure out how to get the answer I want. It is probably something simple that I'm missing
Using mysql 5.5, I have 2 tables, 'referrals' and 'status'. I want to count referrals that have been cancelled, grouped by appt_date:
SELECT SUM(1) AS count, appt_date FROM referrals
GROUP BY appt_date
JOIN status ON referrals.id=status.referral_id
WHERE status.status_name="cancelled"
This works fine until I have a referral that gets cancelled twice. In other words, a referral with a given id that has 2 rows in the status table with matching referral_id will get counted twice.
How to count each referral record only once when doing the join operation here?
EDIT:
My real question should have been this:
SELECT SUM(quantity) AS count, appt_date FROM referrals
GROUP BY appt_date
JOIN status ON referrals.id=status.referral_id
WHERE status.status_name="cancelled"
since each referral can have its own quantity.
Try changing count to count(DISTINCT id)
So count was only a label, I should have seen that :)
The temptation is to do SUM(DISTINCT quantity) but obviously will just remove dupe "quantities", which isn't what you want. It looks like you will need to join on a derived select table. Have a look at this...
http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables
I have 3 tables:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
Orders have multiple Details rows, a detail row per product.
A detail row has multiple parcels, as 10'000 ordered qty may come from 6 different batches, so goods from batches are packed and shipped separately. The picked quantity put in each parcel for a detail row should then be the same as the ordered_qty.
... hope that makes sense.
Im struggling to write a query to provide summary information of all of this.
I need to Group By customer_id to provide a row of data per customer.
That row should contain
Their total number of orders
Their total ordered_qty of goods across all orders
Their total picked_qty of goods across all orders
I can get the first one with:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
But when I LEFT JOIN the other two tables and add the
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
.. then I get results that dont seem to add up for the quantities, and the COUNT(*) seems to include the additional lines from the JOIN which obviously then isn't giving me the number of Orders anymore.
Not sure what to try next.
===== EDIT =======
Here's the query I tried:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
try COUNT(distinct Orders.order_id) as number_of_orders,
as in
SELECT
customer_id,
COUNT(distinct Orders.order_id) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
(select SUM(Parcels.picked_qty)
FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
GROUP BY Orders.customer_id
EDIT: added an other select with subselect
Is there any particular reason you feel the need to combine all these in one query? Simplify by breaking it up in to separate queries, and if you want a single call to get the results, put the queries in a stored procedure, using temp tables.