[英]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: 场景:
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
,并在另一列上获取max
或min
(例如,在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.