简体   繁体   English

Excel Postgresql数据库连接使用单元格作为日期

[英]Excel Postgresql Database Connection Use Cell as Date

I am trying to write the SQL command for a database that uses a cell value as a date parameter. 我正在尝试为使用单元格值作为日期参数的数据库编写SQL命令。 Here is an example query without the cell value: 这是一个没有单元格值的查询示例:

SELECT usda_cattle_auctions.weight_average, usda_cattle_auctions.average_price
FROM public.usda_cattle_auctions usda_cattle_auctions
WHERE (usda_cattle_auctions.report_date>={d '2012-03-24'} And usda_cattle_auctions.report_date<={d '2014-10-25'}) AND (usda_cattle_auctions.state='CO') AND (usda_cattle_auctions.gender='Steers') AND (usda_cattle_auctions.class_type='Feeder')
ORDER BY usda_cattle_auctions.report_date DESC

I read this post: Excel: Use a cell value as a parameter for a SQL query 我读了这篇文章: Excel:使用单元格值作为SQL查询的参数

However when I adjusted one parameter like this: 但是,当我像这样调整一个参数时:

SELECT usda_cattle_auctions.weight_average, usda_cattle_auctions.average_price
FROM public.usda_cattle_auctions usda_cattle_auctions
WHERE (usda_cattle_auctions.report_date>={d [MarketValCal$d3]} And usda_cattle_auctions.report_date<={d '2014-10-25'}) AND (usda_cattle_auctions.state='CO') AND (usda_cattle_auctions.gender='Steers') AND (usda_cattle_auctions.class_type='Feeder')
ORDER BY usda_cattle_auctions.report_date DESC

The query will no longer work because I get an invalid syntax warning. 该查询将不再起作用,因为我收到了无效的语法警告。 How should I properly structure this query? 我应该如何正确构造此查询? Any help is greatly appreciated. 任何帮助是极大的赞赏。

EDIT: 编辑:

I took out the quotes and tried to do this with a none date parameter and now I get the message "the # of binded parameters < the # of parameter markers" 我拿出引号,并尝试使用无日期参数来执行此操作,现在我收到消息“绑定参数的数量<参数标记的数量”

I found the best solution to my problem. 我找到了解决问题的最佳方法。 You can modify use a query like so: 您可以像这样修改使用查询:

SELECT usda_cattle_auctions.weight_average, usda_cattle_auctions.average_price
FROM public.usda_cattle_auctions usda_cattle_auctions
WHERE (usda_cattle_auctions.report_date>= ? And usda_cattle_auctions.report_date<={d '2014-10-25'}) AND (usda_cattle_auctions.state='CO') AND (usda_cattle_auctions.gender='Steers') AND (usda_cattle_auctions.class_type='Feeder')
ORDER BY usda_cattle_auctions.report_date DESC

Modify the query by going to connection properties under the data tab and click on definition. 通过转到数据选项卡下的连接属性并单击定义来修改查询。 Just substitute the command text and excel will ask for the parameters after you click ok. 只需替换命令文本,单击确定后excel就会要求输入参数。 Then a parameter button will show up in connection properties so you can edit the parameters. 然后,连接属性中将显示一个参数按钮,以便您可以编辑参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM