Plsql Pl sql…查找发生次数

Plsql Pl sql…查找发生次数,plsql,oracle9i,Plsql,Oracle9i,假设我有一个日期为的第2个记录 表1和2套记录 key1 startdate1 startdate2 startdate3 100 2349 2456 2345 100 3456 3878 2872 日期是二进制格式的。我需要得到日期更改的计数,也就是说,在一个特定日期之后日期更改的次数 也就是说,假设在2011年4月14日之后,日期更改的次数(对于这两条记录,与特定的键1) 澄清 假设我有三条记录: +------+-------

假设我有一个日期为的第2个记录

表1和2套记录

key1 startdate1  startdate2 startdate3
100  2349         2456       2345
100  3456         3878       2872
日期是二进制格式的。我需要得到日期更改的计数,也就是说,在一个特定日期之后日期更改的次数 也就是说,假设在2011年4月14日之后,日期更改的次数(对于这两条记录,与特定的
键1

澄清 假设我有三条记录:

+------+-------------+-------------+-------------+ | key1 | start_date1 | start_date2 | start_date3 | +------+-------------+-------------+-------------+ | 701 | 08-SEP-2009 | 08-DEC-2009 | 08-jan-2010 | | 701 | 08-JUN-2013 | 08-SEP-2013 | 08-DEC-2013 | | 701 | 08-MAR-2017 | 08-MAR-2018 | 31-DEC-1899 | +------+-------------+-------------+-------------+ +------+-------------+-------------+-------------+ |按键1 |开始日期1 |开始日期2 |开始日期3| +------+-------------+-------------+-------------+ |701 | 08-SEP-2009 | 08-DEC-2009 | 08-jan-2010| |701 | 08-JUN-2013 | 08-SEP-2013 | 08-DEC-2013| |701 | 2017年3月8日| 2018年3月8日| 1899年12月31日| +------+-------------+-------------+-------------+ 我需要计算超过2011年4月14日的更改日期数。例如,对于键701,有5个更改日期大于2011年4月14日。我需要获得带有两个字段的表输出:

+------+---------------+ | Key1 | changedcount | +------+---------------+ | 701 | 5 | +------+---------------+ +------+---------------+ |键1 |更改计数| +------+---------------+ | 701 | 5 | +------+---------------+
现在你提出了更明确的问题:

select key1, count(distinct start_date)
from (
    select key1, start_date1 as start_date from myTable
    UNION ALL
    select key1, start_date2 as start_date from myTable
    UNION ALL
    select key1, start_date3 as start_date from myTable
) normalFormTable N
where start_date > myGivenDate
group by key

尝试规范化您的表:

WITH
  normalized_data AS
  (
    SELECT key1, start_date1 AS start_date FROM MYTABLE
  UNION ALL
    SELECT key1, start_date2 AS start_date FROM MYTABLE
  UNION ALL
    SELECT key1, start_date3 AS start_date FROM MYTABLE
  )

SELECT
  key1,
  COUNT(DISTINCT start_date) AS changedcount
FROM
  normalized_data
WHERE
  start_date > your_start_date_here
GROUP BY
  key1
你可以试试这个:

select sum(d1)+sum(d2)+sum(d3) , key
 from
(
select
  t.key,
  (select count(*) from test t1 where t1.key = t.key and start_date1>to_date('14-Apr-2011','DD-mon-yyyy')) d1,
  (select count(*) from test t2 where t2.key = t.key  and start_date2>to_date('14-Apr-2011','DD-mon-yyyy')) d2,
  (select count(*) from test t3 where t3.key = t.key  and start_date3>to_date('14-Apr-2011','DD-mon-yyyy')) d3
from  test t
group by key
)
group by key
如果您使用的是11g或更高版本,也可以尝试使用ORACLE PIVOT()函数。我目前没有安装11g,但如果你需要一个例子,我可以给你一个与此功能

问候,,
Alex

请写出更具可读性的问题。请不要使用“第二个”,请。。。发帖前请重新阅读您的问题。此问题不清楚。请包括另一个表格,说明您希望查询为您的样本数据生成的内容。假设我有3组记录key1 start_Date 1 start_Date 2 start_Date 3 701 08-SEP-2009 08-DEC-2009 08-jan-2010 701 08-JUN-2013 08-SEP-2013 08-DEC-2013 701 08-MAR-2017 08-MAR-2018 31-DEC-1899,因此我需要获得更改日期大于2011年4月14日。例如,对于特定的键701,3组行的更改日期计数为5 ir,大于2011年4月14日的日期为5计数。我需要获得一个带有两个字段的表输出,作为键1更改计数701 5。希望你得到我的问题。正如其他人所评论的,这个问题不清楚。如果你在寻求帮助,请花点时间确保我们知道您现在在问什么,问题也不清楚???假设我有3组记录key1 start_Date 1 start_Date 2 start_Date 3 701 08-SEP-2009 08-DEC-2009 08-jan-2010 701 08-JUN-2013 08-SEP-2013 08-DEC-2013701 2017年3月8日2018年3月8日1899年12月31日,因此我需要获得大于2011年4月14日的变更日期计数。例如,对于特定的键701,3组行的更改日期计数为5 ir,大于2011年4月14日的日期为5计数。我需要获得一个表输出,其中两个字段为键1 changedcount 701 5。希望你能回答我的问题