I have one table called quiz_data which have column called id, user_id and user_band which have value like below
id user_id user_band
1 1 A
2 2 B
3 1 A
4 3 A
I want get total unique user_band with keep in mind user_id will count only one time. from above example I want result called userband A = 2 and B = 1.
I have tried like below
select('user_band,COUNT(distinct(user_id)) as count')from quiz_data GROUP BY user_band ORDER BY user_band
but its not working properly. Let me know if someone can help me for same.
Thanks!
The syntax you want is:
select user_band, count(distinct user_id) no_users
from quiz_data
group by user_band
order by user_band
Related
I have a table called deals, it has records like this for example
id deal_ref objectname status
1 1234 tom correct
2 1234 tom correct
3 1234 jerry wrong
4 1234 tom correct
I need to identify all latest deals where the status is "correct for example, but the last entry(row 4) must meet the following criteria, where the Max ID is equal to the deal_ref and the status is correct
I tried this
select distinct deal_ref, deal_status
from dealv1 d
where d.deal_ref = max(id)
and d.deal_status = 'Prospect'
and date_created between '2022-11-02 00:00:00' and '2022-11-04 00:00:00'
You use other names in your SQL than in the table (deal_status, date_created).
Nevertheless try do it the following:
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1
i donĀ“t get exactly what you are trying to do with the maxID. You just want the one row where deal_ref=max(id) and status is correct?
Then add
AND deal_ref = (SELECT MAX(id) from dealv1)
after 'correct' from the above statement.
Guys the query below worked, but its displaying multiple deal_refs that has the same deal_ref, for example 2 rows below each other with deal_ref 1234 twice.
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1
Example, Lets suppose I have a table like next one:
id | start | userId
1 3 1
2 2 2
3 5 1
Now, I want to get all the row information for the second higher value of the column start related to some userId. So, if userId equals to 1 I'm expecting to get the next result:
(id, start, userId) = (1,3,1)
I have tried this query:
SELECT id, max(start) FROM table_user WHERE userId = 1;
But this gives me the higher number.
You can do this easy using ordering by column start and the features of LIMIT, like this:
SELECT
*
FROM
table_user
WHERE
userId = 1
ORDER BY
`start` DESC LIMIT 1, 1
You can check this online also: DB-Fiddle
Using PHP and MySQL, I want to query a table of postings my users have made to find the person who has posted the most entries.
What would be the correct query for this?
Sample table structure:
[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack
I would like to see that "johnnietheblack" is the top poster, "marywhite" is second to best, and "dannyrottenegg" has the least
Something like:
SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1
This gets the number of rows posted by a particular ID, then sorts though the count to find the highest value, outputting it, and the ID of the person. You'll need to replace the 'UserID' and 'postings' with the appropriate column and field though.
I believe this should work...
SELECT user_id, COUNT(*) FROM postings ORDER BY COUNT(*) GROUP BY user_id LIMIT 1
Assuming posting is a tuple (user_id, recipient_user_id), where each row represents one posting, from user_id to recipient_user_id:
select user_id, count(*) as posts
from postings
group by user_id
having count(*) = max(count(*)) ;
I have 2 columns having users id participating in a transaction, source_id and destination_id. I'm building a function to sum all transactions grouped by any user participating on it, either as source or as destination.
The problem is, when I do:
select count (*) from transactions group by source_id, destination_id
it will first group by source, then by destination, I want to group them together. Is it possible using only SQL?
Sample Data
source_user_id destination_user_id
1 4
3 4
4 1
3 2
Desired result:
Id Count
4 - 3 (4 appears 3 times in any of the columns)
3 - 2 (3 appears 2 times in any of the columns)
1 - 2 (1 appear 2 times in any of the columns)
2 - 1 (1 appear 1 time in any of the columns)
As you can see on the example result, I want to know the number of times an id will appear in any of the 2 fields.
Use union all to get the id's into one column and get the counts.
select id,count(*)
from (select source_id as id from tbl
union all
select destination_id from tbl
) t
group by id
order by count(*) desc,id
edited to add: Thank you for clarifying your question. The following isn't what you need.
Sounds like you want to use the concatenate function.
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
GROUP BY CONCAT(source_id,"_",destination_id)
The underscore is intended to distinguish "source_id=1, destination_id=11" from "source_id=11, destination_id=1". (We want them to be 1_11 and 11_1 respectively.) If you expect these IDs to contain underscores, you'd have to handle this differently, but I assume they're integers.
It may look like this.
Select id, count(total ) from
(select source_id as id, count (destination_user_id) as total from transactions group by source_id
union
select destination_user_id as id , count (source_id) as total from transactions group by destination_user_id ) q group by id
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;