Looking for Mysql query - mysql

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*

Related

How to sub count of group by

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

MySQL: SUM of a column based on a value of another column

I want to have a sum of a column that is based if the value of another column is this/that. Here's my table structure
id leave_hours leave_type overtime_hours employee_id
1 7.6 1 0 3
2 5 2 0 3
3 0 0 2.3 3
I have this query already but I need to have a column that will only sum if leave type is 1 and if the leave type is 2
SELECT employee_id, SUM(overtime_hours) AS ot_hrs, SUM(leave_hours if leave_type == 1) AS
leave_1, SUM(leave_hours if leave_type == 2) AS leave_2
FROM table
GROUP BY employee_id
Result should be:
employee_id ot_hrs leave_1 leave_2
3 2.3 7.6 5
Thanks for your inputs.
You need to use conditional aggregation to sum the different leave_hours separately:
SELECT employee_id,
SUM(overtime_hours) AS ot,
SUM(CASE WHEN leave_type = 1 THEN leave_hours ELSE 0 END) AS leave_1,
SUM(CASE WHEN leave_type = 2 THEN leave_hours ELSE 0 END) AS leave_2
FROM `table`
GROUP BY employee_id
Output:
employee_id ot leave_1 leave_2
3 2.3 7.6 5
Demo on dbfiddle
Note that if you use floating point numbers to store the hours, you may need to ROUND the results.
you can use max() with case when
SELECT employee_id,
max(overtime_hours) AS ot,
max(CASE WHEN leave_type = 1 THEN leave_hours END) AS leave_1,
max(CASE WHEN leave_type = 2 THEN leave_hours END) AS leave_2
FROM table_name
GROUP BY employee_id
output
employee_id ot leave_1 leave_2
3 2.3 7.599999904632568 5

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

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

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