SQL oracle:Group和Count数据

时间:2018-03-20 09:11:39

标签: sql oracle

我有一张这样的表:

+-----+------+
| acc | CASE |
+-----+------+
| 001 | a    |
| 001 | b    |
| 001 | c    |
| 002 | a    |
| 002 | b    |
| 003 | b    |
| 003 | c    |
| 004 | a    |
| 005 | b    |
| 006 | b    |
| 007 | a    |
| 007 | b    |
| 007 | c    |
| 008 | a    |
| 008 | b    |
| n   | x    |
+-----+------+

我不知道如何使用

对数据进行分组和计数
+-----------+-----------+
|   case    | count_acc |
+-----------+-----------+
| a         | 1         |
| b         | 2         |
| c         | 0         |
| a+b       | 2         |
| b+c       | 1         |
| a+b+c     | 2         |
| a+b+c+…+x | n         |
+-----------+-----------+

如果a + b,b + c ... a + b + c + ... + x我不能分组案例和计数acc。你有什么想法分组和数数?

3 个答案:

答案 0 :(得分:1)

select b.case,count(distinct(a.acc)) as account from
test  a , (select acc  , rtrim(case,'+') case
from ( select acc , case , rn from test
           model
           partition by (acc)
           dimension by (row_number() over (partition by acc order by case) rn )
           measures  (cast(case as varchar2(10)) case)
           rules
           (case[any] order by rn desc = case[cv()]||'+'||case[cv()+1])
     )
where rn = 1) b
where a.acc = b.acc
group by b.case

答案 1 :(得分:0)

您可以使用oracle中的LISTAGG实现相同的目标

with test as ( select  001 acc , 'a' case   FROM DUAL UNION
SELECT  001 , 'b'     FROM DUAL UNION
SELECT  001 , 'c'     FROM DUAL UNION
SELECT  002 , 'a'     FROM DUAL UNION
SELECT  002 , 'b'     FROM DUAL UNION
SELECT  003 , 'b'     FROM DUAL UNION
SELECT  003 , 'c'    FROM DUAL UNION
SELECT  004 , 'a'     FROM DUAL UNION
SELECT  005 , 'b'     FROM DUAL UNION
SELECT  006 , 'b'     FROM DUAL UNION
SELECT  007 , 'a'     FROM DUAL UNION
SELECT  007 , 'b'    FROM DUAL UNION
SELECT  007 , 'c'     FROM DUAL UNION
SELECT  008 , 'a'     FROM DUAL UNION
SELECT  008 , 'b'     FROM DUAL )
select case,count(1) from
(
SELECT count(1),acc, LISTAGG(case, '+') WITHIN GROUP (ORDER BY acc) AS case
FROM   test
GROUP BY acc) GROUP BY case order by 1;

SQL fiddle here

答案 2 :(得分:0)

首先,您必须整理数据,对ACC进行分组并汇总CASE

SELECT case, count(*) FROM (
    SELECT 
        LISTAGG (case,'+') WITHIN GROUP (ORDER BY case) case, 
        ACC 
    FROM TEST 
    GROUP BY ACC
) GROUP BY case
ORDER BY CASE;

你可以算数:

        RsaKeyPairGenerator generator = new RsaKeyPairGenerator();

        generator.Init(new KeyGenerationParameters(new SecureRandom(), 2048));

        AsymmetricCipherKeyPair keyPair = generator.GenerateKeyPair();

        RsaKeyParameters keyParam = (RsaKeyParameters)keyPair.Public;

        var info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyParam);

        byte[] RsaBytes = info.GetEncoded();

如果您没有LISTAGG功能,例如11g以下,请参阅本网站:

http://oracle-base.com/articles/misc/string-aggregation-techniques