Update only duplicate values in a table column - sql

I have got a table that has a column with duplicate values. I would like to update duplicate values (not the first instance though) by incrementing by 1 so that I can get rid of duplicates in that column.
Can anyone help?

You need ranking on Tel over username. This will give you the ranking.
SELECT *, (select count(table1.username)
from table1 as tbl2
where table1.tel >= tbl2.tel and table1.username=tbl2.username)
as rank from table1 ORDER BY username,tel;
if your active column is always T then you can hard code it in the query as below:
SELECT *, (select count(table1.username) from
table1 as tbl2
where table1.tel >= tbl2.tel and table1.username=tbl2.username) & 'T'
as rank from table1 ORDER BY username,tel
if column active in Table1 can be different, then save the first query as query1 and use this join query:
SELECT Table1.active & query1.rank AS Expr1, Table1.tel, Table1.username
FROM Table1 INNER JOIN Query1 ON Table1.ID = Query1.ID;
add an ID column to your Table1 if you don't have one.

Related

SQL Server query showing most recent distinct data

I am trying to build a SQL query to recover only the most young record of a table (it has a Timestamp column already) where the item by which I want to filter appears several times, as shown in my table example:
.
Basically, I have a table1 with Id, Millis, fkName and Price, and a table2 with Id and Name.
In table1, items can appear several times with the same fkName.
What I need to achieve is building up a single query where I can list the last record for every fkName, so that I can get the most actual price for every item.
What I have tried so far is a query with
SELECT DISTINCT [table1].[Millis], [table2].[Name], [table1].[Price]
FROM [table1]
JOIN [table2] ON [table2].[Id] = [table1].[fkName]
ORDER BY [table2].[Name]
But I don't get the correct listing.
Any advice on this? Thanks in advance,
A simple and portable approach to this greatest-n-per-group problem is to filter with a subquery:
select t1.millis, t2.name, t1.price
from table1 t1
inner join table2 t2 on t2.id = t1.fkName
where t1.millis = (select max(t11.millis) from table1 t11 where t11.fkName = t1.fkName)
order by t1.millis desc
using Common Table Expression:
;with [LastPrice] as (
select [Millis], [Price], ROW_NUMBER() over (Partition by [fkName] order by [Millis] desc) rn
from [table1]
)
SELECT DISTINCT [LastPrice].[Millis],[table2].[Name],[LastPrice].[Price]
FROM [LastPrice]
JOIN [table2] ON [table2].[Id] = [LastPrice].[fkName]
WHERE [LastPrice].rn = 1
ORDER BY [table2].[Name]

SQL to sum a total of a column where 2 columns match in a different table

SO I have 2 tables and would like to SUM the total of a column in one table where 2 other columns match in another table.
In table1 I have acc_ref and bill_no.
acc_ref is different but bill_no could be 1-10 (so 2 or more acc_ref could have the same bill_no)
In table2 I have acc_ref, bill_no and tran_amnt.
Εach acc_ref has multiple rows and I want to SUM the tran_amnt but only if acc_ref and bill_no both match in table1.
I tried this but I get an error
'The columns in the SELECT clause must be contained in the GROUP BY
clause'
select a.acc_ref, a.bill_no
from table1 a
where exists (select acc_ref, bill_no, SUM (tran_amount)
from table2 b
where a.acc_ref = b.acc_ref
and a.bill_no = b.bill_no
group by acc_ref)
Apologies if this is very basic and obvious but I'm struggling!!
In you description it seems like table1 does not contain any useful information. Because both columns you give also exist in table2. So if nothing else from table1 is needed, you could just remove table1 from the query). Still with your problem you should do a simple join
SELECT a.acc_ref, a.bill_no, SUM(b.tran_amount)
FROM table1 a
JOIN table2 b ON b.acc_ref = a.acc_ref AND b.bill_no=a.bill_no
GROUP BY a.acc_ref, a.bill_no
I believe you should use case:
Sample:
SELECT
TABEL1.Id,
CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
THEN 'TRUE'
ELSE 'FALSE'
END AS NewFiled
FROM TABLE1

Join between two tables, number of row

