从每个站点选择最新行?

时间:2017-06-21 22:05:36

标签: sql oracle11g oracle-sqldeveloper business-intelligence

我正在尝试从以下脚本中选择每个客户站点的最新行:

select   "CUSTOM_ATTRIBUTE"."VALUE" as "SO_NUMBER",
     "CONTRACT"."END_DATE" as "CONTRACT_END_DATE",
     "ALERT"."LAST_UPDATED_TIMESTAMP" as "LAST_UPDATED_TIMESTAMP_1",
     "DATA_POINT_UPLOAD_DATA"."VALUE" as "DP_UPLD_DATA_VALUE",
     "SITE"."NAME" as "SITE_NAME",
     "SITE"."LAST_UPDATED_TIMESTAMP" as "LAST_UPDATED_TIMESTAMP",
     "CONTRACT_TYPE"."NAME" as "CONTRACT_TYPE_NAME",
     "ALERT"."USER_ID" as "ALERT_USER_ID",
     "ALERT"."ACKNOWLEDGEMENT_TIME" as "NOTES_TIMESTAMP",
     "ALERT"."EVENT" as "ALERT_EVENT",
     "ALERT"."ACKNOWLEDGEMENT_NOTE" as "NOTES",
     "ALERT"."CASE_NO" as "ALERT_CASE_NO",
     "SITE"."ID" as "SITE_ID" 
 from   "DB"."ALERT" "ALERT",
    "DB"."DATA_POINT_UPLOAD_DATA" "DATA_POINT_UPLOAD_DATA",
    "DB"."COMPONENT" "COMPONENT",
    "DB"."COMPONENT_DATA_POINT" "COMPONENT_DATA_POINT",
    "DB"."CONTRACT" "CONTRACT",
    "DB"."CONTRACT_TYPE" "CONTRACT_TYPE",
    "DB"."CUSTOM_ATTRIBUTE" "CUSTOM_ATTRIBUTE",
    "DB"."SITE" "SITE" 
 where   "SITE"."ID"="CUSTOM_ATTRIBUTE"."SITE_ID"
 and     "CUSTOM_ATTRIBUTE"."SITE_ID"="CONTRACT"."SITE_ID"
 and     "CONTRACT"."CONTRACT_TYPE_ID"="CONTRACT_TYPE"."ID"
 and     "CUSTOM_ATTRIBUTE"."SITE_ID"="COMPONENT"."SITE_ID"
 and     "COMPONENT"."ID"="COMPONENT_DATA_POINT"."COMPONENT_ID"
 and     "COMPONENT_DATA_POINT"."ID"="DATA_POINT_UPLOAD_DATA"."COMPONENT_DATA_POINT_ID"
 and     "ALERT"."SITE_ID"="CUSTOM_ATTRIBUTE"."SITE_ID"
  and    "CUSTOM_ATTRIBUTE"."NAME" ='SO#' 
   and   "COMPONENT_DATA_POINT"."NAME" ='State'
   and   "SITE"."EQUIPMENT_TYPE_ID" ='250';

当前输出:

...... 5,000输出......

SO_NUMBER                      CONTRACT_END_DATE LAST_UPDATED_TIMESTAMP_1            DP_UPLD_DATA_VALUE                                                                                   SITE_NAME                                                                                            LAST_UPDATED_TIMESTAMP              CONTRACT_TYPE_NAME                                                           ALERT_USER_ID NOTES_TIMESTAMP                     ALERT_EVENT                                        NOTES                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ALERT_CASE_NO                                         SITE_ID
------------------------------ ----------------- ----------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------- -------------------------------------------------- --------------------------------------- ----------------------------------- -------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ----------
144732                         30-JUN-19         14-JUN-17 04.57.07.119266000 AM     Some_Event_Occurred                                                                                  Customer Name                                                                                  10-NOV-16 08.19.47.000000000 PM     Some Contract                                                                   1169 09-JUN-17 03.34.10.019229000 PM     Some Notes Entered                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ABC123
Only 5,000 rows currently supported in a script results
 5,000 rows selected 

预期产出:

SO_NUMBER                      CONTRACT_END_DATE LAST_UPDATED_TIMESTAMP_1            DP_UPLD_DATA_VALUE                                                                                   SITE_NAME                                                                                            LAST_UPDATED_TIMESTAMP              CONTRACT_TYPE_NAME                                                           ALERT_USER_ID NOTES_TIMESTAMP                     ALERT_EVENT                                        NOTES                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ALERT_CASE_NO                                         SITE_ID
------------------------------ ----------------- ----------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------- -------------------------------------------------- --------------------------------------- ----------------------------------- -------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ----------
145732                         30-JUN-19         14-JUN-17 04.57.07.119266000 AM     Some_Run_Event                                                                                      Customer 1                                                                                  10-NOV-16 08.19.47.000000000 PM      Contract                                                                   1169 09-JUN-17 03.34.10.019229000 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        100763
146733                         30-JUN-19         10-JUN-17 03.20.06.396989000 AM     Some_Run_Event                                                                                      Customer 2                                                                                   10-NOV-16 08.19.47.000000000 PM      Contract                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              100764
144734                         30-JUN-19         10-JUN-17 03.29.07.172041000 AM     Some_Run_Event                                                                                      Customer 3                                                                                   10-NOV-16 08.19.47.000000000 PM      Contract                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              100765
144835                         30-JUN-19         10-JUN-17 03.44.06.692450000 AM     Some_Run_Event                                                                                      Customer 4                                                                                   10-NOV-16 08.19.47.000000000 PM      Contract                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              100766
144936                         30-JUN-19         10-JUN-17 06.30.06.828320000 AM     Some_Run_Event                                                                                      Customer 5                                                                                  10-NOV-16 08.19.47.000000000 PM      Contract                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               100767
144937                         30-JUN-19         10-JUN-17 06.58.06.163756000 AM     Some_Run_Event                                                                                      Customer 6                                                                                   10-NOV-16 08.19.47.000000000 PM      Contract                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              100768

我正在尝试从每个站点ID获取最新行,而不是从每个站点ID获取数千行。

2 个答案:

答案 0 :(得分:1)

假设"最新"你的意思是alert.last_updated_timestamp中的最新日期,然后创建一个子查询,该子查询使用聚合来获取每个SiteID的此日期的max()值,然后将该子查询连接到您已经拥有的查询,将输出限制为那些时间

或者如果"最新"意味着别的东西,然后相应地调整;相同的技术将适用。

答案 1 :(得分:0)

使用

“SITE”。“ID”为“SITE_ID”,

ROW_NUMBER()over(由ALITE.LAST_UPDATED_TIMESTAMP desc按SITE.ID顺序划分)为temp_col

作为现有select语句的最后一列,并添加一个外部查询来过滤SELECT * FROM(....现有查询),其中temp_col = 1