Selecting Distinct field and row num just to display an id number gives duplicated data

别来无恙 提交于 2021-02-05 11:23:29

问题


I have a table application and it has 10 columns. category is one column and this column has duplicated values. To get distinct values I have a query

SELECT distinct(CATEGORY) as CategoryName FROM APPLICATION where applicationId=?.

I am getting result without any issue. Here now I wanted to add a another column as categoryId. There is no such field, I have to generate one. I tried with below query.

SELECT distinct(CATEGORY) as CategoryName , rownum as categoryId FROM APPLICATION where applicationId=?

Then it shows duplicate category with the rownum as id. I am ok with any number as id but category name should not duplicated. Can any one suggests how to do it in a single query.


回答1:


please use

SELECT CATEGORY as CategoryName, sum(rownum) FROM APPLICATION WHERE applicationId=? GROUP BY CATEGORY



回答2:


You need to use the subquery as follows:

select CategoryName , rownum as categoryId from
(SELECT distinct(CATEGORY) as CategoryName FROM APPLICATION where applicationId=?)

You can also use the analytical function as follows:

SELECT distinct(CATEGORY) as CategoryName, 
       rank() over (order by CATEGORY) as categoryId 
  FROM APPLICATION where applicationId=?

Example:

I have the following data:

SQL> SELECT WRITER_ID, TWEET FROM TWEET;

 WRITER_ID TWEET
---------- -----
         1 T1
         1 T2

SQL>

See the output of both of the above query:

SQL> SELECT WRITER_ID, ROWNUM AS GENERATED_NUMBER FROM
  2  (SELECT DISTINCT WRITER_ID AS WRITER_ID FROM TWEET);

 WRITER_ID GENERATED_NUMBER
---------- ----------------
         1                1

SQL> SELECT DISTINCT ( WRITER_ID ),
  2                  RANK() OVER(ORDER BY WRITER_ID) AS GENERATED_NUMBER
  3    FROM TWEET;

 WRITER_ID GENERATED_NUMBER
---------- ----------------
         1                1

Now, Let me change my data

SQL> UPDATE TWEET
  2     SET
  3  WRITER_ID = 2
  4   WHERE ID = 101;

1 row updated.

Table data is changed:

SQL> SELECT WRITER_ID, TWEET FROM TWEET;

 WRITER_ID TWEET
---------- -----
         1 T1
         2 T2

SQL>

Lets see what is the result of the above queries now:

SQL> SELECT WRITER_ID, ROWNUM AS GENERATED_NUMBER FROM
  2  (SELECT DISTINCT WRITER_ID AS WRITER_ID FROM TWEET);

 WRITER_ID GENERATED_NUMBER
---------- ----------------
         1                1
         2                2

SQL> SELECT DISTINCT ( WRITER_ID ),
  2                  RANK() OVER(ORDER BY WRITER_ID) AS GENERATED_NUMBER
  3    FROM TWEET;

 WRITER_ID GENERATED_NUMBER
---------- ----------------
         2                2
         1                1



回答3:


Try using this

SELECT DISTINCT(CATEGORY) AS CategoryName, 
       ROW_NUMBER() OVER (ORDER BY CATEGORY) AS categoryId
FROM APPLICATION 
WHERE applications =? 


来源:https://stackoverflow.com/questions/62769655/selecting-distinct-field-and-row-num-just-to-display-an-id-number-gives-duplicat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!