I have table A like below
Table A:
ID Value
1 A
2 B
Note ID is auto Identity
Now I want to duplicate table A and keep new ID of new record inserted
My expectation after insert I have 2 table like below
Table A
ID Value
1 A
2 B
3 A
4 B
AND
Table IDList
Old_ID New_ID
1 3
2 4
NOTE Table IDList is temperary table to keep old and new ID
Perhaps simply:
SELECT Old_ID=MIN(ID), New_ID=MAX(ID)
FROM dbo.TableA
GROUP BY Value
demo
Related
I have a table
id
repeat customer id
store
date
1
A
07-19-22
2
A
07-19-22
3
A
07-19-22
id
repeat customer id
store
date
1
B
07-19-22
2
B
07-19-22
3
1
B
07-19-22
4
B
07-19-22
and more tables from other store
The problem here is
all stores start with id 1
repeat customer have new id in id column and their original id is retained in repeat customer id column
I have to concatenated all the tables and also keep track of repeating customer for analytics. I have joined all tables using UNION ALL and also created a dummy id column using SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS NEW_ID, * FROM CTE, but I have no clue how to capture and assign value to repeat customer id such that I get the table as below
NEW_ID
id
new_repeat_customer_id
repeat customer id
store
date
1
1
A
07-19-22
2
2
A
07-19-22
3
3
A
07-19-22
4
1
B
07-19-22
5
2
B
07-19-22
6
3
4
1
B
07-19-22
7
4
B
07-19-22
The best way to incorporate it, would be to use Alphanumeric String as NEW_ID, and concat STORE and ID to create NEW_ID. For example A_000000001. In that way you can add similar STORE to REPEAT_CUSTOMER_ID as well.
So in this case, instead of NEW_ID=6, you would have NEW_ID=B_000000003 and REPEAT_CUSTOMER_ID would become B_000000001.
But in case that is not possible, you can use query like below to get the output
DB Fiddle Query
with CTE as
(
select * from STORE1
UNION ALL
select * from STORE2
)
,CTE2 as
(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS NEW_ID,t.* from CTE t)
,REPEAT_ID as
(select NEW_ID,ID,REPEAT_CUSTOMER_ID,STORE from CTE2 where REPEAT_CUSTOMER_ID is not null)
,REPEACT_CUSTOMER_ID as
(select c.NEW_ID as NEW_REPEAT_CUSTOMER_ID,r.NEW_ID
from REPEAT_ID r
left join CTE2 c
on c.ID=r.REPEAT_CUSTOMER_ID and c.STORE=r.STORE
)
select c.* , n.NEW_REPEAT_CUSTOMER_ID
from CTE2 c
left join REPEACT_CUSTOMER_ID n
on c.NEW_ID=n.NEW_ID
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=cbe63994b10f9e3b0eff53b0c89d463a
SO basically you have to separate rows where REPEATE customer is present and join it with main table query.
I have two tables, A and B
Table A:
Id int
Id_B int (FK with B)
Table B:
Id
Current state: An Id in table A can have multiple Id_B relationships, and two Id from table A can have the same Id_B
New state: I wish to make it so that two Id from table A cannot have the same Id_b. If I find such a relationship then create a new Id in table B and link it to the Id in table A to enforce the aforementioned constraint
I am lost on where to begin with this in SQL.
Before
Table A
Id
Id_B
1
1
1
4
2
1
3
2
4
3
Table B
Id
1
2
3
4
After (since there were two occurrences of 1 in the Id_B column, I created a new row in table B (with id 5), and assigned the Id of this to the Id 2 in table A to enforce uniqueness)
Table A
Id
Id_B
1
1
1
4
2
5
3
2
4
3
Table B
Id
1
2
3
4
5
Find all duplicate A.B_ID rows, add new B rows for the duplicates and update the corresponding A.B_ID.
Start with a query like:
with q as
(
select *, row_number() over (partition by B_ID, order by ID) rn
from A
)
select *
from q
where rn > 1
Add a unique constraint on A.B_ID
Table Name: Student
ID Name
-------------------------
1 ABC
2 PQR
3 XYZ
I would like to update it to:
ID Name
-------------------------
2 ABC
1 PQR
3 XYZ
ID column is primary key column.
I have tried the following:
Update student set id =case when id =1 then 2
when id= 2 then 1
else id
end;
Select Id, Id As TmpId, Name, Into #tmp from Student
Update #tmp
Set TmpId = case when id =1 then 2 when id= 2 then 1 else id end
Truncate Table Student
Insert Into Student
Select TmpId, Name From #tmp
I have table like this-
id object_id product_id
1 1 1
2 1 1
4 2 2
6 3 2
7 3 2
8 1 2
9 1 1
I want to delete all rows except these-
1 1 1
4 2 2
6 3 2
9 1 2
Basically there are duplicates and I want to remove them but keep one copy intact.
what would be the most efficient way for this?
If this is a one-off then you can simply identify the records you want to keep like so:
SELECT MIN(id) AS id
FROM yourtable
GROUP BY object_id, product_id;
You want to check that this works before you do the next thing and actually throw records out. To actually delete those duplicate records you do:
DELETE FROM yourtable WHERE id NOT IN (
SELECT MIN(id) AS id
FROM yourtable
GROUP BY object_id, product_id
);
The MIN(id) obviously always returns the record with the lowest id for a set of (object_id, product_id). Change as desired.
I had a table (Some_Table) with two columns:
A B
-------------------
1 test
2 test
3 test1
4 test1
i would like to return DISTINCT values for column B and it's associated value in column A (first in distinct set), so something like this:
A B
-----------
1 test
3 test1
What is the sql?
select min(A),B
from table
group by B