I have a table like in the image below.
I'd like to make a query that returns only those rows that contain a non-unique value in field number as text.
First I tried to get the opposite (unique values only) with Distinct but that returns only the values from field number as text while I need to have returned the whole row.
But in the end it is the non-unique values that I am after, so I am stuck.
The desired outcome is as follows:
Already found the answer:
Criteria:
In (SELECT [number as text] FROM [Tabel1] As Tmp GROUP BY [number as text] HAVING Count(*)>1 )
Related
When I run the following query, I am returned two entries with duplicate results. Why are duplicate results returned when I’m using distinct here? The primary keys are the house number, street name, and unit number.
SELECT distinct
house_num,
Street_name,
Unit_Designator,
Unit_Num
FROM voterinfo.voter_info
WHERE house_num = 420
AND street_name = "PARK"
AND Unit_Num = ''
AND Unit_Designator = '';
select distinct is a statement that ensures that the result set has no duplicate rows. That is, it filters out rows where every column is the same (and NULL values are considered equal).
It does not look at a subset of columns.
Sometimes, people use select distinct and don't realize that it applies to all columns. It is rather amusing when the first column is in parentheses -- as if parentheses make a difference (they don't).
Then, you might also have situations where values look the same but are not.
Consider this simple example where values differ by only a space as the end of string:
select distinct x
from (select 'a' as x union all
select 'a '
) y;
Here is a db<>fiddle with this example.
This returns two rows, not 1.
Without sample data it is hard to say which of these situations you are referring to. But the rows that you think are "identical" really are not.
For the fields with datatype as Char or similar ( Street_name,Unit_Designator) it is possible that there are spaces that aren't visible in the query editor that are to be removed by applying appropriate trimming logic.Please refer below link,
MySQL select fields containing leading or trailing whitespace
i have table like this
Table
I just want to display the data based on the most data appearing in the second field and I do not want to display a little data. So the data appears only the most and count the total value in the third field. And I've tried but no results.
How to query the database to display the most data only
You can do something like this,
SELECT `col_name_of_frequent_value`,
COUNT(`col_name_of_frequent_value`) AS `frequent_value`
FROM `table_name`
GROUP BY `col_name_of_frequent_value`
ORDER BY `frequent_value` DESC // This will sort the result by putting max count at top.
LIMIT 1; // This will only show the TOP-Most value in sorted result.
Please change the names as per your table structure.
SELECT
kd_masalah, total_bobot
FROM
(SELECT -- SubQuery
SUM(bobot) AS total_bobot, -- Gives the sum of values in bobot column relating to a particular group (grouped column)
kd_masalah
FROM
your_table_name
GROUP BY kd_masalah -- Grouping of same values in the column
)AS s having MAX(total_bobot); -- return the values having max sum in total_bobot
I am trying to query from view to remove the row that has null values on column value if only the column name has duplicate row count.
So here is my sample result from my view and what I expect is the highlighted row should not be in the result, just want to have recommendation and suggestion how can I easily remove that highlighted row using query because right now I am removing through my application code.
Select * from TblView1
If I understand the question correctly, you want to:
Include all rows with a unique Name
Include rows without null for rows with non-unique Names
I believe this should work:
select * from TblView1 where Name in
(select Name from TblView1 group by Name having count(*) = 1)
or location is not null
Below is the data in my table:
TABLE:
abc-ac
abc-dc
aax-i
bcs-o-dc
ddd-o-poe-dc
I need to write a query which will display only the unique entries as a result:
abc-ac
aax-i
bcs-o-dc
ddd-o-poe-dc
So basically, since the first two entries start with "abc", it should be treated as one and displayed.
Thanks.
If you're not picky about which one of the two abc-* records that it shows you can use this:
SELECT f1 FROM mytable GROUP BY substring_index(f1, '-', 1)
SQLFiddle Here
That substring_index() function will split the value in your field by - and return the first bit. So essentially your records get grouped by only the first part. This is one of the few times that we can take advantage of MySQLs strange GROUP BY behavior where it will allow you to leave out non-aggregated fields from the group by.
As the title suggests I have a MySQL query like this:
SELECT DISTINCT `friendly_url` FROM `post` WHERE `description` LIKE ? OR `heading` LIKE ? ORDER BY `friendly_url`
I have given the string '%' wild card in the parameters so that this works as a Search function. How ever, Say a user is searching for a common word like 'is' and it appears in both heading and description in the same post. Then this query returns the same post twice. I don't want that to happen, hence the 'DISTINCT'.
Why does this happen? Any way I can work around to make it work the way i want?
The query is not returning the same row twice. The predicates in the WHERE clause are evaulated against each row, and either the row is returned, or it's not. No combination of OR conditions is going to cause MySQL to return the "same row" multiple times.
If you are getting duplicate values of friendly_url, then you have multiple rows in the post table that have the same value for friendly_url. (i.e. friendly_url column is not unique in the post table.)
You don't have to use the DISTINCT keyword to remove duplicates from the resultset. You could remove the DISTINCT keyword, and add GROUP BY friendly_url before the ORDER BY clause.
To identify the "duplicated" values of friendly_url, and how many rows have that same value:
SELECT p.friendly_url
, COUNT(1)
FROM post p
GROUP BY p.friendly_url
HAVING COUNT(1) > 1