I have a table 'A' that looks something like:
_______________________________________________________________
|query_id | query | response |user_response_count |
|---------------------------------------------------------------
| 1 | acne | BothBad | 2 |
| 1 | acne | BothGood | 1 |
| 2 | asthma | BothBad | 1 |
| 2 | asthma | product 1 | 1 |
| 2 | asthma | BothGood | 1 |
| 3 | bell palsy | product 2 | 2 |
| 3 | bell palsy | BothGood | 1 |
---------------------------------------------------------------
I want to write a query to get something that looks like:
__________________________________________________________________________________
| query_id | query | BothGood | BothBad | Product 1 | Product 2 |
-----------------------------------------------------------------------------------
| 1 | acne | 1 | 2 | 0 | 0 |
| 2 | asthma | 1 | 1 | 1 | 0 |
| 3 | bell palsy| 1 | 0 | 0 | 2 |
-----------------------------------------------------------------------------------
That "user_response_count" column actually says, 2 users selected "BothBad" option for "acne" query.
I know, by using max, I can change my rows to the column, but here it would be difficult to the max. Any Thoughts?
Conditional aggregation:
select query_id, query,
sum(case when response = 'BothGood' then cnt else 0 end) as BothGood,
sum(case when response = 'BothBad' then cnt else 0 end) as BothBad,
sum(case when response = 'product 1' then cnt else 0 end) as product1,
sum(case when response = 'product 2' then cnt else 0 end) as product2
from a
group by query_id, query;
You can use a conditional aggregation as
select query_id, query,
max( coalesce(case when response = 'BothGood' then user_response_count end,0) )
as BothGood,
max( coalesce(case when response = 'BothBad' then user_response_count end,0) )
as BothBad,
max( coalesce(case when response = 'product 1' then user_response_count end,0) )
as Product_1,
max( coalesce(case when response = 'product 2' then user_response_count end,0) )
as Product_2
from tableA
group by query_id, query
Demo
Related
My table looks like this:
| id | Vendor | Issue |
|----|--------|-----------|
| 1 | Acme | Defective |
| 2 | Best | Returned |
| 3 | Ace | Other |
| 4 | Best | Returned |
| 5 | Acme | Other |
| 6 | Ace | Other |
| 7 | Best | Defective |
I need a Select statement to sum the amount of each distinct issue each vendor has had.
Output of select statement would look like this in a table:
| Vendor | Defective | Returned | Other |
|--------|-----------|----------|-------|
| Acme | 1 | 0 | 1 |
| Best | 1 | 2 | 0 |
| Ace | 0 | 0 | 2 |
Any help would be greatly appreciated.
You can use the CASE clause to separate the sums, as in:
select
vendor,
sum(case when issue = 'Defective' then 1 end) as defective,
sum(case when issue = 'Returned' then 1 end) as returned,
sum(case when issue = 'Other' then 1 end) as other
from my_table
group by vendor
Final Statement:
$sql = "select
vendor,
sum(case when issue = 'Item Defective' THEN 1 ELSE 0 END) as 'defective',
sum(case when issue = 'Incorrect Item Received' THEN 1 ELSE 0 END) as 'received',
sum(case when issue = 'Incorrect Item Ordered' THEN 1 ELSE 0 END) as 'ordered',
sum(case when issue = 'Item Not Made to Drawing' THEN 1 ELSE 0 END) as 'drawing',
sum(case when issue = 'Other' THEN 1 ELSE 0 END) as 'other'
FROM record GROUP BY vendor";
There are two tables given, tag and media.
mysql> select * from media;
+----+---------+----------+
| id | name | duration |
+----+---------+----------+
| 1 | cat.mp4 | 3.4 |
| 2 | dog.mp4 | 8 |
+----+---------+----------+
mysql> select * from tag;
+----+----------+-------+--------+------------+
| id | media_id | type | value | confidence |
+----+----------+-------+--------+------------+
| 1 | 1 | LABEL | cat | 0.9 |
| 2 | 1 | LABEL | person | 0.6 |
| 3 | 1 | TEXT | kitty | 0.95 |
| 4 | 2 | LABEL | dog | 0.8 |
| 5 | 2 | LABEL | person | 0.75 |
| 6 | 2 | TEXT | food | 0.7 |
+----+----------+-------+--------+------------+
I need to get the output table by joining two tables that gives media_id, name, duration and label of the value from tag such that if the value is cat, the confidence of cat will be inserted into label_cat column otherwise 0 will be inserted.
Something like this:
+----------+---------+----------+-----------+-----------+--------------+
| media_id | name | duration | label_cat | label_dog | label_person |
+----------+---------+----------+-----------+-----------+--------------+
| 1 | cat.mp4 | 3.4 | 0.9 | 0 | 0.6 |
| 2 | dog.mp4 | 8 | 0 | 0.8 | 0.75 |
+----------+---------+----------+-----------+-----------+--------------+
If I understand correctly, you want conditional aggregation:
select m.id, m.name, m.duration,
max(case when t.value = 'cat' then t.confidence end) as label_cat,
max(case when t.value = 'dog' then t.confidence end) as label_dog,
max(case when t.value = 'person' then t.confidence end) as label_person
from media m left join
tag t
on m.id = t.media_it
group by m.id, m.name, m.duration
Select t.media_id,m.name,m.duration,
case "label_cat " when t.value ='cat' then
t.confidence else 0 end case,
case "label_dog" when t.value ='dog' then
t.confidence else 0 end case,
case "label_person" when t.value ='person' then
t.confidence else 0 end case
from
tag t right join media m on t.id=m.id
group by t.media_id
i have table employee
+------+-----------+
| name | is_active |
+------+-----------+
| a | 0 |
| b | 1 |
| c | 1 |
| d | 1 |
| e | 0 |
+------+-----------+
when we use query
select is_active, count(*) count
from employee
group by is_active;
then output in form of
+-----------+--------+
| is_active | count |
+-----------+--------+
| 1 | 3 |
| 0 | 2 |
+-----------+--------+
but i want to output in that form
+-----------+---+----+
| is_active | 0 | 1 |
+-----------+---+----+
| count | 2 | 3 |
+-----------+---+----+
You can use conditional aggregation:
select 'count' as is_active,
count(case when is_active = 0 then 1 end) as count_0,
count(case when is_active = 1 then 1 end) as count_1
from t;
In MySQL, you can write this using sum:
select 'count' as is_active,
sum(is_active = 0) as count_0,
sum(is_active = 1) as count_1
from t;
I have the following table. I would like to add 2 new columns with a select query that will show the total based on the flag type.
Table:
tt | company | count | flag
--------------------------------------------
123 | adeco | 5 | 1
123 | mic | 4 | 2
333 | manpower | 88 | 2
444 | linar | 2 | 2
555 | dlank | 3 | 1
Desired:
tt | company | total | flag | total_flag1 | total_flag2
-------------------------------------------------------------------
123 | adeco | 5 | 1 | 5 | 0
123 | mic | 4 | 2 | 0 | 4
333 | manpower | 88 | 2 | 0 | 88
444 | linar | 2 | 2 | 0 | 2
555 | dlank | 3 | 1 | 3 | 0
By your desired result, you should use case when or if syntax to to this:
select
yourtable.*,
case when flag = 1 then `count` else 0 end as total_flag1,
case when flag = 2 then `count` else 0 end as total_flag2
from yourtable
Or
select
yourtable.*,
if(flag = 1, `count`, 0) as total_flag1,
if(flag = 2, `count`, 0) as total_flag2
from yourtable
I think you can do what you want using correlated subqueries or join:
select t.*, tsum.total_flag1, tsum.total_flag2
from t join
(select t.tt,
sum(case when flag = 1 then total else 0 end) as total_flag1,
sum(case when flag = 2 then total else 0 end) as total_flag2
from t
group by t.tt
) tsum
on t.tt = tsum.tt;
With the following table of car models,
+-------+----+-------+
| model | id | total |
+-------+----+-------+
| civic | 1 | 27 |
| civic | 3 | 11 |
| crv | 3 | 5 |
+-------+----+-------+
would it be possible to query in order to have this result?
+-------+-----+-----+-----+
| model | id1 | id2 | id3 |
+-------+-----+-----+-----+
| civic | 27 | 0 | 11 |
| crv | 0 | 0 | 5 |
+-------+-----+-----+-----+
This is a pivot query. One way to handle this in MySQL is conditional aggregation:
select model,
max(case when id = 1 then total else 0 end) as id1,
max(case when id = 2 then total else 0 end) as id2,
max(case when id = 3 then total else 0 end) as id3
from carmodels t
group by model;