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
Related
I have a table which consists of multiple columns.
Table consists of data like
Group no
Maxtime
Updatedtime
A22
20221502
20221702
A22
20212502
20221702
A22
20212502
20221702
I query that table with a condition like
Select Group no from cnt where maxtime<=updatedtime
The output comes A22
Now I want to use this output to query the same table again and get the count of A22 which is 3 with a condition in where clause where I use other columns of the table.
Something like
Select count(group no)
From cnt
Where (effdate<candate)
Effdate and candate are columns of the same table.
I suppose you want this: count the rows with effdate < candate for each group_no for which exists a row with maxtime <= updatedtime.
Select group_no, count(*)
from cnt
where effdate < candate
and group_no in (select group_no from cnt where maxtime <= updatedtime)
group by group_no
order by group_no;
Use GROUP BY to get the counts of each group. And combine both conditions with AND.
SELECT group_no, COUNT(*)
FROM cnt
WHERE maxtime <= updatedtime
AND effdate < candate
GROUP BY group_no
I have two MySQL tables A and B both with this schema
ID
entity_id
asset
asset_type
0
12345
x
1
..
.........
.....
..........
I would like to get an aggregated top 10/50/whatever entity_ids with the largest row count difference between the two tables. I think I could do this manually by just getting the highest row count by entity_id like so
select count(*), entity_id
-> from A
-> group by entity_id
-> order by count(*) desc;
and just manually comparing to the same query for table B but I'm wondering if there's a way to do this in just one query, that compares row counts for each distinct entity_id and aggregates the differences between row counts. A few notes
There is an index on entity_id for both tables
Table B will always have an equivalent or greater number of rows for each entity_id
Sample output
entity_id
difference
12345
100
3232
75
5992
40
and so on
for top 10/50
Aggregate in each table and join the results to get the difference:
SELECT a.entity_id, b.counter - a.counter diff
FROM (SELECT entity_id, COUNT(*) counter FROM A GROUP BY entity_id) a
INNER JOIN (SELECT entity_id, COUNT(*) counter FROM B GROUP BY entity_id) b
ON a.entity_id = b.entity_id
ORDER BY diff DESC LIMIT 10
I have a query that returns following values:
TemplateCode Total
1418 35
7419 31
7418 31
8325 17
15623 17
4997 17
I want all the rows with top 3 Total values
In my query if I include LIMIT 3 in the query then it gives only 3 which I don't want. I don't want to include LIMIT because count may vary from time to time.
How can I include condition on Total and always get top 3 count values
My current query is like:
select TemplateCode, count(*) as Total from table group by TemplateCode
order by Total desc
limit 3
I think this does what you want:
select t.*
from t
where t.Total >= (select distinct t2.Total
from t t2
order by t2.Total desc
limit 2, 1
);
This assumes that you want the third distinct value. If you just want the third value, remove the distinct.
You could use a inner join on subquery for the top 3 total
select m.TemplateCode , m.total
from my_table m
inner join (
select Total
from my_table
order by Total Desc
limit 3
) t on t.total = m.total
order by m.total, m.TemplateCode
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
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