I've a table:-
id value
1 1
1 2
1 1
1 4
2 1
2 3
2 2
3 1
4 2
I want to get the id and their respective count having value = 1, ie For the above table, the output should be the following:-
id count
1 2
2 1
3 1
4 0
Since, id = 1 has 2 entries of 1, id = 2 has 1 entry of 1, similarly id 3 has 1 entry, id 4 has 0 entry.
I'm get the following output using the following que:-
select id,count(value)
from table
where value=1
group by id
order by count(value);
id count
1 2
2 1
3 1
I want to get the 4, 0 entry as well. How can I do that?
Try this
select id,sum(case when value ='1' then 1 else 0 end) as val
from table
group by id
order by id;
You try this Sql Query
select id,COUNT(CASE WHEN value = '1' THEN 1 END) count
from tablename
group by id;
It is the where clause that remove the row id=4
try following:
select id,sum(case when value = 1 then 1 else 0 end) as count
from table
group by id
order by sum(case when value = 1 then 1 else 0 end);
Related
I have sample Data Set
ID Name Active
1 Mii 0
1 Mii 1
2 Rii 0
2 Rii 1
3 Lii 0
4 Kii 0
4 Kii 1
5 Sii 0
How I can get active records along with Inactive records for other ID's.
ID Name Active
1 Mii 1
2 Rii 1
3 Lii 0
4 Kii 1
5 Sii 0
I have taken all the data into 2 temp tables because lot of joins are there
select * from tmp1 where active = 1
UNION ALL
select * from tmp2 where active = 0 AND
NOT EXISTS (SELECT 1 FROM tmp1 WHERE Active = 1 )
can anyone tell me is there any better way to write in MYSQL
Assuming that active can only be 0 or 1, aggregation could help:
SELECT id,
name,
max(active) active
FROM elbat
GROUP BY id,
name;
max(active) is 1, if there is a record with the id and name that has a 1 in active, as 1 > 0. Otherwise it is 0, the only value.
Using analytical functions:
select * from (
SELECT ID, NAME, RANK() OVER ( ORDER BY ACTIVE desc) AS RN
FROM TABLE1) a where rn = 1;
I have two table. I want count client_id from tbl_appointment_book but tbl_appointment_book's id and tbl_appointment_service's appointment_id should be match.
tbl_appointment_book
id appointment_date client_id status
1 2016-05-11 1 1
2 2016-05-12 1 1
tbl_appointment_service
id appointment_id service_id team_id
1 1 1 1
2 1 2 1
3 1 8 5
4 2 1 1
5 2 1 2
I want to count client_id from tbl_appointment_book condition
appointment_date=2016-05-11, team_id=1
You can use JOIN to achieve your wanted result:
SELECT count(a.client_id) FROM tbl_appointment_book a
JOIN tbl_appointment_service b
ON a.id = b.appointment_id
WHERE a.appointment_date = '2016-05-11' AND b.team_id = 1
Result will be the number of rows returned.
Try the below query, it should work fine.
SELECT COUNT (tab.client_id)
FROM tbl_appointment_book tab
JOIN tbl_appointment_service tas
ON tab.id = tas.appointment_id
WHERE tab.appointment_date = '2016-05-11' AND tas.team_id = 1;
I have a table like this:
table_documents
document_id
document_folder_id
document_title
document_notify_expired
ID FOLDER TITLE Notify Expired
1 2 Test1 1
2 2 Test2 1
3 2 Test3 1
4 2 Test4 1
5 2 Test5 1
I'm like to UPDATE and set document_notify_expired to 0 for all records EXCEPT last, for a specific folder like below
ID FOLDER TITLE Notify Expired
1 2 Test1 0
2 2 Test2 0
3 2 Test3 0
4 2 Test4 0
5 2 Test5 1
Here my code but not update as expected
UPDATE table_documents docs
LEFT OUTER JOIN ( SELECT * FROM table_documents ORDER BY document_id DESC LIMIT 1 )last_doc ON last_doc.document_id = docs.document_id
SET doc.document_notify_expired = '0'
WHERE document_folder_id = '2'
AND last_doc.document_notify_expired = '1'
Try this out
UPDATE table_documents docs
INNER JOIN
(SELECT
MAX(id) id
FROM
table_documents) docsmax ON docs.id != docsmax.id
SET
document_notify_expired = 0;
Obviously the last row has the greatest id, so this row is not going to be there after the join, which will returns all the other rows and you can play with them as you wish.
update table1.table_documents
set table1.document_notify_expired = 0
where table1.document_folder_id = 2
and not table1.document_id = (
select table2.document_id
from table_documents as table2
where table2.document_folder_id = 2
order by table2.document_id desc
limit 1
);
Here is my table with sample data:
id|group_id|column1|column2|column3|
--------------------------------------
1 1 | 1 1 0
2 1 | 1 1 1
3 2 | 0 0 0
4 2 | 1 1 1
5 1 | 1 0 0
6 3 | 0 0 0
Expected result set: (The result should show the maximum sum (col 1 + col 2 + col 3) in each group)
id|group_id|column1|column2|column3|
--------------------------------------
2 1 | 1 1 1
4 2 | 1 1 1
6 3 | 0 0 0
Actual result set: (select *, max(m.column1 + m.column2 + m.column3) as total from my_table m group by m.group_id) which is wrong
id|group_id|column1|column2|column3|total|
------------------------------------------
1 1 | 1 1 0 3
3 2 | 0 0 0 3
6 3 | 0 0 0 0
I'm quite new to SQL, it seems like the query selecting the first id in each group.
what is the best way to get expected result?
You can do get your desired result i.e. with a subselect:
First step:
SELECT
MAX(m1.column1 + m1.column2 + m1.column3)
FROM
my_table m1
GROUP BY
m1.group_id
will get you the maximum total per group_id.
Because values in non aggregated columns are indetermined, if this columns contains different values for a group, you can't simply aggregate as you've done, but a subselect using the query from first step will do it:
Complete query
SELECT
*,
m.column1 + m.column2 + m.column3 as total
FROM
my_table m
WHERE
m.column1 + m.column2 + m.column3 = (
SELECT
MAX(m1.column1 + m1.column2 + m1.column3)
FROM
my_table m1
WHERE
m.group_id = m1.group_id
GROUP BY
m1.group_id
);
Demo
Looks like you are trying to get the rows that yield highest sum of 3 columns in each group:
select a.*
from my_table a
join (
select group_id, max(column1 + column2 + column3) summation
from my_table
group by group_id) b on a.group_id = b.group_id and a.column1 + a.column2 + a.column3 = b.summation;
You didn't specified exactly which values for column1, column2 and column3 must be returned.
select m.*,n.* from
m,
(select
group_id as n_group_id,
max(column1+column2+column3) as n_total
from m group by group_id) as n
where m.group_id=n_group_id and (column1+column2+column3)=n_total
ID GRP VAL CHK
--- ----- ----- ----
1 1 1 0
2 1 3 0
3 2 7 0
4 2 2 0
5 2 1 0
6 3 5 0
I want to set my CHK field to '1' having maximum value VAL for every group of GRP,
so ID 2,3,6 should be set.
I don't write my trials here, all seems rubbish :)
In MySQL, you can do this using the update/join syntax:
update table t join
(select grp, max(val) as maxval
from table t
group by grp
) tmax
on t.grp = tmax.grp and t.val = tmax.maxval
set t.chk = 1;