简体   繁体   English

Oracle SQL-根据列中的值为结果集中的每一行赋予唯一标识符

[英]Oracle SQL - Give each row in a result set a unique identifier depending on a value in a column

I have a result set, being returned from a view, that returns a list of items and the country they originated from, an example would be: 我有一个从视图返回的结果集,该结果集返回一个项目列表以及它们来自的国家/地区,示例如下:

ID  |  Description  |  Country_Name
------------------------------------
 1  |  Item 1       | United Kingdom
 2  |  Item 2       | France
 3  |  Item 3       | United Kingdom
 4  |  Item 4       | France
 5  |  Item 5       | France
 6  |  Item 6       | Germany

I wanted to query this data, returning all columns (There are more columns than ID , Description and Country_Name , I've omitted them for brevity's sake) with an extra one added on giving a unique value depending on the value that is inside the field Country_name 我想查询此数据,返回所有列(除了IDDescriptionCountry_Name ,还有更多列,为简便起见,我省略了它们),并根据字段内的值提供了唯一值Country_name

ID  |  Description  |  Country_Name  |  Country_Relation
---------------------------------------------------------
 1  |  Item 1       | United Kingdom |       1
 2  |  Item 2       | France         |       2
 3  |  Item 3       | United Kingdom |       1
 4  |  Item 4       | France         |       2
 5  |  Item 5       | France         |       2
 6  |  Item 6       | Germany        |       3

The reason behind this, is we're using a Jasper report and need to show these items with an asterisk next to it (Or in this case a number) explaining some details about the country. 其背后的原因是,我们正在使用Jasper报告,并且需要在这些项目旁边显示一个星号(或本例中的数字),以说明有关该国家/地区的一些详细信息。 So the report would look like this: 因此,报告将如下所示:

Desc.         Country
Item 1        United Kingdom(1)
Item 2        France(2)    
Item 3        United Kingdom(1)
Item 4        France(2)    
Item 5        France(2)    
Item 6        Germany(3)    

And then further down the report would be a field stating: 然后在报告的更下方是一个字段,指出:

1: Here are some details about the UK
2: Here are some details about France
3: Here are some details about Germany

I'm having difficulty trying to generate a unique number to go along side each country, starting at one each time the report is ran, incrementing it when a new country is found and keeping track of where to assign it. 我很难尝试生成一个唯一的编号以沿每个国家/地区排列,每次运行报告时都从一个编号开始,在找到新的国家/地区时递增该编号并跟踪分配该位置的位置。 I would hazard a guess at using temporary tables to do such a thing, but I feel that's overkill. 我可能会猜测使用临时表来做这样的事情,但是我觉得这太过分了。

Question

  1. Is this kind of thing possible in Oracle SQL or am I attempting to do something that is rather large and cumbersome? 在Oracle SQL中这种事情是可能的吗?还是我正在尝试做一些相当大且繁琐的事情?
  2. Are there better ways of doing this inside of a Jasper report? 在Jasper报告中是否有更好的方法可以做到这一点?

At the moment, I'm looking at just having the subtext underneath each individual item and repeating the same information several times, just to avoid this situation, rather than having them aggregated and having the subtext once. 目前,我只是在每个单独的项目下面放置一个潜台词,并重复几次相同的信息,只是为了避免这种情况,而不是将它们聚合在一起并只进行一次潜台词。 It's not clean, but it saves this rather odd hassle. 它不干净,但可以省去这个相当奇怪的麻烦。

You are looking for dense_rank() : 您正在寻找dense_rank()

select t.*, dense_rank() over (order by country_name) as country_relation
from t;

I don't know if this can be done inside Jasper reports. 我不知道是否可以在Jasper报告中完成此操作。 However, it is easy enough to set up a view to handle this in Oracle. 但是,在Oracle中设置一个视图来处理这个问题很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM