Add column from another table, but not affect count() - mysql

I have already a query with multiple JOINs, simple list of reservations
SELECT reservation.reservation_id, customer.customer_id, customer.name, count(ordered_services.reservation_id) AS num_of_ordered_services
FROM reservations
JOIN customers ON reservations.customer_id = customer.customer_id
LEFT JOIN ordered_services ON reservations.reservation_id = ordered_services.reservation_id
GROUP BY reservation.reservation_id, customer.customer_id, customer.name
ORDER BY reservation.reservation_id
which outputs something like
reservation_id | customer_id | name | num_of_ordered_services
1 | 1909091202 | John | 2
2 | 2512541508 | Jane | 3
I would like to add another column with information about payment, but simple JOIN, LEFT JOIN interferes with existing count() column. Like
SELECT reservation.reservation_id, count(payments.reservation_id) AS num_of_payments
FROM reservations
LEFT JOIN payments ON reservations.reservation_id = payments.reservation_id
GROUP BY reservation.reservation_id
ORDER BY reservation.reservation_id
reservation_id | num_of_payments
1 | 0
2 | 2
but in both a single result. How to achieve this?
PS: num_of_payments is not necessary, I only need to know if the payment for certain reservation exists or not (1, 0).
Thank you
tbl structure, nothing special:
reservations
reservation_id | customer_id | added
1 | 1909091202 | 2011-11-04 02:37:28
2 | 2512541508 | 2011-11-04 14:27:01
customers
customer_id | name | personal information columns ...
1909091202 | John | | |
2512541508 | Jane | | |
... | ... | | |
payments
payment_id | reservation_id | customer_id | total | added
1 | 2 | 1909091202 | 199 | 2011-11-04 02:37:28
2 | 2 | 2512541508 | 50 | 2011-11-04 14:27:01

You could use a subselect for the additional field.
SELECT reservation.reservation_id, customer.customer_id, customer.name,
count(ordered_services.reservation_id) AS num_of_ordered_services,
(SELECT count(*) FROM payments WHERE reservation.reservation_id=payments.reservation_id) AS num_of_payments
FROM reservations
JOIN customers ON reservations.customer_id = customer.customer_id
LEFT JOIN ordered_services ON reservations.reservation_id = ordered_services.reservation_id
GROUP BY reservation.reservation_id, customer.customer_id, customer.name
ORDER BY reservation.reservation_id

Something like the following should work:
select
reservation.reservation_id,
(case when exists (select * from payments p1 where p1.reservation_id = reservation.reservation_id) then 1 else 0 end) as one_or_many_payments_made
from reservation
GROUP BY reservation.reservation_id
ORDER BY reservation.reservation_id
But without your data, there is some guesswork here.

Related

COUNT all values in a column with JOIN

