I have column "name" with the same values.
For example:
select * table WHERE name Like 'Kate'
Result: 2 rows with this name.
I need leave only one row that has this name (random) in such rows.
And don't show other entries that have the same names.
How can I do this? Thanks.
you can use limit when you delete
DELETE FROM table WHERE name Like 'Kate' limit 1
delete from QQNAMES
where name like 'Kate%'
AND ID <> (select id from QQNAMES
where name like 'Kate%'
and ROWNum <=1);
Will delete all names in 'Kate%' pattern except the first one
Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM. from: w3schools
Try something like that;
delete from table where Id IN(
select Id from table WHERE name Like 'Kate' limit 1)
Related
I have an employee table with their general information. I don't have a modified date column. I cannot add any more columns.
How do I find the last modified records?
If there is a DateModified column, or something like that, and you want the 10 most recent rows (for example) you could use a query like:
SELECT Top 10 * FROM myTable ORDER BY DateModified DESC;
You don't specify the flavor of SQL, so that query might be somewhat different if you're in Oracle, SQL Server, or MS Access.
If you don't have a column like that, and you want to know the most recently added records, if the primary key is an autoincrementing number, you could do the same thing, but sort by PrimaryKey descending. If you truly want modified (i.e., updated) records, and you don't have a DateModified column, you're out of luck. There's no way to know.
Use MAX Aggregate function :
SELECT MAX(Modifieddate)
FROM your_table
WHERE your_conditions.
Use this Code :
SELECT *
FROM Update_Table UT1
WHERE UT1.Modified_Date = (SELECT MAX(UT2.Modified_Date)
FROM Update_Table UT2
WHERE UT2.PK = UT1.PK
AND UT2.Last_User = user)
Use this code to get the last updated time.
select top 1 a.object_id, b.name, a.last_user_update from sys.dm_db_index_usage_stats as a, sys.objects as b where a.object_id=b.object_id and b.name = 'your_table_name' order by last_user_update desc
If I have the following table:
my_table:
varchar name
int score
and I want to do the following query:
select name from my_table group by name having count(*)>3
union
select name from my_table where name like '%xyz'
Is there a way doing this query without a union?
To be clear: I want all names which have more than three entries in the table OR answer the like clause '%xyz'.
The below query will only grab names that match the like clause before grouping:
SELECT name,count(*) as num FROM my_table GROUP BY name HAVING num > 3 OR name like '%xyz'
EDIT: The above query has been altered to allow for either the name or the num clauses to cause the row to be accepted.
Unions are only used when linking two tables.
I think you are looking for something like this?
SELECT name,count(*) as num
FROM my_table
GROUP BY name
HAVING num > 3 OR name like '%xyz'
http://sqlfiddle.com/#!9/1b6a0/2/0
Edited to match OP's question after reading intended results.
select name,count(*) as num
from my_table
group by name having num>3
where name like '%xyz'
Following Example Table Structure:
NR1 | NR2 | PRENAME | LASTNAME
If i query all 4 fields of this table and grouping it´s first 2 fields (NR1,NR2) in mysql,
i can do something like this:
SELECT NR1,NR2,PRENAME,LASTNAME FROM tbl GROUP BY NR1,NR1
But this won´t work in informix.
INFORMIX ERROR: the column (PRENAME) must be in the group by list
After reading some Topics at google, it is an "Informix feature" that all Selected Columns has to be in the Grouping List.
But if i will do that, the result is not that result, that i wish to have.
If i use
DISTINCT
instead GROUP BY the result is similar false, because i can not put the DISTINCTfunction only to column 1 and 2.
So: How can i make a "MYSQL GROUP BY" function ?
Your original syntax is suitable in one database -- MySQL. And, that database says that the results of the non-aggregated columns come from indeterminate rows. So, an equivalent query is just to use MIN() or MAX():
SELECT NR1, NR2, MIN(PRENAME), MIN(LASTNAME)
FROM tbl
GROUP BY NR1, NR1;
My guess is that you want an arbitrary value from just one row. I'd be inclined to concatenate them:
SELECT NR1, NR2, MIN(PRENAME || ' ' || LASTNAME)
FROM tbl
GROUP BY NR1, NR1;
I am having a problem I can't seem to solve. I have a data table that looks like this:
Example:
http://i.stack.imgur.com/tbKEk.png
I need to select the ID_JOB value which is duplicated the most. In this particular example it would be ID_JOB = 1.
Adapt this to your specific SQL implementation. Substitute [job_table] with the table you are querying.
SELECT TOP 1 ID_JOB
FROM job_table
GROUP BY ID_JOB
ORDER BY COUNT(*) DESC
You may need to add more ORDER BY logic in case a count "ties".
In sql table I select duplicate IDs which count is > 1 .Then I need to update only first row of selecting duplicate id rows.How to update just first row value.
Thanks in advance.
Assuming you have something like this:
SELECT ID
FROM ProductView
GROUP BY ID
HAVING COUNT(*) > 1
You will need to create a loop around the query and then select a result using TOP 1 and matching the ID. You can create the loop using a cursor.