Sum count of a specific value

时间:2017-07-17 15:26:53

标签: sql oracle-sqldeveloper oracle-bi

I am trying to get the total count for a specific "COMPONENT_DATA_POINT"."NAME" as it relates to the "DATA_POINT_UPLOAD_DATA"."VALUE". For example, here is my current query:

select "DATA_POINT_UPLOAD_DATA"."VALUE" as "EQUIP_ONLINE_VALUE" 
 from   "DB"."DATA_POINT_UPLOAD_DATA" "DATA_POINT_UPLOAD_DATA"
where  
       "DATA_POINT_UPLOAD_DATA"."COMPONENT_UPLOAD_DATA_ID" IN (select id from (select "COMPONENT_UPLOAD_DATA".* from "DB"."COMPONENT_UPLOAD_DATA" "COMPONENT_UPLOAD_DATA",
               "DB"."COMPONENT" "COMPONENT"
where  
       "COMPONENT_UPLOAD_DATA"."DATA_COLLECTED_TIME" < CURRENT_DATE
   and "COMPONENT_UPLOAD_DATA"."COMPONENT_NO"='1'
   and "COMPONENT_UPLOAD_DATA"."SITE_UPLOAD_DATA_SITE_ID"="COMPONENT"."SITE_ID"
   and "COMPONENT_UPLOAD_DATA"."COMPONENT_ID"="COMPONENT"."ID"
   and "COMPONENT"."COMPONENT_TYPE_ID" = '123'
   and "COMPONENT"."SITE_ID" in ('123ABC')
ORDER BY "COMPONENT_UPLOAD_DATA"."DATA_COLLECTED_TIME" DESC)
where rownum <= 1)
   and "DATA_POINT_UPLOAD_DATA"."COMPONENT_DATA_POINT_ID" IN (select COMPONENT_DATA_POINT_ID from (select  "DATA_POINT_UPLOAD_DATA".*
from     "DB"."COMPONENT_DATA_POINT" "COMPONENT_DATA_POINT",
               "DB"."DATA_POINT_UPLOAD_DATA" "DATA_POINT_UPLOAD_DATA",
               "DB"."COMPONENT" "COMPONENT" 
 where  "DATA_POINT_UPLOAD_DATA"."COMPONENT_DATA_POINT_ID"="COMPONENT_DATA_POINT"."ID"
   and "COMPONENT_DATA_POINT"."NAME" ='EquipUp'
   and "COMPONENT_DATA_POINT"."COMPONENT_ID" = "COMPONENT"."ID"
   and "COMPONENT"."COMPONENT_TYPE_ID" = '123'
   and "COMPONENT"."SITE_ID" in ('123ABC')));

Current output I believe only shows one count, and not a total sum of all 'EquipUp':

EQUIP_ONLINE_VALUE                                                                                   
------------------------
1

I would like to get a total 'EQUIP_ONLINE_VALUE' sum of all 'EquipUp' names.

Any help would be greatly appreciated.

Thanks.

1 个答案:

答案 0 :(得分:0)

  • 您的查询中没有计数/求和功能。
  • 您的where子句仅返回1行,其中包含DATA_POINT_UPLOAD_DATA.VALUE 1.
  • 你确定你的rownum&lt; = 1是正确的吗?
  • 尝试单独运行where子句中的每个部分以确定它们是否存在 带回多行数据。
  • 如果您需要各自的总和 EquipUp,使用来自... group的SUM(值)BY COMPONENT_DATA_POINT.NAME

运行这两个脚本以确定您拥有正确的数据:

SELECT "COMPONENT_UPLOAD_DATA".*
        FROM "DB"."COMPONENT_UPLOAD_DATA" "COMPONENT_UPLOAD_DATA"
            ,"DB"."COMPONENT" "COMPONENT"
        WHERE "COMPONENT_UPLOAD_DATA"."DATA_COLLECTED_TIME" < CURRENT_DATE
            AND "COMPONENT_UPLOAD_DATA"."COMPONENT_NO" = '1'
            AND "COMPONENT_UPLOAD_DATA"."SITE_UPLOAD_DATA_SITE_ID" = "COMPONENT"."SITE_ID"
            AND "COMPONENT_UPLOAD_DATA"."COMPONENT_ID" = "COMPONENT"."ID"
            AND "COMPONENT"."COMPONENT_TYPE_ID" = '123'
            AND "COMPONENT"."SITE_ID" IN ('123ABC')
        ORDER BY "COMPONENT_UPLOAD_DATA"."DATA_COLLECTED_TIME" DESC

SELECT "DATA_POINT_UPLOAD_DATA".*
        FROM "DB"."COMPONENT_DATA_POINT" "COMPONENT_DATA_POINT"
            ,"DB"."DATA_POINT_UPLOAD_DATA" "DATA_POINT_UPLOAD_DATA"
            ,"DB"."COMPONENT" "COMPONENT"
        WHERE "DATA_POINT_UPLOAD_DATA"."COMPONENT_DATA_POINT_ID" = "COMPONENT_DATA_POINT"."ID"
            AND "COMPONENT_DATA_POINT"."NAME" = 'EquipUp'
            AND "COMPONENT_DATA_POINT"."COMPONENT_ID" = "COMPONENT"."ID"
            AND "COMPONENT"."COMPONENT_TYPE_ID" = '123'
            AND "COMPONENT"."SITE_ID" IN ('123ABC')