【问题标题】:postgresql/pgAdmin - accepting start_date & end_date arguments as input on query runpostgresql/pgAdmin - 接受 start_date 和 end_date 参数作为查询运行的输入
【发布时间】:2020-10-07 18:00:43
【问题描述】:

这是我正在使用 pgAdmin 处理的 postgresql 数据库。

如果这是常识,请原谅我,尤其是我是 postgresql 的新手......而且我没有通过之前的搜索找到任何直接的答案。

我想知道当查询运行使用 pgadmin 及其任何内置功能时,是否有一种简单的方法可以将 start_time/end_time 参数作为输入实现。

我在这里使用的数据类型是“带时区的时间戳”。

寻找实现这一点的最佳方式的方向。

我考虑将 start_time 和 end_time 声明为变量,然后使用 WHERE 基于这些进行过滤,但没有第 3 方/应用程序级别的解决方案,有没有办法在 pgadmin 内部运行查询时提示输入?

感谢任何建议 - 这是我试图让某些东西正常工作的尝试,但它出错了:查询没有结果数据的目的地。

do $$
DECLARE
    start_date timestamp := '2020-10-1';
    end_date timestamp := '2020-10-5';
begin

select distinct on (account.id, menu.name, kitchen_item.name)
account.id as "Account ID",
account.firstname as "Seller First Name", 
account.lastname as "Seller Last Name",
account.email as "Seller Email",
account.phone as "Seller Phone",
address.address as "Seller Address (Street)",
address.address_2 as "Seller Address 2",
account.zip_code as "Seller Zip",
address.neighborhood as "Seller Neighborhood",
menu.name as "Name of active menu",
kitchen_item.name as "Dishes", 
kitchen_item.price as "Price",
kitchen_item.daily_max_orders as "Quantity",
menu.pickup_start_time as "Start time", 
menu.pickup_end_time as "End time",
menu.repeat_mon as "Monday",
menu.repeat_tues as "Tuesday",
menu.repeat_wed as "Wednesday",
menu.repeat_thurs as "Thursday",
menu.repeat_fri as "Friday",
menu.repeat_sat as "Saturday", 
menu.repeat_sun as "Sunday",
order_item.created as "Date of last sale"
from account
left join store on account.id = store.account_id
left join menu on store.id = menu.store_id
left join menu_item on menu.id = menu_item.menu_id
left join kitchen_item on (menu_item.kitchen_item_id = kitchen_item.id and store.id = kitchen_item.store_id)
left join orders on (orders.store_id = store.id)
left join order_item on (order_item.order_id = orders.id)
join store_address on store.id = store_address.store_id
join address on store_address.address_id = address.id
where orders.placed BETWEEN start_date AND end_date
order by account.id asc, menu.name, kitchen_item.name asc, order_item.created desc;

end $$;

【问题讨论】:

  • IMO 忘记 pgAdmin4 - 它是垃圾。我根本不喜欢 JAVA,但 dBeaver 可能是一个很好的解决方案。

标签: postgresql subquery declaration sql-function


【解决方案1】:

DO 创建一个不返回数据的匿名函数。 你可以使用WITH:

WITH input (start_date, end_date) AS 
(SELECT '2020-10-01'::timestamp AS start_date,
'2020-10-05'::timestamp AS end_date)
SELECT ...
FROM...
JOIN input 
WHERE orders.placed BETWEEN input.start_date AND input.end_date

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2021-09-27
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多