PL / SQL - 你怎么得到" first()"行显示并告诉它不计算空值?

时间:2015-06-15 14:06:28

标签: sql oracle plsql oracle-sqldeveloper

我是PL / SQL的新手,但有使用Proc SQL(SAS)的经验。在过去,每当我有"计算"一个特定的变量,它将跳过空值。然而,在PL / SQL中,我注意到它仍然在计算它们。

示例查询(计算pl / sql中的空值):

select 
  month, 
  count(et_referral_traffic) as refer, 
  count(et_direct_traffic)   as direct, 
  count(et_organic_search)   as organic, 
  count(et_olavisit)         as visit, 
  count(et_olaimpression)    as olai, 
  count(et_paid_search)      as paid
from
(
  select distinct 
    userid, 
    extract(month from timestamp) as month, 
    et_referral_traffic, 
    et_direct_traffic, 
    et_organic_search, 
    et_olavisit, 
    et_olaimpression, 
    et_paid_search
  from 
    EPIPEN_CLEAN_20150607
)
group by month;

此外,我总是能够选择一个特定的变量然后通过选择First(variable_name)来聚合另一个变量,所以我将得到该variable_name的第一行,并按照我需要的其他变量对其进行排序以获得正确的变量

实施例

select first_entry, count(touchpointid) as total_entries
from
(
select touchpointid, sessionid, first(revisedentrytype) as first_entry, et_key_action
from epipen_clean_20150607
   group by sessionid, touchpointid
   order by sessionid, touchpointid
)
group by first_entry;

第二个查询是我最困惑的问题,因为我始终能够在其他形式的SQL中执行此类查询。任何关于如何重写这个以在PL SQL中工作的帮助都将非常感谢!!

谢谢!!!

2 个答案:

答案 0 :(得分:0)

您不需要PL / SQL - 您可以使用select first_entry, count(touchpointid) as total_entries from ( select touchpointid, sessionid, MIN(revisedentrytype) KEEP ( DENSE_RANK FIRST ORDER BY time ) AS first_entry from epipen_clean_20150607 group by sessionid, touchpointid order by sessionid, touchpointid ) group by first_entry [文档here使用SQL执行此操作,另一种解释是here]:

SQL Fiddle

Oracle 11g R2架构设置

| FIRST_ENTRY | TOTAL_ENTRIES |
|-------------|---------------|
|           B |             1 |
|           A |             3 |

查询1

static final int maximumPeople = 65;

<强> Results

while (totalPeople <= maximumPeople && totalPeople >= 0) 
{      
   //code
}

if (totalPeople > maximumPeople) {
   //code
}

答案 1 :(得分:0)

SQL count()聚合函数忽略作为参数传递的null值,请检查this example

要获得第一个值,您需要first_value分析函数,但在任何暗示排序的函数中,您需要指定如何排序值。您可以找到示例here

但是如下所示group by的示例没有指定任何排序,您只需要使用min()聚合函数来获取组中的最小(或第一)值:

select 
  first_entry, 
  count(touchpointid) as total_entries
from
(
  select 
    touchpointid, 
    sessionid, 
    min(revisedentrytype) as first_entry, 
    et_key_action
  from 
    epipen_clean_20150607
   group by sessionid, touchpointid
)
group by first_entry;