I am joining three tables and need to return two separate counts, one showing the total number of unique users who have purchased an item, and the other showing the total number of unique users who haven't purchased an item. These are cropped for brevity, but here are the relevant tables:
user table
+----------+------+------+-----+
| username | colb | colc | etc |
+----------+------+------+-----+
| user1 | * | * | * |
| user2 | * | * | * |
| user3 | * | * | * |
+----------+------+------+-----+
purchase table
+------------+---------+----------+------+
| purchaseID | storeID | username | cost |
+------------+---------+----------+------+
| 1 | 1 | user1 | * |
| 2 | 1 | user2 | * |
| 3 | 5 | user2 | * |
| 4 | 3 | user1 | * |
+------------+---------+----------+------+
store table
+---------+-----------+-----+
| storeID | storeName | etc |
+---------+-----------+-----+
| 1 | store1 | * |
| 2 | store2 | * |
| 3 | store3 | * |
+---------+-----------+-----+
I am currently using this query to get the unique users who have purchased an item from a store:
SELECT
store.storeID storeID,
store.storeName storeName,
COUNT(DISTINCT CASE WHEN purchase.username IS NOT NULL
THEN purchase.purchaseID END) AS purchases
[Query to retrieve total unique users who have not purchased an item]
FROM store
LEFT JOIN purchase
ON store.storeID = purchase.storeID
LEFT JOIN user
ON purchase.username = user.username
GROUP BY 1, 2
I have tried a few different ways, none of which have worked. The issue I've identified is when the LEFT JOIN happens it only returns the matching results for usernames, thus the COUNT won't include the other users in the user table. I have not had any luck finding a way to fix this, so I'm hoping someone on here can lend me a hand. The results I'm hoping to see should be something like this:
+---------+-----------+-----------+--------------+
| storeID | storeName | purchases | nonPurchases |
+---------+-----------+-----------+--------------+
| 1 | store1 | 2 | 1 |
| 2 | store2 | 0 | 3 |
| 3 | store3 | 1 | 2 |
+---------+-----------+-----------+--------------+
that is actually quite simple.
First you count all user and subtract te count of distinct purchasers
SELECT
store.storeID storeID,
store.storeName storeName,
COUNT(DISTINCT CASE WHEN purchase.username IS NOT NULL
THEN purchase.purchaseID END) AS purchases,
(SELECT COUNT(*) FROM User) - COUNT(DISTINCT CASE WHEN purchase.username IS NOT NULL
THEN purchase.purchaseID END) AS NON_purchases
FROM store
LEFT JOIN purchase
ON store.storeID = purchase.storeID
LEFT JOIN user
ON purchase.username = user.username
GROUP BY 1, 2
Here is a clean solution.
Please note that the aggregation is done before the join.
with
purchases as
(
select storeID
,count(distinct username) as purchase
from purchase
group by storeID
),
users as
(
select count(*) as total_users
from user
)
select storeID
,storeName
,coalesce(purchase, 0) as purchase
,total_users - coalesce(purchase, 0) as nonPurchases
from store
left join purchases using (storeID)
cross join users
storeID
storeName
purchase
nonPurchases
1
store1
2
1
2
store2
0
3
3
store3
1
2
Fiddle
I'll go with a slightly different approach.
Generate a combination of store and user using CROSS JOIN, make it as a subquery then use that to LEFT JOIN with purchase table. In SELECT, change COUNT(DISTINCT ..) to SUM(..). Something like this:
SELECT us.storeID,
us.storeName,
SUM(CASE WHEN p.username IS NOT NULL
THEN 1 ELSE 0 END) AS purchases,
SUM(CASE WHEN p.username IS NULL
THEN 1 ELSE 0 END) AS nonPurchases
FROM (SELECT storeID, storeName, username FROM user u CROSS JOIN store s) us
LEFT JOIN (SELECT DISTINCT storeid, username FROM purchase) p
ON us.storeID = p.storeID
AND us.username=p.username
GROUP BY 1, 2;
Thanks to David pointing out in the comment that my previous suggestion is not exactly counting unique users. So I made a quick modification to make sure that it does what OP wanted in the first place. Therefore I did a SELECT DISTINCT ... on purchase table then make it as a subquery for the LEFT JOIN. The other parts of the original suggestion remains.
Updated fiddle

MySQL query to count user orders by type

