SQL group by different values in different fields - mysql

I'm trying to make a query in my mySQL DB
I Have
Field 1 | Field 2 | url
v1 | Value01 | http:1
v1 | Value02 | http:2
v2 | Value02 | http:3
v3 | Value01 | http:4
v3 | Value02 | http:5
v2 | Value02 | http:6
I try to count the values from field 2 when they are repeated
all grouped by Field 1
I expect this
Field 1 | count Value01(from Field 2)|count Value02(from Field 2)
v1 | 1 | 1
v2 | 0 | 2
v3 | 1 | 1
is there a way to do something like that?

You use a conditional COUNT or SUM
You can remove the ELSE NULL because is the default return for CASE but I include it for clear reading.
.
SELECT
Field1,
COUNT( CASE
WHEN Field2 = 'Value01' THEN 1
ELSE NULL
END) as count_Value01,
SUM ( CASE
WHEN Field2 = 'Value02' THEN 1
ELSE 0
END) as count_Value02
FROM yourTable
GROUP BY Field1

You can do this using conditional aggregation. This is particularly simple in MySQL:
select field1, sum(field2 = 'Value1') as NumValue01,
sum(field2 = 'Value02') as NumValue2
from table t
group by field1;

Related

How to add up rows and return one of them in MySQL

I need to retrieve rows that have a numeric or null value in the HomeID column and finally return the value with a numeric value if there is a row with the same symbol
// my table
+--------+---------+-------+
| Symbol | Home ID | Value |
+--------+---------+-------+
| test | 1 | value |
| test | NULL | value |
| test1 | 2 | value |
| test2 | 3 | vlaue |
+--------+---------+-------+
Actually, I did something like that. It added up the symbols for me, but I don't know how to return the poem I need
SELECT
[Symbol],
COUNT(*) AS CNT
FROM [DB].[dbo].[Table]
GROUP BY
[Symbol]
HAVING COUNT(*) > 1;
One method is:
select t.*
from t
where t.homeid is not null or
not exists (select 1
from t t2
where t2.symbol = t.symbol and t2.homeid is not null
);
You can also do this with aggregation, if you just have these three columns and you want exactly one row per symbol:
select symbol, max(homeid) as homeid,
coalesce(max(case when homeid is not null then value end),
max(value)
) as value
from t
group by symbol;

How to manipulate SQL Query output

I would like to know if it's possible for example I have one sql table with the following results
+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+
And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?
Well with the answers bellow I managed to get it changed but now how can I echo the value in php?
SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table
Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.
Just use a case statement for translating 1 = yes and 0 = No like this
select ID
,Name
,Number
,case when Active=0 then 'No'
when Active=1 then 'Yes'
end as active_y_n
from table
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
A particularly simple way would use elt():
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t

Mysql Sum Conditional

I'm trying to get the total cost from the table below and if Include_Extra is set, use the value in the corresponding Extra_Seat_Cost column. I'm new to mysql, so any help would be greatly appreciated!
+-----------+-----------------+---------------+
| ID | Cost | Extra_Seat_Cost | Include_Extra |
+-----------+-----------------+---------------+
| 3 | 20 | 15 | 1 |
| 4 | 10 | 5 | 0 |
+----+------+-----------------+---------------+
The result should yield total = $45
You can use SUM with CASE WHEN:
SELECT SUM(Cost + CASE WHEN Include_Extra = 1 --if Include_Extra is bool delete = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
SqlFiddleDemo
I've added COALESCE in case Extra_Seat_Cost can be nullable. number + NULL produces NULL.
If you have grouping column use:
SELECT group_column, SUM(Cost + CASE WHEN Include_Extra = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
GROUP BY group_column;

Return result via column view instead row view in mysql

I want to know if there is a way to return my result via column. Simple Select statement will return like
_________________
| id | name |
| 1 | abc |
| 2 | abc1 |
| 3 | abc2 |
is there a way to retrieve my result like this
| 1 | abc | 2 | abc1| 3 | abc2|
or like
|1 | 2 | 3 |
|abc |abc1|abc2|
i am only returning 4 columns, and i am using the data as header of other queries. Please help me. Thanks
Try This:
select group_concat(id,"|",name SEPARATOR '|') from T;
You can achieve this using PIVOT. You need to use MAX and GROUP BY to simulate PIVOT.
SELECT Id,
MAX(CASE WHEN Id = 1 THEN name END) col1,
MAX(CASE WHEN Id = 2 THEN name END) col2
FROM Table1
GROUP BY Id
You need to decide based on which criteria you need to group the data. Try this example.
Try this..
Query
SELECT
MAX
(
CASE id WHEN 1
THEN name
ELSE NULL END
) AS `1`,
MAX
(
CASE id WHEN 2
THEN name
ELSE NULL END
) AS `2`,
MAX
(
CASE id WHEN 3
THEN name
ELSE NULL END
) AS `3`
FROM tbl;
Demo
I solve my problem because they give me clue, now this is my code, I want to share it
SELECT
MIN( CONCAT(title,"\n", DATE_FORMAT(start_time,'%h:%i %p')) ) col1,
MAX( CONCAT(title,"\n", DATE_FORMAT(start_time,'%h:%i %p')) ) col2
from table
thanks everyone

Rows to columns in mysql

I'm doing the select:
select id,
status,
count(status) as qtd
from user
group by id, status;
The return is:
id | status | qtd
1 YES 5
1 NO 3
2 YES 3
2 NO 1
I want this:
id | YES | NO
1 5 3
2 3 1
Thanks.
NOTE:
you can use case logic to do what you want.. basically you want to pivot the results and to pivot them you have to use aggregates with conditionals to fake a pivot table since mysql doesn't have a way to accomplish that
QUERY:
SELECT
id,
SUM(CASE status WHEN 'Yes' THEN 1 ELSE 0 END) as 'YES',
SUM(CASE status WHEN 'No' THEN 1 ELSE 0 END) as 'NO'
FROM user
GROUP BY id;
DEMO
OUTPUT:
+----+-----+----+
| id | YES | NO |
+----+-----+----+
| 1 | 5 | 3 |
| 2 | 3 | 1 |
+----+-----+----+
You can do so,using expression in sum() like sum(status ='Yes') will result as boolean (0/1) and thus you can have your count based on your criteria you provide in sum function
select id,
sum(status ='Yes') as `YES`,
sum(status ='No') as `NO`
from user
group by id;