I hope you're well.
I try to write a sql query with a join between two tables like below :
table1 (id_master, id)
1,1
1,2
1,3
1,4
1,5
And the second table
table2 (id_master, id)
1,1
1,2
1,3
1,4
As you can see, each table contain ID_master & id.
The table2 contains the acknownledgement (ack) of table1. Each row in the table1 must have an "ack" in the table2.
In my example, I have no result because (table1 (1,5) hasn't got an ack in table2 and I want result when table1.row (1,5) has got a ack in table2.
I have tried with join but i have result when we have the first "ack". I want have result when I have all "ack".
I hope to be clear.
thanks for your help.
kind regards
EDIT :
Thanks stripathi & jpw,
Example1:
table1 (id_master, id)
1,A
1,B
1,C
2,D
2,E
the second table
table2 (id_master, id)
1,A
1,B
2,D
2,E
My query's result must be :
2,D
2,E
Because we can find the rows(2,D) & (2,E) in the two tables, but it isn't the case for (1,*) (it miss the (1,C) in the table2).
I think both of these two queries should do what you want, and they seem to work using Oracle 11g R2 (see this SQL Fiddle). Note that the result might be wrong if the second table contains items that are not present in the first table.
select *
from table1
where id_master in (
select a.id_master
from table1 a
group by a.id_master
having count(distinct a.id) = (
select count(distinct b.id)
from table2 b
where a.id_master = b.id_master
group by b.id_master
)
);
select *
from table1 a
where not exists (
select id from Table1 where id_master = a.id_master
minus
select id from Table2
);
If you using oracle you can use ROWNUM to get row number of first ack. You can try this :
SELECT ID,ID_MASTER FROM(
SELECT ID,ID_MASTER,ROWNUM RR
FROM TABLE2
ORDER BY ID_MASTER,ID ASC) T2
WHERE RR >= (
SELECT R FROM(
SELECT ID_MASTER,ID, ROWNUM R
FROM TABLE1
ORDER BY ID_MASTER,ID ASC
) T1
WHERE T1.ID_MASTER||T1.ID NOT IN(SELECT ID_MASTER||ID FROM TABLE2)
)

How to compare tables and find duplicates and also find columns with different value

I have the following tables in Oracle 10g:
Table1
Name Status
a closed
b live
c live
Table2
Name Status
a final
b live
c live
There are no primary keys in both tables, and I am trying to write a query which will return identical rows without looping both tables and comparing rows/columns. If the status column is different then the row in the Table2 takes presedence.
So in the above example my query should return this:
Name Status
a final
b live
c live
Since you have mentioned that there are no Primary Key on both tables, I'm assuming that there maybe a possibility that a row may exist on Table1, Table2, or both. The query below uses Common Table Expression and Windowing function to get such result.
WITH unionTable
AS
(
SELECT Name, Status, 1 AS ordr FROM Table1
UNION
SELECT Name, Status, 2 AS ordr FROM Table2
),
ranks
AS
(
SELECT Name, Status,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY ordr DESC) rn
FROM unionTable
)
SELECT Name, Status
FROM ranks
WHERE rn = 1
SQLFiddle Demo
Something like this?
SELECT table1.Name, table2.Status
FROM table1
INNER JOIN table2 ON table1.Name = table2.Name
By always returning table2.Status you've covered both the case when they're the same and when they're different (essentially it doesn't matter what the value of table1.Status is).

Help with Joins

my first table has about 18K records
so when i
select * from table2 i get about 18k
i'm trying to do a join on it as follows, but i'm getting like 26K back.. what am i doing wrong? i though it's supposed to return all of the "right" aka table2 records plus show me whatever value matches from the first in a separate column...
Select t1.fID , t2.*
FROM table1 t1 right join table2 t2 on t1.fName = t2.f
here is an exmaple of my tables:
table 1:
fID, fName
table 2: id, f, address, etc
i need to get all records from table 2, with an fID column, whenever f=fName
table1 has many rows with a value of fname that matches the same in table2.
Example, say 5k rows table2 have no matching rows in table1, you have a average of 2 rows in table 1 for each of the remaining 13k table2 rows
Because you have also asked for a column for table1, this will happen. You'll note multiple t1.fId values for a given t2.fname. Or NULLs
If t1.fName and t2.f aren't unique identifiers for their tables, you will find that rows from table1 are being joined with multiple rows from table2.
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).See Right Join
So it looks like you do not have your matching criteria set correctly or you have no matches.
This is possible when some fName values are repeated in Table2 and/or Table 1.
Run these Queries and See:
SELECT fName, COUNT(1) FROM Table2 GROUP BY fName HAVING COUNT(1) > 1
SELECT fName, COUNT(1) FROM Table1 GROUP BY fName HAVING COUNT(1) > 1