How to Find a Higher Duplicate ID in SQL Server? - sql

I need a help with a specific scenario I ran into.
I have a table structure like this.
In this table(lets call it a user table) ID is the primary key. I want to find the higher value of the ID if the data is duplicated in the username column.
For an example, John is listed 4 times and the highest ID of that data is 10. So i need that in my result.
I can find the duplicate but max id I am having some issue.

Using MAX Function:
Lists both duplicated rows and non duplicated also
Select Max(ID), username from table group by username
If u need only duplicated only then
Select Max(ID), username from table group by username
Having min(ID)<>Max(id)

Related

SQL query to achieve those rows which matches for two(or more) column value pair list

Suppose, there is a table users_customers which has three column user_id,customer_id and id. This table gives information about which user is assigned to which customer and vice-versa.
Now, I have a list of pair of user_id and customer_id. I know SQL query for to get row for a single pair of user_id and customer_id.
That is,
select * from users_customers where user_id in(uId) and customer_id in (cId).
But, how to get for all pairs in one go without executing query again and again for different pair. I am using postgresql 9.6. And, I will use alternate of this query in Spring Data JPA.
I would appreciate any help.
I'm not sure what your actual problem is, but you can use in with tuples:
where (user_id, customer_id) in ( (u1, c1), (u2, c2), . . . )
You can also pass in multiple values as an array.

How to remove duplicate rows and keep one in an Access database?

I need to remove duplicate rows in my Access database, does anyone have generic query to do this? As I have this problem with multiple tables
There are two things you need to do,
Determine what the criteria are for a unique record - what is the list of columns where two, or more, records would be considered duplicates, e.g. JobbID and HisGuid
Decide what you want to do with the duplicate records - do you want to hard delete them, or set the IsDeleted flag that you have on the table
Once you've determined the criteria that you want to use for uniqueness you then need to pick 1 record from each group of duplicates to retain. A query along the lines of:
SELECT MAX(ID)
FROM MyTable
GROUP
BY JobbID, HisGuid
Will give you (and I've assumed that the ID column is an auto-increment/identity column that is unique across all records in the table) the highest value for each group of records where JobbID and HisGuid are both the same. You could use MIN(ID) if you want, it's up to you - you just need to pick ONE record from each group to keep.
Assuming that you want to set the IsDeleted flag on the records you don't want to keep, you can then incorporate this into an update query:
UPDATE MyTable
SET IsDeleted = 1
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP
BY JobbID, HisGuid
)
This takes the result of the query that retrieves the highest IDs and uses it to say set IsDeleted to 1 for all the records where the ID isn't the highest ID for each group of records where JobbID and HisGuid are the same.
The only part I can't help you with is running these queries in Access as I don't have it installed on the PC I'm using right now and my memory is a bit rusty regarding how/where to run arbitrary queries.

Can column values be accessed like an array in SQL server

Can column values be accessed like an array in SQL server. for instance Select x[1] from tableA gives me the value for that field. I'm attempting to do this #OldValue = (SELECT #fieldname FROM #del). But this just returns the actual field name not the field value
No. Data in tables is not ordered, so there is no first, second, etc. record. You access a record by some criteria, e.g. a user by a user ID or login name, an employee by an employee number, etc. Or you use technical IDs to access records.
If you want the fifth record according to some order you can usually use some limit clause. E.g.:
SELECT *
FROM user
ORDER BY userid
OFFSET 4 ROWS FETCH NEXT 1 ROW ONLY;
fetches the 5th record ordered by user ID.
As to columns: They are accessed by their names. E.g.:
SELECT name
FROM user
ORDER BY userid
OFFSET 4 ROWS FETCH NEXT 1 ROW ONLY;
As you see it would make no sense to have some Excel-like access, because you need an order by clause and accessing columns via name rather than by some letter or number is even more readable and less prone to errors.

Assign an ID Value for Every Set of Duplicates

How can i generate an ID value for every set of duplicate records as seen in the second table with ID column? In other words, how can I let the first table to look like the second table using SQL query?
Assume that first name and last name in the first table can appear in duplicates.
Each first name and last name can have one or many purchase yr and cost.
The given image is just a sample. Total records in table 1 can reach thousands.
I'm using Oracle SQL.
Note: I'm working with one table only that is the first one. The second table is what I want.
You can use the DENSE_RANK analytic function to assign ID's as below:
EDIT:
Simplified query to generate ID's.
SELECT
DENSE_RANK() OVER (ORDER BY First_Name, Last_Name) ID,
t.*
FROM Table1 t;
Reference:
DENSE_RANK on Oracle Database SQL Reference

FILEMAKER SQL - Fetch column2 from records with distinct column1

How do I find a distinct set of records based upon one field while returning a different field from those records?
ExecuteSQL ( "SELECT DISTINCT account FROM Albums2"; ""; "" )
How do I get another field returned in this type of query?
My goal is fetch 1 record from 1 instance of each account in the list. I am going to populate a global with the id's from the records so that the accounts show in a portal. Then I want to select an account so that all records from the account will show in another portal.
Dan's answer below works perfectly:
SELECT account, MAX(id) FROM Albums2 GROUP BY account
I apologize for syntax error in my original answer. I no longer have Filemaker, but this code below will work with SQL. Does Filemaker 12 allow the SQL "GROUP BY" syntax? If so, it would solve the problem like this:
SELECT account, MAX(id) FROM Albums2 GROUP BY account
Explanation (assuming it works in Filemaker 12):
Rather than using DISTINCT to combine rows, you can use the GROUP BY syntax instead. To do that, you will also need to use an aggregate function like MAX() for the id column to indicate which of the non-duplicated id values to retain in the combined result.