How to assign same unique row number if records are same in sql query?

Viewed 150

I am trying to generate row number for the records but for the same record I am getting 2 unique row number. Is it possible if records are same then it generate the same unique number?

Example:

GENERIC_ID|Generic Name|HTA_INSERT_DT
2|Eculizumab|2021-02-10 05:50:28
7|Eculizumab|2021-02-10 05:50:28
9|Eculizumab|2021-02-10 05:50:28

in final output I want-

GENERIC_ID|Generic Name|HTA_INSERT_DT
2|Eculizumab|2021-02-10 05:50:28
2|Eculizumab|2021-02-10 05:50:28
2|Eculizumab|2021-02-10 05:50:28

I am running below sql query-

SELECT ROW_NUMBER() OVER(Order by (select NULL)) as GENERIC_ID
     , a.`Generic Name`
     , date_format(current_timestamp(),'yyyy-MM-dd hh:mm:ss') as HTA_INSERT_DT 
  from 
     ( 
Select distinct `Generic Name` from vw_non_onco_pharma 
 Union 
Select distinct `Generic Name` from vw_plasma_protein 
 Union 
Select distinct `Generic Name` from vw_non_onco_cell_gene 
 Union 
Select distinct `Generic Name` from vw_onco_cell_gene 
 Union 
Select distinct `Generic Name` from vw_onco_pharma 
 Union 
Select distinct `Generic Name` from vw_non_onco_no_id 
 Union 
Select distinct `Generic Name` from vw_onco_no_id
) a
3 Answers

ROW_NUMBER() will always apply a sequence of numbers, even if the data is the same on row level. You will have to create an unique list with row numbers, then join the full list based on Generic Name and HTA_INSERT_DT colums. They query will apply the same sequence number for matching generic name and insert date time.

You can DENSE_RANK() the records ordered by your key fields to get matching ids in sets. If you want the duplicates to take away from the sequential counter then use RANK() instead.

 DECLARE @X TABLE(ID1 INT, ID2 INT)
INSERT @X VALUES(1,1),(2,1),(2,2),(1,2),(1,2),(1,3),(1,1)

SELECT 
    R = RANK() OVER(PARTITION BY 1 ORDER BY ID1,ID2),
    DR = DENSE_RANK() OVER(PARTITION BY 1 ORDER BY ID1,ID2),
    ID1,
    ID2
FROM 
    @X

R   DR ID1  ID2
1   1   1   1
1   1   1   1
3   2   1   2
3   2   1   2
5   3   1   3
6   4   2   1
7   5   2   2

However, after re-reading your question. I now think you are looking for something similar to adding a derived key to id your unioned records like below.

SELECT 
DENSE_RANK() OVER(PARTITION BY 1 ORDER BY a.`Generic Name`,date_format(current_timestamp(),'yyyy-MM-dd hh:mm:ss')) as GENERIC_ID
     , a.`Generic Name`
     , date_format(current_timestamp(),'yyyy-MM-dd hh:mm:ss') as HTA_INSERT_DT 
  from 
     ( 
Select distinct `Generic Name` from vw_non_onco_pharma 
 Union 
Select distinct `Generic Name` from vw_plasma_protein 
 Union 
Select distinct `Generic Name` from vw_non_onco_cell_gene 
 Union 
Select distinct `Generic Name` from vw_onco_cell_gene 
 Union 
Select distinct `Generic Name` from vw_onco_pharma 
 Union 
Select distinct `Generic Name` from vw_non_onco_no_id 
 Union 
Select distinct `Generic Name` from vw_onco_no_id
) a

I'm not clear if your tables contain multiple distinct generic_names.

You could in a cte get the min row number for each generic_name then union and join in the main query

drop table if exists t1,t,t2;
create table t(Generic_Name varchar(20));
create table t1(Generic_Name varchar(20));
create table t2(Generic_Name varchar(20));
insert into t values('aaa'),('Eculizumab');
insert into t1 values('Eculizumab');
insert into t2 values('Eculizumab');
with cte as
(
select generic_name,min(rn) generic_id,current_timestamp ht
from
(
select row_number() over (order by generic_name) rn,
         generic_name
from
(
select generic_name from t
union 
select generic_name from t1
union 
select generic_name from t2
) s
) u
group by generic_name
) 
select generic_id,t.generic_name,ht 
from cte
join t on t.generic_name = cte.generic_name
union all
select generic_id,t1.generic_name,ht 
from cte
join t1 on t1.generic_name = cte.generic_name
union all
select generic_id,t2.generic_name,ht 
from cte
join t2 on t2.generic_name = cte.generic_name;

+------------+--------------+---------------------+
| generic_id | generic_name | ht                  |
+------------+--------------+---------------------+
|          1 | aaa          | 2021-02-10 14:32:50 |
|          2 | Eculizumab   | 2021-02-10 14:32:50 |
|          2 | Eculizumab   | 2021-02-10 14:32:50 |
|          2 | Eculizumab   | 2021-02-10 14:32:50 |
+------------+--------------+---------------------+
4 rows in set (0.004 sec)
Related