My database has a simple table, with two columns, BID and Value. There may be some records in the database with same BID, like this:
BID Value
Record1: BID = 1, Value = 0
Record2: BID = 1, Value = 3
Record3: BID = 2, Value = 4
Record4: BID = 2, Value = 5
Record5: BID = 2, Value = 6
Record6: BID = 3, Value = 7
Now I want to write a SQL query that can get the total count of the BID that has more than one corresponding records.
In the above sample, BID 1 has 2 corresponding records, and BID 2 has 3. So the total count of BID values that has more than one corresponding records is 2.
How to write such a SQL query?
You are looking for an aggregation query.
SELECT COUNT(*)
FROM (
SELECT BID, COUNT(*) cnt
FROM your_table
GROUP BY BID
HAVING COUNT(*) > 1
) q
The inner query here groups your bids together, counts how many in each group, and then selects only those groups with more than 1 value.
The outer query simply counts the results of that query.
You can see how it works here: http://sqlfiddle.com/#!9/0c9e6/3
declare #t table (bid int,val int)
insert into #t (bid,val)values (1,0),(1,3),(2,4),(2,5),(2,6),(3,7)
;with cte as (
select bid,
val,ROW_NUMBER()OVER(PARTITION BY bid order by val )RN from #t
)
select COUNT( DISTINCT bid) from cte
where RN > 1
you need only 2 count one for checking the count of values for each id and the other for all ids who has more than one value, you don't need to use having:
SELECT COUNT(*)
FROM (
SELECT BID, COUNT(*) CNT
FROM your_table
GROUP BY BID
) q
WHERE CNT>1
Related
I have a table like this
I want to check the all rows in Column A with column B and get the count of duplicates.
For example, I want to get the
count of 12 as 3(2 times in A+1 time in B)
count of 11 as 2(2 times in A+0 time in B)
count of 13 as 2(1 time in A+0 time in B)
How can I acheive it?
You can calculate the total occurrences from a union all. A where clause can show only the values that occur in the A column:
select nr
, count(*)
from (
select A as nr
from YourTable
union all
select B
from YourTable
) sub
where nr in -- only values that occur at least once in the A column
(
select A
from YourTable
)
group by
nr
having count(*) > 1 -- show only duplicates
You can combine all values in A and B then do the group by.
Then only select those values found in column A.
Select A, count(A) as cnt
From (
Select A
from yourTable
Union All
Select B
from yourTable) t
Where t.A in
(select distinct A from yourTable)
Group by t.A
Order by t.A;
Result:
A cnt
11 2
12 3
13 1
See demo: http://sqlfiddle.com/#!9/9fcfe9/3
I have a table in which I have 3 columns
ID Amount District
1 100 A
2 500 B
1 250 A
2 240 B
1 100 A
Now I want to display Subtotal of distinct district and Grand total. Please guide me how should I write my sql query.
I want to show output as-
District Subtotal Grand Total
A 450
B 740 1190
You can use the SUM function to get the grand total, like this:
SELECT SUM(Amount) FROM data;
That'll return a single row containing a single number, the total of all of the Amounts in your entire table.
To get the per-district total, combine SUM with a GROUP BY clause:
SELECT SUM(Amount), District FROM data GROUP BY District;
This will return a table with one row for each district containing the total for that district.
Declaring the sample table and insert some data
DECLARE #tbl TABLE (ID INT, Amount INT, District CHAR(1))
INSERT #tbl
SELECT 1, 100, 'A' UNION ALL
SELECT 2, 500, 'B' UNION ALL
SELECT 1, 250, 'A' UNION ALL
SELECT 2, 240, 'B' UNION ALL
SELECT 1, 100, 'A'
Query
SELECT DISTINCT t.District,
CASE WHEN ID = (SELECT TOP 1 ID FROM #tbl
WHERE District = t.District
ORDER BY ID DESC)
THEN (SELECT SUM(Amount)
FROM #tbl
WHERE ID <= t.ID
AND District = t.District)
ELSE ' ' END AS [Subtotal],
CASE WHEN ID = (SELECT TOP 1 ID FROM #tbl
ORDER BY District DESC)
THEN (SELECT SUM(Amount)
FROM #tbl)
ELSE ' ' END AS [Grand Total]
FROM #tbl AS t
You can do the same thing for MySQL but instead of using TOP you need to use LIMIT
If you want the data in this format you can try using below code :
SELECT District As 'District', SUM(Amount) as 'Total'
FROM tbl
GROUP BY District
UNION
SELECT 'Grand Total', sum(Amount)
FROM tbl
I would like to select multiple values from a single column in a database table that equal to a number of values. I want all these values to match otherwise it should return no rows. I do not want to use "IN" as that is equal to "OR".
The following is a basic mockup of what it should do but it needs to be dynamic as I wish to use it with a PDO statement. If the database only contains id's 1 and 2 it should fail ie return no rows.
SELECT
id
FROM
reports
WHERE
id=1 AND id=2 AND id=3
I have the current code as follow which is incorrectly returning zero rows:
SELECT id,title
FROM reports
WHERE id IN (1,2)
GROUP BY title
HAVING COUNT(DISTINCT id) = 2
My current table structure is as follows:
http://www.sqlfiddle.com/#!2/ce4aa/1
You have to use HAVING COUNT(id) = 3 to ensure that the selected rows have all the three id's. Something like:
SELECT *
FROM reports
WHERE id = 1 OR id = 2 OR id = 3 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3;
Or:
SELECT *
FROM reports
WHERE SomeOtherField IN (SELECT SomeOtherField
FROM reports
WHERE id = 1 or id = 2 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3
);
Note that: You have to GROUP BY SomeOtherField where SomeOtherField is other field than id because if you GROUP BY id with HAVING COUNT(id) you won't get any records, since COUNT(id) will be always = 1.
Edit: fixed WHERE clause, OR's instead of AND's.
SQL Fiddle Demo
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;
We read values from a set of sensors, occasionally a reading or two is lost for a particular sensor , so now and again I run a query to see if all sensors have the same record count.
GROUP BY sensor_id HAVING COUNT(*) != xxx;
So I run a query once to visually get a value of xxx and then run it again to see if any vary.
But is there any clever way of doing this automatically in a single query?
You could do:
HAVING COUNT(*) != (SELECT MAX(count) FROM (
SELECT COUNT(*) AS count FROM my_table GROUP BY sensor_id
) t)
Or else group again by the count in each group (and ignore the first result):
SELECT count, GROUP_CONCAT(sensor_id) AS sensors
FROM (
SELECT sensor_id, COUNT(*) AS count FROM my_table GROUP BY sensor_id
) t
GROUP BY count
ORDER BY count DESC
LIMIT 1, 18446744073709551615
SELECT sensor_id,COUNT(*) AS count
FROM table
GROUP BY sensor_id
ORDER BY count
Will show a list of the sensor_id along with a count of all the records it has, you can then manually check to see if any vary.
SELECT * FROM (
SELECT sensor_id,COUNT(*) AS count
FROM table
GROUP BY sensor_id
) AS t1
GROUP BY count
Will show all the counts that vary, but the group by will lose information about which sensor_ids have which counts.
---EDIT---
Taken a bit from both mine and eggyal's answer and created this, for the count that is most frequent I call the id default, and then for any values that stand out I have given them separate rows. This way you maintain the readability of a table if you have many results Multi Row, but also have a simple one row column if all counts are the same One Row. If however you are happy with the concocted strings then go with eggyal's answer.
Might be a bit over the top but here goes:
select 'default' as id,t5.c1 as count from(
select id,count(*) as c1 from your_table group by id having count(*)=
(select t4.count from
(
select max(t3.count2) as max,t3.count as count from
(
select count(*) as count2,t2.count from
(
SELECT id,COUNT(*) AS count
FROM your_table
GROUP BY id
) as t2
GROUP BY count
) as t3
) as t4)) as t5 group by count
union all
select t5.id as id,t5.c1 as count from(
select id,count(*) as c1 from your_table group by id having count(*)<>
(select t4.count from
(
select max(t3.count2) as max,t3.count as count from
(
select count(*) as count2,t2.count from
(
SELECT id,COUNT(*) AS count
FROM your_table
GROUP BY id
) as t2
GROUP BY count
) as t3
) as t4)) as t5