Oracle查询分组

时间:2017-10-04 04:28:04

标签: sql oracle

我需要从满足某些条件的组中获取记录。请在下面找到我的输入记录集

ID  FIRSTNAME   SURNAME
123 E          Mcilwham
123 Emma       Mcilwham
123 Enda       Mcilwham
321 Lion       Mark
321 Lous       Mark
342 L          Isaac
342 L          Isaac
455 Lewis      hoting
455 L          hoting
325 D          Mark

在这个记录中我需要根据ID和Surname做一个组。我需要输出这样一种方式,对于相同的ID,至少应该有一个长度为1的记录,对于相同的ID,应该是长度大于1的其他记录,这些记录应以与长度为1的记录相同的字母开头。

在上面的示例ID中: - 123,455将涵盖上述情况。

此外,我不期待任何只有该ID记录的输出记录。(ID: - 325)。

对于ID 342的情况,这不应该出现在输出中,因为没有该ID长度大于1的记录。

希望每个人都清楚这一点。如果需要对此进行更多说明,请随时问我

请找到我在下面使用的查询,但没有按预期给出正确的结果。

SELECT 
  C.ID,
  C.FIRST_NAME,
  C.SURNAME
  FROM TABLE1 C
WHERE C.ID IN
  (SELECT DISTINCT A.ID
  FROM TABLE1 A ,
    TABLE1 B
  WHERE A.ID                 = B.ID
  AND LENGTH(TRIM(A.FIRST_NAME))    <> LENGTH(TRIM(B.FIRST_NAME))
--  AND LENGTH(TRIM(B.FIRST_NAME))    <> LENGTH(TRIM(A.FIRST_NAME))
AND LENGTH(TRIM(A.FIRST_NAME))   = 1 OR  LENGTH(TRIM(B.FIRST_NAME)) = 1
  AND SUBSTR(TRIM(A.FIRST_NAME),1,1) = SUBSTR(TRIM(B.FIRST_NAME),1,1)
  --AND SUBSTR(TRIM(B.FIRST_NAME),1,1) = SUBSTR(TRIM(A.FIRST_NAME),1,1)
  AND TRIM(A.SURNAME) = TRIM(B.SURNAME)
  )
ORDER BY 1

2 个答案:

答案 0 :(得分:0)

以下查询满足您为特定数据集指定的所有要求。根据您必须使用的实际数据,您可能需要更具体。

with sample_data as(
   select 123 as id, 'E'     as firstname, 'Mcilwham' as surname from dual union all
   select 123 as id, 'Emma'  as firstname, 'Mcilwham' as surname from dual union all
   select 123 as id, 'Enda'  as firstname, 'Mcilwham' as surname from dual union all
   select 321 as id, 'Lion'  as firstname, 'Mark'     as surname from dual union all
   select 321 as id, 'Lous'  as firstname, 'Mark'     as surname from dual union all
   select 342 as id, 'L'     as firstname, 'Isaac'    as surname from dual union all
   select 342 as id, 'L'     as firstname, 'Isaac'    as surname from dual union all
   select 455 as id, 'Lewis' as firstname, 'hoting'   as surname from dual union all
   select 455 as id, 'L'     as firstname, 'hoting'   as surname from dual union all
   select 325 as id, 'D'     as firstname, 'Mark'     as surname from dual
)
select id
      ,surname
      ,listagg(firstname, ',') within group (order by firstname) as firstnames
      ,count(*) as nof_firstnames 
  from sample_data a
 group 
    by id
      ,surname      
having count(*) > 1                 -- at least one record 
   and min(length(firstname)) = 1   -- at least one record has length = 1
   and max(length(firstname)) > 1   -- at least one record has a length greater than 1
  

在这条记录中,我需要根据ID和Surname做一个小组。我需要   以这样的方式输出,对于相同的ID,至少应该存在   一条长度为1的记录,对于相同的ID,应该有另一条记录   长度大于1且记录应该开始的记录   与长度为1的记录相同的字母。

当长度= 1且长度为&gt;的多条记录时会发生什么? 1?例如亚历山大并不是以&#39; R&#39;开头。 Bengt并非始于&#39; A&#39;但是有一个记录&#39; B&#39;。塞德里克以“C&C”开头。但它并不匹配A&#39; A&#39;记录。

ID  FIRSTNAME   SURNAME
1   R           Bahlsten
1   Ronnie      Bahlsten
1   Alexander   Bahlsten
2   A           Andersson
2   Anna        Andersson
2   B           Andersson
2   Bengt       Andersson
3   C           Diggory
3   A           Diggory
3   Cedric      Diggory

答案 1 :(得分:0)

对ID和姓氏使用条件聚合:

create procedure dbo.A
as
    select 1 as T
go
exec dbo.A
go
begin transaction
go
IF OBJECT_ID(N'dbo.A', N'P') IS NOT NULL
BEGIN 
    EXEC('drop procedure dbo.A')
END
go
create procedure dbo.A
as
    select penguin from sys.objects --This will fail
go
IF OBJECT_ID(N'dbo.A', N'P') IS NOT NULL
BEGIN
    commit
END
ELSE
BEGIN
    rollback transaction
END
go
exec dbo.A