多个列oracle

时间:2019-12-06 07:25:31

标签: oracle count distinct

我有下表,我想计算两列之间的不同值。

ID_DATE     DESCRIPT1   DESCRIPT2
20191001    A           R
20191001    D           B
20191001    B           D
20191001    A           B
20191002    A           B
20191002    C           A
20191002    A           B

以下是我的查询,但结果不准确

SELECT  
COUNT(distinct DESCRIPT1 || ' - ' ||  DESCRIPT2) AS ALL_DESCRIPT,
COUNT(DISTINCT DESCRIPT1) AS DESCRIPT_A, 
COUNT(DISTINCT DESCRIPT2) AS DESCRIPT_B, 
ID_DATE FROM MY_TABLE  GROUP BY ID_DATE;

我的结果

enter image description here

ALL_DESCRIPT    DESCRIPT_A  DESCRIPT_B  ID_DATE
4               3           3           20191001
2               2           2           20191002

在我的结果中,ID_DATE 20191002的列ALL_DESCRIPT给我总计2而不是3。应该为3,因为我在两个列{{1中都具有A,B和C,总计为3 }}和DESCRIPT1 我在哪里做错了。

下面是用于在oracle中进行测试的插入查询,以防万一。

DESCRIPT2

4 个答案:

答案 0 :(得分:2)

要添加到littlefoot的查询中并提供其他列,这似乎是一项重要操作:

select 
  id_date, 
  count(distinct descript) all_descript,
  count(case when descript = 'A' then 1 end) as descript_a,
  count(case when descript = 'B' then 1 end) as descript_B
from 
(
  select id_date, descript1 descript
  from src_data
  union all
  select id_date, descript2 descript 
  from src_data
) x
group by id_date
order by id_date;

您可以按照以下方式为不同的字母添加更多列:遵循在字符串中放置另一个后者并以不同方式命名该列的模式。它的工作原理是在数据例如为0时返回非null值。 A,如果数据不是A,则为null。Count仅计算非null数据。使用SUM(CASE WHEN descript = 'A' THEN 1 ELSE 0 END)对您来说可能更有意义-效果相同


编辑:实际上我认为我误解了该请求。尝试以下方法:

    select 
      id_date, 
      count(distinct descript) all_descript,
      count(distinct descript1) as descript_a,
      count(distinct descript2) as descript_B
    from 
    (
      select id_date, descript1 descript, descript1, descript2
      from src_data
      union all
      select id_date, descript2 descript, null, null
      from src_data
    ) x
    group by id_date
    order by id_date

答案 1 :(得分:1)

我看不到图像,但是-根据您的描述,看起来像

SQL> select id_date, count(distinct descript) cnt
  2  from (select id_date, descript1 descript from src_data
  3        union all
  4        select id_date, descript2 descript from src_data
  5       )
  6  group by id_date
  7  order by id_date;

ID_DATE         CNT
-------- ----------
20191001          4
20191002          3

SQL>

如果您添加显示来源的列(在我的示例中为what),那么您会

SQL> select id_date,
  2    count(distinct descript) cnt,
  3    count(distinct case when what = 'A' then descript end) descript_a,
  4    count(distinct case when what = 'B' then descript end) descript_b
  5  from (select 'A' what, id_date, descript1 descript from src_data
  6        union all
  7        select 'B' what, id_date, descript2 descript from src_data
  8       )
  9  group by id_date
 10  order by id_date;

ID_DATE         CNT DESCRIPT_A DESCRIPT_B
-------- ---------- ---------- ----------
20191001          4          3          3
20191002          3          2          2

SQL>

答案 2 :(得分:1)

遇到聚合问题时,您始终可以编写单独的聚合查询,然后将它们加入。在您的情况下,可能是:

select t1.all_descript, t2.descript_a, t2.descript_b, id_date
from -- this subquery gets you the overall distinct count
(
  select id_date, count(*) as all_descript
  from 
  (
    select id_date, descript1 from mytable
    union
    select id_date, descript2 from my_table
  )
  group by id_date
) t1
join -- this subquery gets you the separate distinct counts
(
  select
    id_date,
    count(distinct descript1) as descript_a,
    count(distinct descript2) as descript_b
  from my_table
  group by id_date
) t2 using (id_date)
order by id_date;

答案 3 :(得分:1)

这应该可以解决您的查询,我只是使用一个内存表将不同的列存储在内存中,然后将它们称为并以不同的形式计数。

WITH b AS  (
              SELECT id_date,DESCRIPT1  col1,descript1,descript2 FROM 
              SRC_DATA
              UNION 
            SELECT id_date,DESCRIPT2  col1,descript1,descript2 FROM SRC_DATA
           )
SELECT id_date,count(DISTINCT col1) col1,count(DISTINCT descript1) 
    descript1,count(DISTINCT descript2) descript2 
FROM b
GROUP BY id_date