Update grouped records in Oracle with an incremental value - sql

So a dilemna, I have an Oracle table called T_GROUP. The records in the table have a unique id (ID) and they are part of a Study, identified by STUDY_ID, so multiple groups can be in the same Study.
CREATE TABLE T_GROUP
(
"ID" NUMBER(10,0),
"GROUP_NAME" VARCHAR2(255 CHAR),
"STUDY_ID" NUMBER(10,0)
)
The existing table has hundreds of records and I now add a new column called GROUP_INDEX:
ALTER TABLE T_GROUP ADD (
GROUP_INDEX NUMBER(10,0) DEFAULT(0)
);
After adding the column I need to run a script to update the GROUP_INDEX field as such: it should start at 1 and increment by 1 for each group within a study, starting with the lowest ID.
So now I have data as follows:
ID GROUP_NAME STUDY_ID GROUP_INDEX
-------------------------------------------
1 Group 1 3 0
2 Group 2 3 0
3 My Group 5 0
4 Big Group 5 0
5 Group X 5 0
6 Group Z 6 0
7 Best Group 6 0
After the update the group_index field should be as follows:
ID GROUP_NAME STUDY_ID GROUP_INDEX
-------------------------------------------
1 Group 1 3 1
2 Group 2 3 2
3 My Group 5 1
4 Big Group 5 2
5 Group X 5 3
6 Group Z 6 1
7 Best Group 6 2
The update will be run from sqlplus via a batch file. I've played around with group by and sub queries but I'm not having much luck, and having never used sqlplus I'm not sure if I can use variables, cursors etc. All tips greatly appreciated!

You should be able to use the analytic function row_number for this
UPDATE t_group t1
SET group_index = (SELECT rnk
FROM (SELECT id,
row_number() over (partition by study_id
order by id) rnk
FROM t_group) t2
WHERE t2.id = t1.id)

Here is a version using the MERGE statement. Might be faster than the sub-select (but doesn't have to be).
merge into t_group
using
(
select id,
row_number() over (partition by study_id order by id) rnk
from t_group
) t on t.id = t_group.id
when matched then update
set group_index = t.rnk;
This assumes that id is the primary key (or at least unique)
I can't test it right now, so there might be some syntax error in it.

Related

Update new foreign key column of existing table with ids from another table in SQL Server

I have an existing table to which I have added a new column which is supposed to hold the Id of a record in another (new) table.
Simplified structure is sort of like this:
Customer table
[CustomerId] [GroupId] [LicenceId] <-- new column
Licence table <-- new table
[LicenceId] [GroupId]
The Licence table has a certain number of licences per group than can be assigned to customers in that same group. There are multiple groups, and each group has a variable number of customers and licences.
So say there are 100 licences available for group 1 and there are 50 customers in group 1, so each can get a license. There are never more customers than there are licences.
Sample
Customer
[CustomerId] [GroupId] [LicenceId]
1 1 NULL
2 1 NULL
3 1 NULL
4 1 NULL
5 2 NULL
6 2 NULL
7 2 NULL
8 3 NULL
9 3 NULL
Licence
[LicenceId] [GroupId]
1 1
2 1
3 1
4 1
5 1
6 1
7 2
8 2
9 2
10 2
11 2
12 3
13 3
14 3
15 3
16 3
17 3
Desired outcome
Customer
[CustomerId] [GroupId] [LicenceId]
1 1 1
2 1 2
3 1 3
4 1 4
5 2 7
6 2 8
7 2 9
8 3 12
9 3 13
So now I have to do this one time update to give every customer a licence and I have no idea how to go about it.
I'm not allowed to use a cursor. I can't seem to do a MERGE UPDATE, because joining the Customer to the Licence table by GroupId will result in multiple hits.
How do I assign each customer the next available LicenceId within their group in one query?
Is this even possible?
You can use window functions:
with c as (
select c.*, row_number() over (partition by groupid order by newid()) as seqnum
from customers c
),
l as (
select l.*, row_number() over (partition by groupid order by newid()) as seqnum
from licenses c
)
update c
set c.licenceid = l.licenseid
from c join
l
on c.seqnum = l.seqnum and c.groupid = l.groupid;
This assigns the licenses randomly. That is really just for fun. The most efficient method is to use:
row_number() over (partition by groupid order by (select null)) as seqnum
SQL Server often avoids an additional sort operation in this case.
But you might want to order them by something else -- for instance by the ordering of the customer ids, or by some date column, or something else.
Gordon has put it very well in his answer.
Let me break it down into simpler steps for you.
Step 1. Use the ROW_NUMBER() function to assign a SeqNum to the Customers. Use PARTITION BY GroupId so that the number starts from 1 in every group. I would ORDER BY CustomerId
Step 2. Use the ROW_NUMBER() function to assign a SeqNum to the Licences. Use PARTITION BY GroupId so that the number starts from 1 in every group. ORDER BY LicenseId because your ask is to "assign each customer the next available LicenceId within their group".
Now use these 2 queries to update LicenseId in Customer table.

How to eliminate repeated rows from table with composite primary key ORACLE

I need to find the repeated rows from a table where the first three columns will make up the primary key. Then after finding out which one's are repeated, those repeated rows need to be removed from the query results as this example shows:
Given this table. The first 3 columns act as the primary key.
--------------1 2 3 4 5 6-------------------------------------------------------------------------------------------------------------------------------1 2 3 9 8 9-------------------------------------------------------------------------------------------------------------------------------1 4 3 9 8 9-------------------------------------------------------------------------------------------------------------------------------3 4 2 2 2 1-------------------------------------------------------------------------------------------------------------------------------2 3 4 1 1 3-------------------------------------------------------------------------------------------------------------------------------2 3 4 9 9 0--------
Since 1 2 3 is the composite primary key. The first 2 rows should be considered repeated and therefore eliminated from the results. Just as the two 2 3 4 rows.
The only rows in the result set should be:
1 4 3 9 8 9 and 3 4 2 2 2 1
Could you please help?
Thanks a lot in advance..
Your question doesn't fully make sense. A composite primary key would prevent duplicates in the table. So, these columns are not declared as a comosite primary key if the data contains duplicates.
If you just one one row for each group, you can use row_number() for this, something like this (the column names are obviously invalid):
select t.*
from (select t.*, row_number() over (partition by 1, 2, 3 order by 1) as seqnum
from table t
) t
where seqnum = 1;
If you want to delete extra rows, you can try:
delete from t
where rowid not in (select min(rowid) from table t group by 1, 2, 3);
EDIT:
If you want to remove cases where the rows are repeated, then you want count() instead of row_number():
select t.*
from (select t.*, count() over (partition by 1, 2, 3) as cnt
from table t
) t
where cnt > 1;

SQL - Order by amount of occurrences

It's my first question here so I hope I can explain it well enough,
I want to order my data by amount of occurrences in the table.
My table is like this:
id Daynr
1 2
1 4
2 4
2 5
2 6
3 1
4 2
4 5
And I want it to sort it like this:
id Daynr
3 1
1 2
1 4
4 2
4 5
2 4
2 5
2 6
Player #3 has one day in the table, and Player #1 has 2.
My table is named "dayid"
Both id and Daynr are foreign keys, together making it a primary key
I hope this explains my problem enough, Please ask for more information it's my first time here.
Thanks in advance
You can do this by counting the number of times that things occur for each id. Most databases support window functions, so you can do this as:
select id, daynr
from (select t.*, count(*) over (partition by id) as cnt
from table t
) t
order by cnt, id;
You can also express this as a join:
select t.id, t.daynr
from table as t inner join
(select id, count(*) as cnt
from table
group by id
) as tg
on t.id = tg.id
order by tg.cnt, id;
Note that both of these include the id in the order by. That way, if two ids have the same count, all rows for the id will appear together.

Is there a way to update groups of rows with separate incrementing values in one query

Lets say you have the following table:
Id Index
1 3
1 1
2 1
3 3
1 5
what I would like to have is the following:
Id Index
1 0
1 1
2 0
3 0
1 2
As you might notice, the goal is for every row where Id is the same, to incrementally update the Index column, starting from zero.
Now, I know this is fairly simple with using cursors, but out of curiosity is there a way to do this with single UPDATE query, somehow combining with temp tables, common table expressions or something similar?
Yes, assuming that the you don't really care about the order of the values for the new index values. SQL Server offers updatable CTEs and window functions that do exactly what you want:
with toupdate as (
select t.*, row_number() over (partition by id order by (select NULL)) as newindex
from table t
)
update toupdate
set index = newindex;
If you want them in a specific order, then you need another column to specify the ordering. The existing index column doesn't work.
With Row_number() -1 and CTE you can write as:
CREATE TABLE #temp1(
Id int,
[Index] int)
INSERT INTO #temp1 VALUES (1,3),(1,1),(2,1),(3,3),(1,5);
--select * from #temp1;
With CTE as
(
select t.*, row_number() over (partition by id order by (select null))-1 as newindex
from #temp1 t
)
Update CTE
set [Index] = newindex;
select * from #temp1;
Demo
I'm not sure why you would want to do this really, but I had fun figuring it out!
This solution relies on your table having a primary key for the self join... but you could always create an auto inc index if none exists and this is a one off job... This will also have the added benefit of getting you to think about the precise ordering of this you want... as currently there is no way of saying which order [ID] will get [Index] in.
UPDATE dbo.Example
SET [Index] = b.newIndex
FROM dbo.Example a
INNER JOIN (
select
z.ID,
z.[Index],
(row_number() over (partition by ID order by (select NULL))) as newIndex
from Example z
) b ON a.ID = b.ID AND a.[Index]=b.[Index] --Is this a unique self join for your table?.. no PK provided. You might need to make an index first.
Probably, this is what you want
SELECT *,RANK() OVER(PARTITION BY Id ORDER BY [Index])-1 AS NewIndex FROM
(
SELECT 1 AS Id,3 [Index]
UNION
SELECT 1,1
UNION
SELECT 2,1
UNION
SELECT 3,3
UNION
SELECT 1,5
) AS T
& the result will come as
Now if you want to update the table then execute this script
UPDATE tblname SET Index=RANK() OVER(PARTITION BY t.Id ORDER BY t.[Index])-1
FROM tblname AS t
In case I am missing something or any further assistance is required please let me know.
CREATE TABLE #temp1(
Id int,
Value int)
INSERT INTO #temp1 VALUES (1,2),(1,3),(2,3),(4,5)
SELECT
Id
,Value
,ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) Id
FROM #temp1
Start with this :)
Gave me results like
Id Value Count
1 2 1
1 3 2
1 2 3
1 3 4
1 2 5
1 3 6
1 2 7
1 3 8
2 3 1
2 4 2
2 5 3
2 3 4
2 4 5
2 5 6
2 4 7
2 5 8
2 3 9
2 3 10
3 4 1
4 5 1
4 5 2
4 5 3
4 5 4

Active Record select 15 records order by date with different field value using

Here I have some articles:
id text group_id source_id
1 t1 1 1
2 t2 1 1
3 t3 2 2
4 t4 3 4
So I want to have records in result ordered by created_at column (it exists, but I didn't show it in table) and having distinct group id, such as that:
id text group_id source_id
1 t1 1 1
3 t3 2 2
4 t4 3 4
Also, I should be able to filter result with source_id.
I'm stuck with this question for two days and don't even know how to start solve problem.
Assuming you want the minimum values of the non-duplicated columns, try:
select min(id) as id,
min(text) as text,
group_id,
source_id,
min(created_at) as created_at
from articles
where source_id = #your_parameter_value
group by group_id,
source_id
order by 5
Select * from
(Select * from articles
Order by group_id, id) x
Group by group_id