是否可以将搜索查询更新为仅显示过去 30 天内看到的设备?
Posted
技术标签:
【中文标题】是否可以将搜索查询更新为仅显示过去 30 天内看到的设备?【英文标题】:Would it be possible to update the search query to only display devices that have been seen within the last 30 days? 【发布时间】:2021-10-25 08:31:10 【问题描述】:SELECT name as "Device Name",description as "Description",last_seen_at as "Last Seen At",collector as "Collector",alias_name as "Alias Name",
model as "Model",device_types as "Device Types",ip_function_type as "IP Function Type",system_oid as "System OID",vendor as "Vendor",
host_name as "Host Name",primary_ip_address as "Primary IP Address",city as "City",state as "State",
collection_system as "Collection System",location as "Location",location_description as "Location Description",
std_device_name as "Standard Device Name",collector_alternate_id as "Collector Alternate Id",create_time as "Create Time",
device_model_id as "Device Model Id",location_id as "Location Id",display_name as "Display Name",
display_description as "Display Description",supported_protocols as "Supported Protocols",polled_item_count as "Polled Iten Count",
life_cycle_state as "Life Cycle State",life_cycle_state_last_chg_on as "Life Cycle State Last Change On",
contact_status as "Contact Status",calculated_contact_status as "Calculated Contact Status",last_updated_at as "Last Updated At",npm_status as "NPM Status",node_id as "Node Id"
FROM NPM_DEVICES
WHERE --device_id > 20000000 and
(Lower(name) LIKE Lower('%$Search%') or Lower(description) LIKE Lower('%$Search%') or Lower(collector) LIKE Lower('%$Search%')
or Lower(alias_name) LIKE Lower('%$Search%') or Lower(model) LIKE Lower('%$Search%') or Lower(device_types) LIKE Lower('%$Search%')
or ip_function_type LIKE '%$Search%' or Lower(display_name) LIKE Lower('%$Search%') or
Lower(display_description) LIKE Lower('%$Search%') or Lower(system_oid) LIKE Lower('%$Search%') or
Lower(location) LIKE Lower('%$Search%') or Lower(location_description) LIKE Lower('%$Search%') or
Lower(vendor) LIKE Lower('%$Search%') or Lower(life_cycle_state) LIKE Lower('%$Search%') or
Lower(contact_status) LIKE Lower('%$Search%') or Lower(state) LIKE Lower('%$Search%') or
Lower(city) LIKE Lower('%$Search%') or Lower(std_device_name) LIKE Lower('%$Search%') or
collector_alternate_id LIKE '%$Search%' or primary_ip_address LIKE '%$Search%'
or Lower(host_name) LIKE Lower('%$Search%'))
LIMIT $resultlimit;
以上查询需要获取最近 30 天内最后一次出现的设备
【问题讨论】:
能否格式化查询,使其可读? 【参考方案1】:在 WHERE 子句中添加类似的内容:
AND CAST(last_updated_at AS DATE) BETWEEN DATEADD(DAY, -30, CAST(NOW() AS DATE)) AND NOW()
【讨论】:
感谢您的解决方案,但我们在 NPM_DEVICES 表中没有列日 在该示例中,DAY 是 SQL 函数 DATEADD 的一部分,用于指示间隔。只是PostgreSQL没有实现DATEADD,第一个答案显示了postgres中如何解决相同的要求。希望这会有所帮助。【参考方案2】:您可以使用 last_updated_at 列来获取最近 30 天的记录。
where last_updated_at BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE()
【讨论】:
我们有一个 last_seen_at 列,因此我们可以使用它,因为获取在 30 天时已被看到的设备以及在使用此条件时“其中 last_updated_at BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE( )" 得到 sytanx 错误作为 pq: 在 "30" 或附近出现语法错误以上是关于是否可以将搜索查询更新为仅显示过去 30 天内看到的设备?的主要内容,如果未能解决你的问题,请参考以下文章
是否不仅可以在 Firebase Analytics 中查看过去 30 分钟内的事件?