I have this 3 tables: users, orders and order_item.
When a user can have a order, and the order item can be event only, membership only or both, and so 2 line will be written to the order_items
users tables
id | user_id | name | phone
---|-----------|-------|------
1 | 123456789 | Jon | 555-55555
2 | 123456780 | Alice | 555-6666
orders tables
id | user_id | user_uid | user_info
---|---------|-----------|----------
1 | 1 | 123456789 | bla
2 | 2 | 123456780 | foo
3 | 2 | 123456780 | foo
order_items table
id | order_id | order_type | price
--- | -------- | ---------- | ------
1 | 1 | membership | 70
2 | 1 | event | 200
3 | 2 | event | 300
4 | 3 | membership | 70
The relationship is like this,
order_items.order_id -> orders.id
orders.user_id -> users.id
orders.user_uid -> users.user_id
I'm looking for a query which will produce this type of output,
user_id | name | count_membership | count_events | total_orders
-------- | ------ | ------------------ | -------------- | --------------
123456789 | Jon | 1 | 1 | 1
123456780 | Alice | 1 | 1 | 2
I like to count the total orders a user have, and count how many of each of item he have. in the end I like to filter out all users where count_membership = 0
Thanks in advance,
You can get the expected result set by using some conditional aggregation
select u.user_id,
u.name,
sum(oi.membership_count) membership_count,
sum(oi.event_count) event_count,
count(o.id) total_orders
from users u
join orders o on u.id = o.user_id
join (
select order_id,
sum(order_type = 'membership') membership_count,
sum(order_type = 'event') event_count
from order_items
group by order_id
) oi on o.id = oi.order_id
group by u.user_id,u.name
Demo
Your query needs to join the three tables on their common fields, which would be user_id and order_id.
Since you said you don't want any results where membership is not an order type, your results example doesn't make sense and the grouping into single results that way using a single mysql query makes things complicated to put order_type which is a single field into two separate columns. So I adjusted slightly.
select users.user_id, users.name, order_type, count(order_id) as total_orders from orders, order_items, users where users.user_id = orders.user_id and orders.id = order_items.order_id and order_type = "membership" group by users.user_id, users.name
working output from mysql terminal:
mysql> select users.user_id, users.name, order_type, count(order_id) as total_orders from orders, order_items, users where users.user_id = orders.user_id and orders.id = order_items.order_id and order_type = "membership" group by users.user_id, users.name;
+---------+------+------------+--------------+
| user_id | name | order_type | total_orders |
+---------+------+------------+--------------+
| 123 | Jon | membership | 1 |
| 1234 | Pam | membership | 2 |
+---------+------+------------+--------------+

Get SUM of two fields in 2 different related tables

I'd like to get the SUM of the amount column in two related tables.
Invoices Table:
-----------------------------------------
| id | student_id | created | updated |
-----------------------------------------
| 5 | 25 | date | date |
-----------------------------------------
Invoice Items Table:
------------------------------
| id | invoice_id | amount |
------------------------------
| 1 | 5 | 250 |
------------------------------
| 2 | 5 | 100 |
------------------------------
| 3 | 5 | 40 |
------------------------------
Payments Table:
------------------------------
| id | invoice_id | amount |
------------------------------
| 1 | 5 | 100 |
------------------------------
| 2 | 5 | 290 |
------------------------------
Desired Output:
--------------------------------------
| id | invoiceTotal | paymentTotal |
--------------------------------------
| 1 | 390 | 390 |
--------------------------------------
The query I've tried
SELECT
i.id,
sum(ii.amount) as invoiceTotal,
sum(p.amount) as paymentTotal
FROM
invoices i
LEFT JOIN
invoice_items ii ON i.id = ii.invoice_id
LEFT JOIN
payments p ON i.id = p.invoice_id
WHERE
i.student_id = '25'
GROUP BY
i.id
What this seems to do is calculate the sum of the payments properly but the invoice_items.amount appears to have been duplicated by 6 (which is the number of payments there are).
I have read similar questions on SO here and here but the examples are so much more complex than what I'm trying to do and I can't figure out what to put where.
The join causes a problem with cartesian products. If a student has multiple invoice items and payments, then the totals will be wrong.
One approach that works best for all invoices is a union all/group by approach:
select i.id, sum(invoiceTotal) as invoiceTotal, sum(paymentTotal) as paymentTotal
from ((select i.id, 0 as invoiceTotal, 0 as paymentTotal
from invoices i
) union all
(select ii.invoiceId, sum(ii.amount) as invoiceTotal, NULL
from invoiceitems ii
group by ii.invoiceId
) union all
(select p.invoiceId, 0, sum(p.amount) as paymentTotal
from payments p
group by p.invoiceId
)
) iip
group by id;
For a single student, I would recommend correlated subqueries:
select i.id,
(select sum(ii.amount)
from invoiceitems ii
where ii.invoiceid = i.id
) as totalAmount,
(select sum(p.amount)
from payment p
where p.invoiceid = i.id
) as paymentAmount
from invoices i
where i.studentid = 25;

