Here I am with a problem I can't solve myself.
My problem is:
I need to SELECT the count of a column, but I also need to GROUP BY on that same column. What I've tried so far is not returning as I expected.
Here is what I tried:
'SELECT COUNT(user_id) AS total_donors
FROM ' . DONATION_SECURITY_TABLE .
" WHERE payment_status = 'Completed'
GROUP BY user_id"
This is how my table looks like:
id user_id payment_status
1 20 Completed
2 33 Completed
3 44 Completed
4 20 Pending
5 33 Pending
6 44 Completed
7 20 Completed
As you see, a single user_id can be Pending or Completed more than once, but I want my query to return 3 (based on the table example above).
So, I want my query to COUNT GROUPED user_ids if payment status is completed.
Ideas?
You can use the DISTINCT keyword to select the unique User ID's like so
"SELECT COUNT(DISTINCT user_id) AS total_donors
FROM " . DONATION_SECURITY_TABLE .
" WHERE payment_status = 'Completed'
sounds like a distinct count is what you need. throw away the group by and try this:
SELECT
COUNT(DISTINCT user_id) AS total_donors
FROM
mytable
WHERE
payment_status = 'Completed'
For a result of 3, replace your Count(user_id) with Count(distinct user_id) and remove the group by.
That gives you the count of unique user_ids with a payment status of completed.
Use this:
SELECT COUNT(DISTINCT `user_id`) as `Total` FROM `Donation_Table`
WHERE `payment_status` = 'Completed'
since you are only interested on the count of user_ID, you don't need to use GROUP BY clause
SELECT COUNT(DISTINCT user_ID)
FROM tableName
WHERE payment_status = 'completed'
when you try to add GROUP BY clause. the result is very different from what you are expecting.
Related
I am trying to get a count of tickets from my database where there are results with multiple status. If I run:
SELECT status, count(status) AS t_count
FROM `ticket`
WHERE user_id = 123 AND status <=3
GROUP BY status
The result is as follows
status t_count
1 1
2 5
3 17
I am trying to get t_count to show 23 so have written a sub query as follows:
SELECT user_id, sum(status) AS ticket_count
FROM ticket
WHERE (user_id, ticket_count) IN
(SELECT user_id, status, sum(status) AS ticket_count
FROM `ticket` WHERE user_id = 123 GROUP BY status)
GROUP BY user_id
this results in an error of #1054 -
Unknown column 'ticket_count' in 'IN/ALL/ANY subquery'
Can you advise on what I'm doing wrong. Not do subqueries much before and the one I have done are straight forward and examples I've googled don't use counts and sums
i have a problem my database looks like this DATABASE STRUCTURE
i want to sort the lboard and ignore the repeated values
like this REQUIRED OUTPUT
can anyone help me?
You could use GROUP BY to get single result for one user and MAX function to get its maximum score
the query will be like this
SELECT id, exam_id, user_id, MAX(lboard) FROM `leaderb` GROUP BY user_id ORDER BY lboard DESC
SELECT id, exam_id, user_id, MAX( lboard )
FROM wp_watu_takings WHERE exam_id = 3
GROUP BY user_id
ORDER BY MAX( lboard ) DESC
Hello I have the following table design
ID account_id score date
------------------------------------------
1 500 4 x
2 764 4 x
3 500 6 x
4 500 7 x
5 764 5 x
I'm trying to get all rows with the latest account_id entry
so my code should return
ID account_id score date
------------------------------------------
4 500 7 x
5 764 5 x
I tried the following code but it seems to return the first entry but with the latest date
SELECT account_id,score, max(date) from table group by account_id
Case 1: If id is auto-increment column or max(id) means latest row.
select * from
(select * from table_name
order by id desc) temp
group by account_id
Case 2: If date column decides latest row then replace id by date in order clause and group clause.
try it-
SELECT distinct a.id,a.account_id,b.score,b.date
FROM mytable b
JOIN
(
SELECT account_id,MAX(id) AS id
FROM mytable
GROUP BY account_id
) a ON a.account_id=b.account_id
ORDER BY a.id;
This question is just a duplicate of SQL Select only rows with Max Value on a Column
There you'll find a good explanation.
SELECT a.*
FROM table a
INNER JOIN (
SELECT `account_id`,MAX(`date`) AS latest FROM table GROUP BY account_id
) b
ON a.`date` = b.`latest` AND a.`account_id` = b.`account_id`
ORDER BY `date` DESC
Reference :
Using ORDER BY and GROUP BY together
Try using this query it works fine
Select a.account_id,a.score,a.date
from authors as a
join
(Select account_id,max(date) as date
from authors
group by account_id) as d
on(d.date=a.date)
I currently work on the e-shopping cart project and need to record the tranasaction
The structure is like this:
order
------
id
order_id
* member_id
member
------
id
member_id
status
--------
id
description
* order_id
create_date
So, the status is to store the order change status, e.g. when the order is received , then I add the order , and add the status with the description 100 <== I define and hardcode it . And if the order is deliver, then I will insert one more status 200 <== also is I define.
Then , the problem is , how to query the result like this?
(Desc) create_date order_id member-id status_description
e.g.
2014 06 13 05:00:00 #11111 #12345 order_receive
2014 06 13 03:00:00 #15555 #12222 order_deliver
Notice e.g. I store the status time whenever the order update. So there should be a several status of the order, and the result should contain the latest status only , so I need to get the max (create_date). How to structure the query in this scenario?
I have thought about it but it is not in one query, I wonder are there any way to create it in one query? Thanks
Update
My attempt query is like this
select order_id from status group by order_id order by create_date
In PHP , for each order_id, select * from order where order_id = that id and join the member table
Try this:
SELECT src.max_date,
src.order_id,
src.member_id,
CASE
WHEN s1.description = 100 THEN 'order_receive'
WHEN s1.description = 200 THEN 'order_deliver'
END AS status_description FROM
(SELECT MAX(s.create_date) max_date, s.order_id , o.member_id
FROM status s
JOIN `order` o ON o.order_id = s.order_id
GROUP BY s.order_id, o.member_id) src
JOIN status s1 ON s1.create_date = src.max_date;
Demo
I have a database table 'sales_list'. In this is rows of sales records attributed to a users_sales_guild_id. I'd like to query the table and order results by the number of sales made by each user, highest to lowest.
I thought this query would do it, but alas no...
$total_query = "SELECT *, COUNT(users_sales_guild_id) AS users_sales_guild_id_count FROM sales_list WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59' ORDER BY users_sales_guild_id_count DESC";
$total_rs = mysql_query($total_query) or trigger_error ("Query: $total_query\n<br>MySQL Error: " .#mysql_error()); // Run the query.
$num_rs = mysql_num_rows($total_rs);
This query returns 1 record. rather than a selection of records ordered by the number of sales by each user.
Your assistance is much welcomed.
count(*) will return one row unless there is a group by clause, so the query should be as
SELECT *,
COUNT(*) AS users_sales_guild_id_count
FROM sales_list
WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59'
group by users_sales_guild_id
ORDER BY users_sales_guild_id_count DESC
UPDATE : Its better to select col1,col2 ..... instead of * while doing group by - Point raised by InoSHeo
check this link
http://sqlfiddle.com/#!2/1201d/6
check this link if you would like to get details based on username http://sqlfiddle.com/#!2/1201d/4 here i have used type instead you can use username