oracle查询复杂

时间:2017-01-04 05:22:24

标签: sql oracle

CREATE TABLE TEST_1(
    ID  NUMBER(15,0) NOT NULL,
    PERSON VARCHAR2(30),
    DATETIMES  VARCHAR2(21),
    primary key(ID)  
);    

ID       PERSON         DATETIMES
    1       PERSON-1    20160114-03:01:29
    2       PERSON-1    20160114-03:01:29
    3       PERSON-1    20160114-03:01:29
    4       PERSON-1    20160114-03:01:29
    5       PERSON-1    20160114-03:01:29
    6       PERSON-2    20160114-03:01:29
    7       PERSON-2    20160114-03:01:29
    8       PERSON-3    20160114-03:01:29
    9       PERSON-4    20160114-03:01:29
    10      PERSON-4    20160114-03:01:29
    ---------------------------------------
    11      PERSON-1    20160114-03:01:56
    12      PERSON-2    20160114-03:01:56
    13      PERSON-1    20160114-03:01:56
    ---------------------------------------
    14      PERSON-2    20160114-03:01:45
    15      PERSON-2    20160114-03:01:45
    16      PERSON-2    20160114-03:01:45
    17      PERSON-2    20160114-03:01:45
    18      PERSON-3    20160114-03:01:45
    19      PERSON-1    20160114-03:01:45
    20      PERSON-1    20160114-03:01:45

首先按照DATETIME的顺序按计数

取出上面的表组(*)
count(*)    DATETIME
---------   ------------
10          20160114-03:01:29
7           20160114-03:01:45
3           20160114-03:01:56

根据上表中日期时间的顺序。我需要下表

count(*)    PERSON          DATETIMES
---------   ------------    -----------------
5           PERSON-1        20160114-03:01:29
2           PERSON-2        20160114-03:01:29
2           PERSON-4        20160114-03:01:29
1           PERSON-3        20160114-03:01:29

count(*)    PERSON          DATETIMES
---------   ------------    -----------------
4           PERSON-2        20160114-03:01:45
2           PERSON-1        20160114-03:01:45
1           PERSON-3        20160114-03:01:45

count(*)    PERSON          DATETIMES
---------   ------------    -----------------
2           PERSON-1        20160114-03:01:56
1           PERSON-2        20160114-03:01:56

需要此表:逐个人,日期时间

inside group,
    order by count(*) 
but group wise 
    order by DATETIMES

我试过但我不能得到这张桌子......

3 个答案:

答案 0 :(得分:0)

据我了解你的问题,你想要select

SELECT COUNT(*), PERSON, DATETIME FROM table_name GROUP BY PERSON, DATETIME ORDER BY DATETIME

答案 1 :(得分:0)

检查这个。

        select COUNT(*) Count ,PERSON,datetimes from #TableName
        group by PERSON,datetimes
        order by datetimes asc , Count desc
  

输出:

enter image description here

检查演示here

答案 2 :(得分:0)

您需要使用Oracle分析功能。他们可以count使用指定的群组,而无需使用group by声明

这是适合你的版本。

select distinct 
 count(*) over(partition by datetimes,person ) count,
 count(*) over(partition by datetimes ) group_count,
 person,
 datetimes 
from TEST_1 order by 2 desc,1 desc



COUNT   GROUP_COUNT PERSON  DATETIMES
---------------------------------------------
5       10          PERSON-1    20160114-03:01:29
2       10          PERSON-2    20160114-03:01:29
2       10          PERSON-4    20160114-03:01:29
1       10          PERSON-3    20160114-03:01:29
4       7           PERSON-2    20160114-03:01:45
2       7           PERSON-1    20160114-03:01:45
1       7           PERSON-3    20160114-03:01:45
2       3           PERSON-1    20160114-03:01:56
1       3           PERSON-2    20160114-03:01:56

Coulmn 2是额外的,但它是order by子句所必需的。如果要从结果中删除该列,可以在此查询上编写包装器。

  • Oder by 2(组计数)确保DATETIMES根据组计数排序。
  • 按1(计数)排序可确保在每个组中persons根据countdatetimes
  • 进行排序

如果您想尝试自己http://rextester.com/YQQ34095

,这是链接
相关问题