Query with distinct and group by - mysql

How do I get 9,300 only out of the table above? I just need to add 6500 + 1800 + 1000
Here is my current query
SELECT
SUM(e.amount) / (SELECT count(e2.receipt_no)
FROM entries e2
WHERE e2.receipt_no = e.receipt_no) as total,
e.user_id
FROM
entries e
GROUP BY e.receipt_no
The result is
Now i need to get the total per user_id
Expected output should be

From my understanding this should give you want you want
SELECT sum(DISTINCT amount) as total, reciept_no FROM entries GROUP BY receipt_no

Try some thing like this
SELECT SUM(DISTINCT(amount)) as total, user_id FROM `entries` GROUP BY user_id

Try this
SELECT SUM(amount) as total,user_id FROM entries GROUP BY user_id

First calculate DISTINCT amount group by userid,receipt_no and then sum of there entries group by user_id:
SELECT sum(total),userid from (SELECT sum(DISTINCT amount) as total,
userid,receipt_no FROM entries GROUP BY userid,receipt_no) as rgrouped
GROUP BY userid

You can also try this
SELECT user_id, SUM(DISTINCT `amount`) FROM `test` group by `user_id`
Step 1: Select distinct amount for each user id
101 - 6500,1800,1000
189 - 1019.00
Step - 2
101 = 6500+1800+1000 = 9300.00
189 = 1019.00
This will select distinct amount for each user id and then add selected amount and give you same result.

Related

How to stop this repetition and group by date

After multiple attempts I'm unable to get my required result from this query.
SELECT
cc.date as credi_date,
cd.date as debit_date,
cd.month AS month,
ROUND(IFNULL(cc.credit_amount,0),2) AS credit,
ROUND(IFNULL(cd.debit_amount,0),2) AS debit
FROM
(SELECT
DATE(cc.credit_date) as date,
MONTHNAME(cc.credit_date) as month,
IFNULL(SUM(cc.credit_amount),0) AS credit_amount
FROM
cust_credit cc
WHERE YEAR(cc.credit_date) = YEAR(NOW())
GROUP BY DATE(cc.credit_date)) cc
INNER JOIN
(SELECT
DATE(cd.debit_date) as date,
MONTHNAME(cd.debit_date) as month,
IFNULL(SUM(cd.debit_amount),0) AS debit_amount
FROM
cust_debit cd
WHERE YEAR(cd.debit_date) = YEAR(NOW())
GROUP BY DATE(cd.debit_date)) cd ON cc.month=cd.month
The problem is that this query repeats the rows multiple times. I don't know what thing cause this repetition and how can fix this. The image of result is given below.
My required result is
My credit table is give below
My debit table is give below
Yon should use a UNION in the subquery and then GROUP BY in the SELECT:
SELECT my_date,
my_month,
SUM(credit),
SUM(debit)
FROM (
SELECT cc.credit_date as my_date,
MONTHNAME(cc.credit_date) as my_month,
ROUND(SUM(IFNULL(cc.credit_amount,0)),2) AS credit,
0 AS debit
FROM cust_credit cc
WHERE YEAR(cc.credit_date) = YEAR(NOW())
GROUP BY cc.credit_date,
MONTHNAME(cc.credit_date)
UNION
SELECT
cd.debit_date ,
MONTHNAME(cd.debit_date) ,
0 ,
ROUND(SUM(IFNULL(cd.debit_amount,0)),2)
FROM cust_debit cd
WHERE YEAR(cd.debit_date) = YEAR(NOW())
GROUP BY cd.debit_date,
MONTHNAME(cd.debit_date)
) as TT
GROUP BY my_date,my_month
This is the Fiddle
Basically, your result is cross join between two query and that's the reason your are getting cross product i.e. total number of rows from credit x total number of rows from debit.
Because, month for all rows in debit and credit tables are same (i.e. January). But, you have unique date in both table
So, making below change should give you expected output.
ON cc.month=cd.month
to
ON cc.date = cd.date

How to display only value that occurs second highest number of times?

Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;

Max And Sum mysql

I've to get all the orders from one customer, then get the sum of the orders and then get the max order.
I can't use order by and limit.
I got the result but i can't get the make sum and max work properly
here is my current query:
SELECT SUM(Qty * UnitPrice) AS Total FROM `Details`
WHERE ONo IN (
SELECT Orders.Ono
FROM Orders, Customers
WHERE Customers.FName = 'Charles' AND Customers.LName = 'Xavier' AND Customers.CNo = Orders.CNo
GROUP BY Orders.ONo
)
GROUP BY ONo
Total
7.50
20.99
54.47
49.98
8.00
Please try:
SELECT MAX(Total) as MaxTotal FROM (<your query comes here>) AS T

SQL - COUNT() counts wrong number

I am trying to count the number of profile visits, but it counts the wrong number. In the following example there should be 3 visits, but it counts 6! Anyone know what is wrong with it? http://sqlfiddle.com/#!9/b43ea/8
SELECT *,
COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
LEFT JOIN user_login ON user_login.user_id = profile_visitors.user_id
WHERE profile_visitors.user_id = 1
You need to Group By to count multiple rows, So take the Star out of your query and add a group by user_id also make it profile_visitors.*
The LEFT JOIN to user_login table provides no benefit to this question, but, the following query will get you the detail you want to see (assuming you only want to see the number of visits for user_id = 1):
SELECT COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
WHERE profile_visitors.user_id = 1
GROUP BY profile_visitors.profile_id
To see all visits by profile use:
SELECT profile_id, COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
GROUP BY profile_visitors.profile_id
you can use WHERE IN () to compare if profile_visitors.user_id exist in user_login
SELECT *,
COUNT(profile_visitors.user_id) AS visitorCount
FROM profile_visitors
WHERE profile_visitors.user_id IN (SELECT user_id FROM user_login )
result:
id user_id profile_id visit_date visitorCount
1 1 1 May, 10 2015 15:26:46 3

How to find the minimum values of a query's column

how do you find the minimum value of a column of a query?
My table X's data looks like this:
(id, something, userId, something) values
('R001','something','U0006','something'),
('R002','something','U0014','something'),
('R001','something','U0006','something'),
('R002','something','U0015','something'),
('R003','something','U0003','something'),
('R001','something','U0014','something'),
('R001','something','U0002','something');
My query, looks like this:
SELECT DISTINCT userId, COUNT( id ) AS count
FROM X
GROUP BY userId
ORDER BY count DESC
and this query returns:
userId count
U0006 2
U0014 2
U0002 1
U0003 1
U0015 1
How do I get the minimum values of count in the query, bearing in mind that there are multiple minimum values?
The return I want from the query would look like this:
userId
U0002
U0003
U0015
Thanks, in advance :)
You can do this using a having clause:
SELECT userId, COUNT( id ) AS count
FROM X
GROUP BY userId
HAVING COUNT(id) = (SELECT MIN(cnt)
FROM (SELECT COUNT(id) as cnt
FROM X
GROUP BY userId
) xx
);
Using select distinct with group by is almost always redundant.