How to sub count of group by - mysql

I have 3 table:
user 1 - n (user_id)feedback(type_id) n - 1 word
User:
id name
1 name1
2 name2
3 name3
4 name4
5 name5
6 name6
Feedback:
id user_id type_id title
1 1 1 title1
2 1 1 title2
3 1 2 title3
4 2 1 title4
5 2 2 title5
6 2 2 title6
7 2 1 title7
Word:
id name category
1 great work FEEDBACK_TYPE
2 good work FEEDBACK_TYPE
I tried to count feedback of each user with below query
Result:
user_id countAllFeedback
1 3
2 4
Query
select
feed.user_id,
count(feed.id) as countAllFeedback
from
feedback feed
group by
feed.user_id;
I want to count feedback of each user by word name
user_id countAllFeedback countGreatWorkFeedback countGoodWorkFeedback
1 3 2 1
2 4 2 2
Please help me with your Query ^^ Thank you

You can try this below script. As you have nothing to collect dynamically from other table, No joining is required. You can use Type_ID as static logic inside CASE statement as those are known list in Word table.
SELECT A.user_id,
COUNT(*) countAllFeedback,
SUM(CASE WHEN A.type_id = 1 THEN 1 ELSE 0 END) countGreatWorkFeedback,
SUM(CASE WHEN A.type_id = 2 THEN 1 ELSE 0 END) countGoodWorkFeedback
FROM Feedback A
GROUP BY A.user_id
If there are possibilities of change in ID, you can do the below-
SELECT A.user_id,
COUNT(A.*) countAllFeedback,
SUM(CASE WHEN B.name = 'great work' THEN 1 ELSE 0 END) countGreatWorkFeedback,
SUM(CASE WHEN B.name = 'good work' THEN 1 ELSE 0 END) countGoodWorkFeedback
FROM Feedback A
INNER JOIN Word B ON A.type_id = B.id
GROUP BY A.user_id

there is no relevance of User table (as per demo data)
This can achieve using this following Query....
SELECT b.user_id,c.name,
SUM(b.user_id) AS 'countAllFeedback',
SUM(CASE WHEN c.name='great work' THEN 1 ELSE 0 END) AS 'countGreatWorkFeedback',
SUM(CASE WHEN c.name='good work' THEN 1 ELSE 0 END) AS 'countGoodWorkFeedback'
FROM tbl_Feedback b
LEFT OUTER JOIN
tbl_Word c
ON b.type_id=c.id
GROUP BY b.user_id,c.name
Note:- Here i'm using Case statement with sum function

Related

How to achieve MySQL Query combining multiple COUNT() and GROUP BY

I have this data:
id date userid result
1 2015-05-01 1 a
2 2015-05-02 1 b
3 2015-05-03 1 b
4 2015-05-03 1 a
5 2015-05-04 1 a
I need to get users sorted by result:
id a b
1 1 1
2 1 1
You want conditional aggregation:
SELECT user_id, sum(result = 'win') AS wins, sum(result = 'loss') as losses
FROM table
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4;
I would do it like this:
SELECT user_id,
SUM(CASE WHEN result='win' THEN 1 ELSE 0 END) AS wins,
SUM(CASE WHEN result='loss' THEN 1 ELSE 0 END) AS losses
FROM table1
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4
Fiddle is here: http://sqlfiddle.com/#!9/00ac7/8

Summerize sql count on post statuses

I have a the following table structure:
Users table:
id username
1 bob
2 john
3 harry
Posts table:
id user_id status
1 1 4
2 2 4
3 3 2
4 1 1
5 2 1
If I wanted a summary of posts per user and cols of statuses, what is the most efficient sql to generate that?
Ex:
username status 4 status 2 status 1
bob 1 0 1
john 1 0 1
harry 0 1 0
This should work:
SELECT users.username
, sum(if(posts.status=4,1,0)) as `status 4`
, sum(if(posts.status=2,1,0)) as `status 2`
, sum(if(posts.status=1,1,0)) as `status 1`
FROM users, posts
WHERE users.id=posts.user_id
GROUP BY users.id
You can try somthing like this:-
SELECT A.USERNAME,
CASE WHEN STATUS = 4 THEN COUNT(B.STATUS) AS STATUS4 ELSE 0 END,
CASE WHEN STATUS = 2 THEN COUNT(B.STATUS) AS STATUS2 ELSE 0 END,
CASE WHEN STATUS = 1 THEN COUNT(B.STATUS) AS STATUS1 ELSE 0 END,
FROM USERS INNER JOIN POSTS
ON USERS.ID = POSTS.USER_ID
GROUP BY USERS.ID;
This might help you.

SQL two "group by" in the same query

We have the table to track what product page a user visited.
product_tracking
____________________
id user_id product
1 1 A
2 1 B
3 2 A
4 1 A
I know we can use group by user_id, and group_by product but I need both.
Expected result :
result_table
____________________________
user_id A B C
1 2 1 0
2 1 0 0
any idea how to merge the 2 group by ?
You could do this:
SELECT
product_tracking.user_id,
SUM(CASE WHEN product_tracking.product='A' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN product_tracking.product='B' THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN product_tracking.product='C' THEN 1 ELSE 0 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id
An alternative to using sum(...) is using count(...):
SELECT
product_tracking.user_id,
COUNT(CASE WHEN product_tracking.product='A' THEN 1 END) AS A,
COUNT(CASE WHEN product_tracking.product='B' THEN 1 END) AS B,
COUNT(CASE WHEN product_tracking.product='C' THEN 1 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id

Looking for Mysql query

I have mysql table with following 3 fields:
- vouchercode: Unique KEY
- voucherstatus: 1 for unused voucher, 3 for used voucher
- partnerId
I want to display how many vouchers are used and unused w.r.t partnerId.
Example :- table data
PartnerId voucherstaus vouchercode(unique)
1 1
1 3
1 1
2 3
2 3
2 1
Result:
PartnerId usedvouchers unusedvouchers
1 1 2
2 2 1
Please help me with mysql query for same. Seems i have to use subquery and group by doesn't work. Thanks in advance
SELECT partnerID,
SUM(CASE WHEN voucherstatus = 1 THEN 1 ELSE 0 END) unusedvouchers,
SUM(CASE WHEN voucherstatus = 3 THEN 1 ELSE 0 END) usedvouchers
FROM data
GROUP BY partnerID
SQLFiddle Demo
SQLFiddle Demo (will work only in MySQL)
select PartnerId, SUM(case when voucher_status =1 then 1 else 0 end) as unused, sum(case when voucher_status =3 then 1 else 0 end) as used from Voucher group by PartnerID;
sqlfiddle *demo*

MySQL query to count non-null values in a single row

I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4