TABLE quotation
id clientid
1 25
2 25
3 25
4 25
5 26
How can I query how many different clients exist in TABLE quotation? I don't want duplicate entries to be counted more than once.
I need the answer to be 2, in 1 row, because the only non-duplicated entries are (25, 26).
select count(distinct clientid) from quotation
read more
I find a way out
SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY
clientid) t1
If you want to count the total number of unique entries this will return a number in column count.
SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY clientid having count(*) > 1) t1
If you want to count the total number of entries with duplicate rows then use the following mysql.
SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY clientid having count(*) >= 2) t1
I tried the following on a MySQL 5.x database.
id is an integer and clientid is an integer. I populated with two rows:
id clientid
1 25
2 25
This SQL query will print the rows that have exactly 2 elements:
select * from test1 group by clientid having count(*) = 2;
If you want 2 or more elements, replace = 2 in the example above with >= 2.
select * from test1 group by clientid having count(*) >= 2;
SELECT clientid, COUNT(clientid) FROM quotation
GROUP BY clientid
Related
I have data in a MySQL table in the following format. I want to retrieve the count in two different conditions as shown in the query below, I want to combine these queries into a single one, by which I mean I would like the first query result in one column and second query result in another column, as so:
Expected output:
count totalcount
--------------------------
3 6
Queries:
select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14';
select count(*) as totalcount from entries ;
Data in mysql table:
id date
------------------------
1 2014-08-14
2 2014-08-13
3 2014-08-12
4 2014-08-11
5 2014-08-10
6 2014-08-09
sql fiddle http://sqlfiddle.com/#!2/faeb26/6
select sum(date between '2014-08-12' and '2014-08-14'), count(*) as totalcount from entries ;
The boolean expression in SUM() equals to true or false, 1 or 0. Therefore just use SUM() instead of COUNT().
Just put the two queries together:
select count(*) as count, b.totalcount from entries,
(select count(*) as totalcount from entries) b
where date between '2014-08-12' and '2014-08-14';
select sum(c) as count, sum(tc) as totalcount
from (select count(*) as c , 0 as tc from entries where date between '2014-08-12' and '2014-08-14'
union all
select 0 as c, count(*) as tc from entries)
simple combine to result in on other select query try this
SELECT (select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14'
) as count, (select count(*) as totalcount from entries) as totalcount;
DEMO LINK
I have a MySql table with a non-Unique Name Field, I'd like to query the following;
For each Name Value ("John") count how many records there are (32 records with name John), I'll end up writing this to each record.
Average the counts for each name group (e.g 32 records with John and 42 records with Sally, Average for table 37 records)
Count counts meaning e.g 6 10 Record groups, 10 20-record groups, etc
Can I do this with one or more queries?
Average for Table Groups http://sqlfiddle.com/#!2/f5af3/5:
SELECT
AVG(ct)
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1;
Count per count http://sqlfiddle.com/#!2/b0628/1:
SELECT
COUNT(ct) AS "Count_per_count",
ct as "Actual Count"
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1
GROUP BY ct;
select name, count(*) as num from table group by name;
select ((select count(*) from table) / (select count(distinct name) from table));
select count(*) as num, type from
(select count(*) as type from table group by name) t
group by type
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;
I am trying to get multiple columns and show duplicate ItemCodes form a table. Ie. if an ItemCode is repeated more than once.
Using the query:
SELECT ItemCode
,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
ItemCode
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I get 38 records with duplicate counts of 2 each.
When I try to get another column, ItemName, I get only 37 records returned:
SELECT ItemCode
,ItemName
,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
ItemCode
,ItemName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
How do I correctly modify the first query to get multiple columns and still return 38 records?
Inner join your first query (by putting it in a derived table) back to the main table, joining on the ItemCode.
SELECT
I.*,
D.DuplicateCount
FROM
IQR1 I
INNER JOIN (
SELECT
ItemCode,
DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY ItemCode
HAVING COUNT(*) > 1
) D ON I.ItemCode = D.ItemCode
ORDER BY DuplicateCount DESC
Let's say i have a simple table voting with columns
id(primaryKey),token(int),candidate(int),rank(int).
I want to extract all rows having specific rank,grouped by candidate and most importantly only with minimum count(*).
So far i have reached
SELECT candidate, count( * ) AS count
FROM voting
WHERE rank =1
AND candidate <200
GROUP BY candidate
HAVING count = min( count )
But,it is returning empty set.If i replace min(count) with actual minimum value it works properly.
I have also tried
SELECT candidate,min(count)
FROM (SELECT candidate,count(*) AS count
FROM voting
where rank = 1
AND candidate < 200
group by candidate
order by count(*)
) AS temp
But this resulted in only 1 row,I have 3 rows with same min count but with different candidates.I want all these 3 rows.
Can anyone help me.The no.of rows with same minimum count(*) value will also help.
Sample is quite a big,so i am showing some dummy values
1 $sampleToken1 101 1
2 $sampleToken2 102 1
3 $sampleToken3 103 1
4 $sampleToken4 102 1
Here ,when grouped according to candidate there are 3 rows combining with count( * ) results
candidate count( * )
101 1
103 1
102 2
I want the top 2 rows to be showed i.e with count(*) = 1 or whatever is the minimum
Try to use this script as pattern -
-- find minimum count
SELECT MIN(cnt) INTO #min FROM (SELECT COUNT(*) cnt FROM voting GROUP BY candidate) t;
-- show records with minimum count
SELECT * FROM voting t1
JOIN (SELECT id FROM voting GROUP BY candidate HAVING COUNT(*) = #min) t2
ON t1.candidate = t2.candidate;
Remove your HAVING keyword completely, it is not correctly written.
and add SUB SELECT into the where clause to fit that criteria.
(ie. select cand, count(*) as count from voting where rank = 1 and count = (select ..... )
The HAVING keyword can not use the MIN function in the way you are trying. Replace the MIN function with an absolute value such as HAVING count > 10