m new in mysql
here is my table
now i want to count "count_id" where count of 'questionID' greater than 2
Try this :
SELECT COUNT(count_id) FROM myTable WHERE questionID > 2
select count(Count_ID),QuestionID,SurveyId from table
where QuestionID>2
group by QuestionID,SurveyID
select count(count_id) from yourtable where questionID > 2
If you want to count unique ID:
select count(DISTINCT count_id) from table_name where questionID > 2
SELECT COUNT(count_id) FROM table_name WHERE questionID > 2
Group by Count_ID and count their distinct questions. Stay with those that have more than two. Then count how many IDs you got.
select count(*)
from
(
select count_id
from mytable
group by count_id
having count(distinct questionid) > 2
) x;
EDIT: If count_id + questionid happen to be unique for the table, you can replace count(distinct questionid) with count(*).
You can also try the statement below:
select count(count_id) CountOfID,count_id from mytable
where questionID > 2 group by count_id;
Related
Is it possible to group results and then filter by how many rows are in the group?
Something like this:
SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name
You want to use HAVING to filter on the aggregate function.
SELECT name, COUNT(*)
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
You need to use HAVING
SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1
Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example
You want a HAVING clause.
SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
Use having in your query:
SELECT * FROM mytable GROUP BY name having COUNT(*) > 1
i could not create the correct query.
This is the screenshot from the table
I am trying to count only the unique question_id for each cat_id. SO the output must be
totalQuestion cat_id
2 2
2 1
we can use GROUP BY and get distinct count for the questions
SELECT COUNT(DISTINCT question_id) as total_questions, cat_id
from tableA
group by cat_id
select
count(distinct qid) as totalQuestion
, cid
from table_name group by cid
This question already has answers here:
Count the occurrences of DISTINCT values
(4 answers)
Closed 1 year ago.
How do I write an SQL query to count the total number of a specific num value in the num column of a table?
Assuming we have the following data.
NAME
NUM
SAM
1
BOB
1
JAKE
2
JOHN
4
Take the following query:
SELECT WHERE num = 1;
This would return these two rows.
NAME
NUM
SAM
1
BOB
1
Try
SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME
SQL FIDDLE
If you want to have the result for all values of NUM:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
GROUP BY `NUM`
Or just for one specific:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
WHERE `NUM`=1
FOR SPECIFIC NUM:
SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1
FOR ALL NUM:
SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM
SELECT
COUNT(NUM) as 'result'
FROM
Table1
GROUP BY
NUM
HAVING NUM = 1
Try this Query
select NUM, count(1) as count
from tbl
where num = 1
group by NUM
--having count(1) (You condition)
SQL FIDDLE
SELECT sum(num) WHERE num = 1;
SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';
e.g. for you query:-
SELECT SUM(IF(num=1,1,0)) FROM your_table_name;
Use this query this will give your output:
select
t.name
,( select
count (*) as num_value
from Table
where num =t.num) cnt
from Table t;
I have table with, folowing structure.
tbl
id name
1 AAA
2 BBB
3 BBB
4 BBB
5 AAA
6 CCC
select count(name) c from tbl
group by name having c >1
The query returning this result:
AAA(2) duplicate
BBB(3) duplicate
CCC(1) not duplicate
The names who are duplicates as AAA and BBB. The final result, who I want is count of this duplicate records.
Result should be like this:
Total duplicate products (2)
The approach is to have a nested query that has one line per duplicate, and an outer query returning just the count of the results of the inner query.
SELECT count(*) AS duplicate_count
FROM (
SELECT name FROM tbl
GROUP BY name HAVING COUNT(name) > 1
) AS t
Use IF statement to get your desired output:
SELECT name, COUNT(*) AS times, IF (COUNT(*)>1,"duplicated", "not duplicated") AS duplicated FROM <MY_TABLE> GROUP BY name
Output:
AAA 2 duplicated
BBB 3 duplicated
CCC 1 not duplicated
For List:
SELECT COUNT(`name`) AS adet, name
FROM `tbl` WHERE `status`=1 GROUP BY `name`
ORDER BY `adet` DESC
For Total Count:
SELECT COUNT(*) AS Total
FROM (SELECT COUNT(name) AS cou FROM tbl GROUP BY name HAVING cou>1 ) AS virtual_tbl
// Total: 5
why not just wrap this in a sub-query:
SELECT Count(*) TotalDups
FROM
(
select Name, Count(*)
from yourTable
group by name
having Count(*) > 1
) x
See SQL Fiddle with Demo
The accepted answer counts the number of rows that have duplicates, not the amount of duplicates. If you want to count the actual number of duplicates, use this:
SELECT COALESCE(SUM(rows) - count(1), 0) as dupes FROM(
SELECT COUNT(1) as rows
FROM `yourtable`
GROUP BY `name`
HAVING rows > 1
) x
What this does is total the duplicates in the group by, but then subtracts the amount of records that have duplicates. The reason is the group by total is not all duplicates, one record of each of those groupings is the unique row.
Fiddle: http://sqlfiddle.com/#!2/29639a/3
SQL code is:
SELECT VERSION_ID, PROJECT_ID, VERSION_NO, COUNT(VERSION_NO) AS dup_cnt
FROM MOVEMENTS
GROUP BY VERSION_NO
HAVING (dup_cnt > 1 && PROJECT_ID = 11660)
I'm using this query for my own table in PHP, but it only gives me one result whereas I'd like to the amount of duplicate per username, is that possible?
SELECT count(*) AS duplicate_count
FROM (
SELECT username FROM login_history
GROUP BY username HAVING COUNT(time) > 1
) AS t;
I have a query?
I have a table as
sender_user_id receiver_user_id
2 3
3 2
2 7
2 8
7 3
10 6
2 3
and i want to group the columns by sender_user_id and receiver_user_id also
(sender id=2 and receiver_id=3) and (sender id=3 and receiver_id=2) should be grouped as one column with max timestamp
Is this what you are looking for?
SELECT CONCAT(sender_user_id,' ', receiver_user_id ) as newcolumn, max(datestamp)
from temptable
group by CONCAT(sender_user_id,' ', receiver_user_id ) ;
Really crude, quick, SQLFiddle Solution
Try the GROUP BY as a subquery and do the CONCAT afterwards
SELECT
CONCAT(sender_user_id,' ',receiver_user_id) sender_receiver,
max_datestamp
FROM
(
SELECT sender_user_id,receiver_user_id, max(datestamp) max_datestamp
FROM temptable GROUP BY sender_user_id,receiver_user_id
) A;
Make sure temptable is properly indexed
ALTER TABLE temptable ADD INDEX sender_receiver_ndx (sender_user_id,receiver_user_id);
Try this:
select max(timestamp) from yourTable group by sender_user_id, receiver_user_id
Not sure if you can do this with a GROUP BY combining the two columns. You could try a UNION
SELECT user1,user2,MAX(timestamp) FROM
(SELECT
sender_user_id AS user1,
receiver_user_id AS user2,
timestamp
FROM table
) AS table1
UNION DISTINCT
(SELECT
receiver_user_id AS user1,
sender_user_id AS user2,
timestamp
FROM table) AS table2
GROUP BY user1,user2
try
SELECT *, MAX(timestamp) FROM <my_table> GROUP BY sender_user_id, receiver_user_id
Not clear what you mean. Do you want something like
group by sender_id, reciever_id
order by timestamp desc