简体   繁体   English

Oracle:如何按行中的第一个值分组?

[英]Oracle: how to group by 1st value in row?

Table has 3 columns in a table: ITEM, SUB_ITEM, DATE_CREATED. 表格在表格中有3列:ITEM,SUB_ITEM,DATE_CREATED。

ITEM - item id (string)
SUB_ITEM - item id (int)
DATE_CREATE - date the SUB_ITEM was created (date)

Scenario: 场景:

  1. There are 3 Different item's (AAA1, AAB2, ABB3) 有3种不同的商品(AAA1,AAB2,ABB3)
  2. All 3 of these item's have multiple sub-items. 这些项目的全部3个都有多个子项目。
  3. Each item has a sub-item that is the same for each of them (eg. All 3 of the item's have a SUB_ITEM = 101010) 每个项目都有一个与每个项目相同的子项目(例如,所有3个项目的SUB_ITEM = 101010)

I am trying to do something like: 我正在尝试做类似的事情:

select * 
from table 
group by ITEM, SUB_ITEM, DATE_CREATED

How do you make it display only 1 row? 如何使它仅显示1行? I don't care if it chooses AAA1 or AAB2 or ABB3, I just want it to pick 1 and remove the rest so it will show 1 row per SUB_ITEM, but still displays at least one of the parent items. 我不在乎它是否选择AAA1或AAB2或ABB3,我只希望它选择1并删除其余部分,因此每个SUB_ITEM将显示1行,但仍显示至少一个父项。

Edit: 编辑:

Thank you to mathguy for answering the above question. 感谢Mathguy回答上述问题。

Question 2: Is it possible to group by the 1st 2 letters of the item in addition to the sub_item? 问题2:除了sub_item以外,是否可以按项目的前2个字母分组? So instead of returning 1 row, return 2 rows: AAA1 and AAB2 will cascade in to 1 row, and ABB3 will be the 2nd row because 'AA' and 'AB' are different. 因此,返回2行而不是返回1行:AAA1和AAB2将级联为1行,而ABB3将成为第二行,因为'AA'和'AB'不同。

Edit 2: See Main Answer comments for answer to question 2 编辑2:请参阅主要答案注释以获取问题2的答案

One way is to group by sub_item , and take the max or min over another column (let's say max over date_created ) and whatever is in the remaining column IN THE SAME ROW. 一种方法是按sub_item ,并在另一列上获取maxmin (例如,在date_created max ),并在同一行的其余列中获取任何值。

select min(item) keep (dense_rank last order by date_created) as item, 
       sub_item, max(date_created) as date_created
from   table_name
group by sub_item
;

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

相关问题 如何从组中获取第一个非零行? - How to get 1st non-zero row from a group? Oracle sql 添加自定义列名作为结果集的第一行 - Oracle sql adding custom column name as 1st row of the resultSet 如何在ssrs报表中从sql设置第一行值? - How do I set 1st row value from sql in ssrs report? 如何在另一个表中选择具有相同值的SQL表中的第一行和第二行实例? - How to select 1st and 2nd row instance in SQL table having the same value in another table? 如何更新两次单行。 第二次更新应采用第一次更新的更新值 - How to update the single row twice. Second update should take the updated value from 1st update 如何在第一行的相同 ID 中显示相同的行 ID? - How to display same id of row in same id of 1st row? 在 Oracle 中,如何将 1 等数字转换为“1st”等字符串? - In Oracle, how do I convert a number such as 1 to a string such as “1st”? Oracle按行分组值 - Oracle group by row value 如何获取 PostgreSQL 中每个组的第一和第二条记录 - How to get the 1st and 2nd records for each group in PostgreSQL 如何在teradata的日表中的每一行中获取当前月份的第一天值和上个月的最后一天值? - How to get current month's 1st day value and last month's last day value in every row in a daily table in teradata?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM