My subreport goes through a list of ID's. Each ID has a location assigned to it. The report is grouped by the location (Group #1), then by ID (Group #2).
In the table, the ID's should look like:
14600
14602
14602
14602
14700
14703
14704
14704
My desired output would be 2 because there are 2 ID's with more than one entry. How can I easily calculate this?
You can try this:
DECLARE #temp table(num integer);
INSERT INTO #temp(num) VALUES (14600), (14602), (14602), (14602), (14700), (14703), (14704), (14704);
SELECT COUNT(distinct num) repeats
FROM
(SELECT
num, count(num) as counts
FROM #temp
GROUP BY num
HAVING COUNT(num) > 1) a;
Sounds like you're looking for a combination of COUNT and DISTINCT, in conjunction with ensuring that there is more than one occurrence. You can accomplish this with something like:
SELECT COUNT(DISTINCT id) FROM table_name HAVING COUNT(*) > 1
This returns the number of distinct IDs in the table. In your case, this is 2.
Related
What I mean is, I have table with a "list" column. The data that goes into the "list" is related to addresses, so I sometimes get repeated zip codes for one record in that field.
For example, "12345,12345,12345,12456".
I want to know if it's possible to construct a query that would find the records that have an unknown string that duplicates within the field, such that I would get the records like "12345,12345,12345,12456", but not ones like "12345,45678,09876".
I hope that makes sense.
Yes, it is possible. You need to use a numbers table to convert your delimited string into rows, then use group by to find duplicates, e.g.
CREATE TABLE T (ID INT, List VARCHAR(100));
INSERT INTO T (ID, List)
VALUES (1, '12345,12345,12345,12456'), (2, '12345,45678,09876');
SELECT
T.ID,
SUBSTRING_INDEX(SUBSTRING_INDEX(T.list, ',', n.Number), ',', -1) AS ListItem
FROM T
INNER JOIN
( SELECT 1 AS Number UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
) AS n
ON CHAR_LENGTH(T.list)-CHAR_LENGTH(REPLACE(T.list, ',', ''))>=n.Number-1
GROUP BY T.ID, ListItem
HAVING COUNT(*) > 1;
If you don't have a numbers table you can create one in a derived query as I have above with UNION ALL
Example on DB Fiddle
With that being said, this is almost certainly not the right way to store your data, you should instead use a child table, e.g.
CREATE TABLE ListItems
(
MainTableId INT NOT NULL, --Foreign Key to your current table
ItemName VARCHAR(10) NOT NULL -- Or whatever data type you need
);
Then your query is much more simple:
SELECT T.ID, li.ItemName
FROM T
INNER JOIN ListItems AS li
ON li.MainTableId = T.ID
GROUP BY T.ID, li.ItemName
HAVING COUNT(*) > 1;
If you need to recreate your original format, this is easily done with GROUP_CONCAT():
SELECT T.ID,
GROUP_CONCAT(li.ItemName) AS List
FROM T
INNER JOIN ListItems AS li
ON li.MainTableId = T.ID
GROUP BY T.ID;
Example on DB Fiddle
I am still unclear what your desired result is based on your question however if it is simply to get all rows where there is a duplicate entry in column list you could do the following:
SELECT * FROM TABLE
WHERE COLUMN IN
(SELECT COLUMN FROM TABLE
having count(*) >1)
Select count(*) from usa.adult group by `income`;
income col has mainly two values i.e >50k and <50k
which is found in the result as
how can i find which value represent >50k and <50k.
desired output:
2: https://i.stack.imgur.com/VKTKE.png
as said from #JNevill extract columns directly
Select income, count(*) from usa.adult group by income;
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
All I need to do is to count all records that match this query... that is the only info I need.. is the count of the records... what is the most efficient way to do that?
SELECT id, country, city
FROM sales
WHERE country='Germany'
AND city='Munich'
AND closed<>0
I mean, I assume this is correct but is there a more efficient way to do this? I will be running this counting query often...
SELECT COUNT(*)
FROM sales
WHERE country='Germany'
AND city='Munich'
AND closed<>0
Should I use * or something more specific?
If you need to count the Null value also for your record count then use count(*) or if ignore the Null value to be counted then use count().
DECLARE #AA AS TABLE (ID INT, NAME VARCHAR(10))
INSERT INTO #AA VALUES(1,NULL)
INSERT INTO #AA VALUES(2,'A')
INSERT INTO #AA VALUES(3,'B')
INSERT INTO #AA VALUES(4,'C')
SELECT * FROM #AA
SELECT COUNT(*) FROM #AA
SELECT COUNT(NAME) FROM #AA
As you execute the above query you can find that there will be four records in #AA table but if you use COUNT(*) if will result 4 and if you use COUNT(NAME) it will result 3 this example is for SQL Server, but the mysql will do same.
Please refer the following link.
MySQL: Fastest way to count number of rows
Hope this helps you.
I have a query with many wheres and orders criterias. One of the fields of the select is 'price' (element price) but I would like to have also (in every row) the maximun price of all the selected elements.
I tried to include MAX aggregate function on select hoping that this will return desired value, but insetead of that, price and MAX(price) returns the same. Searching into MySql doc I found the reason:
If you use a group function in a
statement containing no GROUP BY
clause, it is equivalent to grouping
on all rows.
Is there a way to solve this problem?
Thanks in advance!
There's a similar question (but not resolving this): find max value without aggregate operator in mysql
You can do this:
SELECT
id,
price,
(SELECT MAX(price) FROM your_table) AS max_price
FROM your_table
I'm not sure why you'd want to return that value on every row though... I'd probably do this in two separate queries, or else use a UNION ALL:
SELECT id, price FROM your_table
UNION ALL
SELECT NULL, MAX(price) FROM your_table
Maybe you could use a stored procedure like so:
CREATE PROCEDURE QueryWithMax ([parameter list if necessary])
BEGIN
-- Obtain the maximum price
DECLARE x INT UNSIGNED; -- change datatype if appropriate
SET x = SELECT
MAX(price)
FROM
...
WHERE
...
;
-- Now do your query
SELECT
price,
[other columns],
x AS MaxPrice
FROM
...
WHERE
...
GROUP BY
...
;
END
I haven't tried this, but if you had a subquery that extracts the maximum price (so you get one row), and then do a cross join (cartesian product) with it. You should get something like what you want. I can't vouch for how fast this would be.