MySql LEFT JOIN with COUNT

I have two tables, customers and sales. I want to count sales for each customer and create a table of sales per month for each store.
I would like to produce something like;
------------------------------
month | customers | sales |
------------------------------
1/2013 | 5 | 2 |
2/2013 | 21 | 9 |
3/2013 | 14 | 4 |
4/2013 | 9 | 3 |
but I am having trouble getting the sales count to be correct when using the following;
SELECT CONCAT(MONTH(c.added), '/', YEAR(c.added)), count(c.id), count(s.id)
FROM customers c
LEFT JOIN sales s
ON s.customer_id = c.id AND MONTH(c.added) = MONTH(s.added) AND YEAR(c.added) = YEAR(s.added)
WHERE c.store_id = 1
GROUP BY YEAR(c.added), MONTH(c.added);
Customers table;
-------------------------------
id | store_id | added |
-------------------------------
1 | 1 |2013-02-01 |
2 | 1 |2013-02-02 |
3 | 1 |2013-03-16 |
sales table;
---------------------------------
id | added | customer_id |
---------------------------------
1 | 2013-02-18 | 3 |
2 | 2013-03-02 | 2 |
3 | 2013-03-16 | 3 |
Can anyone help here?
thanks
(Updated) The existing query will only count sales made in the same month that the customer was added. Try this, instead:
SELECT CONCAT(MONTH(sq.added), '/', YEAR(sq.added)) month_year,
sum(sq.customer_count),
sum(sq.sales_count)
FROM (select s.added, 0 customer_count, 1 sales_count
from customers c
JOIN sales s ON s.customer_id = c.id
WHERE c.store_id = 1
union all
select added, 1 customer_count, 0 sales_count
from customers
WHERE store_id = 1) sq
GROUP BY YEAR(sq.added), MONTH(sq.added);
SELECT c.* , s.sales_count<br>
FROM customers c<br>
LEFT JOIN (SELECT customer_id, count(id) as sales_count FROM sales GROUP BY customer_id) s on c.id=s.customer_id<br>
WHERE c.store_id = 1<br>

MySQL Join Without Duplicates

I have a customers table, and an orders table. Each customer can have many orders.
I want to select every customer, along with their earliest order number from the orders table (it is important that I select the earliest order number, not just any order). I want to list customers whether they have an order or not, and I don't want to include customers twice if they have more than one order.
I'm using this:
SELECT *
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id
GROUP BY customers.id
This gives me almost what I want, except it will pick whatever order ID it likes from the table. I need to be able to sort, and pick the smallest order ID.
Any ideas please?
Im pretty sure its something that's staring me in the face...
EDIT: Tables' Structures as requested
Customers:
| ID | Name | Address | Etc |
----------------------------------------
| 1 | Joe | 123 Fake Street | |
| 2 | Mike | 1600 Fake Road | |
| 3 | Bill | Red Square, Moscow | |
----------------------------------------
Orders:
| ID | Customer_ID | Date |
---------------------------
| 1 | 1 | ... |
| 2 | 2 | ... |
| 3 | 2 | ... |
| 4 | 1 | ... |
---------------------------
Create a virtual table (a/k/a subquery) with the lowest numerical order ID for each customer.
SELECT customer_id, min(order_id)
FROM orders
GROUP BY customer_id
Then join that table with the customer table, like so.
SELECT C.customer_id, firstorder.order_id
FROM CUSTOMERS as C
LEFT JOIN (
SELECT customer_id, min(order_id)
FROM orders
GROUP BY customer_id
) AS firstorder ON c.customer_id = firstorder.customer_id
Try this
Select customer.*,Order.OrderNo As EarilerORderNo
From Customers Left Join
(Select customer_id,orderid from orders order by customer_id,orderid desc) As Orders
ON Customers.Id=Orders.OrderID