sql oracle count发生日期

时间:2015-05-01 17:06:38

标签: sql oracle

我有大量的药物和日期数据 一种药物可能在同一个月内开处方不止一次

我需要列出详细信息,只要药物每月处方超过3次 老实说我对日期查询不好

select Tradename as Drug, TO_CHAR(PrescDT,'MM-YYYY') as Date_Prescribe
from PRESCRIPTION
group by TradeName
having count(PrescDT) > 3;

给我一​​个错误“不是GROUP BY表达式”并且结果是错误的。 提前致谢

4 个答案:

答案 0 :(得分:0)

以下查询应该有效:

select Tradename as Drug, TO_CHAR(PrescDT,'MM-YYYY') as Date_Prescribe
    from PRESCRIPTION
    group by TradeName,to_char(PrescDT,'MM-YYYY') 
    having to_char(PrescDT,'MM-YYYY')>3

答案 1 :(得分:0)

select Tradename as Drug, TO_CHAR(PrescDT,'MM-YYYY') as Date_Prescribe
from PRESCRIPTION
group by TradeName, TO_CHAR(PrescDT,'MM-YYYY')
having count(*) > 3;

您可以使用TRUNC(PrescDT,'MM')代替TO_CHAR,而应该提高效率。

答案 2 :(得分:0)

使用CTE数据:

with prescription as (
  select 'advil' as tradename, TO_DATE('04-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'advil' as tradename, TO_DATE('04-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'advil' as tradename, TO_DATE('04-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'advil' as tradename, TO_DATE('06-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'tylenol' as tradename, TO_DATE('09-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'advil' as tradename, TO_DATE('04-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'tylenol' as tradename, TO_DATE('08-2015', 'MM-YYYY') as PrescDT from dual UNION ALL
  select 'morphine' as tradename, TO_DATE('10-2015', 'MM-YYYY') as PrescDT from dual 
)
select tradename as Drug, PrescDt as Date_Prescribe
from prescription
group by tradename, prescdt
having count(prescdt) > 3
;

输出:

DRUG   DATE_PRESCRIBE
advil   04-2015

答案 3 :(得分:0)

试试这个:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor clearColor]]; }