我正在研究Postgres 8.3数据库.我使用的查询仅用于选择工作日中包含的行.现在我已经像下面的例子中那样手动完成了这个,但是我想将它转移到一些功能,我可以在这里使用开始和结束日期,并获得相同的逻辑以应用如下.那是
如何创建一个输入为开始日期和结束日期的函数,函数的结果将是选择仅包含在数据集工作日的所有行(我想在where子句条件中排除每个星期六和星期日)下面)?
create table filter_tbl as
select *
from base_tbl where
(start_Time >= '2012-11-5' and start_Time < '2012-11-10')
or (start_time >= '2012-11-12' and start_time < '2012-11-17')
or (start_time >= '2012-11-19' and start_time < '2012-11-24')
or (start_time >= '2012-11-26' and start_time < '2012-12-01')
or (start_time >= '2012-12-03' and start_time < '2012-12-07')
or (start_time >= '2012-12-10' and start_time < '2012-12-14')
or (start_time >= '2012-12-17' and start_time < '2012-12-21')
or (start_time >= '2012-12-24' and start_time < '2012-12-28')
or (start_time >= '2012-12-31' and …Run Code Online (Sandbox Code Playgroud) 我正在尝试自动运行PLPGSQL一系列日期的函数的过程.
通常,我必须运行以下代码,每个函数调用每天生成一个表:
SELECT dhcp.singleday('2012-11-24'::date, '2012-11-25'::date);
SELECT dhcp.singleday('2012-11-25'::date, '2012-11-26'::date);
SELECT dhcp.singleday('2012-11-26'::date, '2012-11-27'::date);
SELECT dhcp.singleday('2012-11-27'::date, '2012-11-28'::date);
SELECT dhcp.singleday('2012-11-28'::date, '2012-11-29'::date);
SELECT dhcp.singleday('2012-11-29'::date, '2012-11-30'::date);
SELECT dhcp.singleday('2012-11-30'::date, '2012-12-01'::date);
SELECT dhcp.singleday('2012-12-01'::date, '2012-12-02'::date);
SELECT dhcp.singleday('2012-12-02'::date, '2012-12-03'::date);
SELECT dhcp.singleday('2012-12-03'::date, '2012-12-04'::date);
Run Code Online (Sandbox Code Playgroud)
有一个很好的方法可以通过一个简单的循环或函数自动化这种事情来处理任意日期范围吗?
我认为可能很难处理每个月的情况,所以我认为假设日期范围是一个月更好.
我试图弄清楚如何确定数据库中特定列的大小,例如我有两个名为sourceip,destinationip的列,它们都是16字节字段.
我认为这将在information_schema或\ d +中的某处,但我找不到一个特定的命令来隔离每个列类型的大小.
您可以在数据库中计算列类型大小,还是只需要在Postgresql文档中引用每种类型的字节大小?
我正在尝试编写一个查询来比较当前给定计算机的平均连接数与7到14天之前的平均连接数.我认为这最好由窗口函数处理,但我无法使日期的语法正确.
假设我有一个名为iptable的IP地址和连接记录表,其中包括soucreip,destinationip,timestamp作为列.这是我在前7天窗口中尝试获取每个sourceip的计数的查询:
select
sourceip,
destinationip,
timestamp,
count(*) OVER (PARTITION BY sourceip order by timestamp
RANGE BETWEEN now() - '7 day'::Interval PRECEDING
now() - '14 day'::Interval FOLLOWING)
from
iptable;
Run Code Online (Sandbox Code Playgroud)
编写这种类型的查询的最佳方法是,窗函数方法是否有意义,还是有更优化的方法来处理大型表的情况?
我使用json4s库将scala中的映射转换为json,但在Map [Char,Int]上操作时仍然遇到基本错误:
这是给我问题的代码示例.
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL.WithDouble._
val myMap = Map('a' -> 123)
render(myMap)
Run Code Online (Sandbox Code Playgroud)
错误:(Char,Int)=> org.json4s.JsonAST.JValue没有可用的隐式视图.
问题:使用Json4s将由[Char,Int]构成的Map转换为Json对象的正确方法是什么?
我的计算机运行的SQL查询的对一个大表PostgreSQL数据库上的集合(700万个新行/天),并已运行到与第一观点的一些性能问题,现在创建表.我使用的大多数命令与以下查询类似:
CREATE TABLE events_tb AS
SELECT *
FROM
(SELECT column1, column2, column3, column4, column5
FROM test_database_1
WHERE column6 = 'value1'
AND date_column > '2012-07-01'
AND date_column < '2012-07-10'
) a
INNER JOIN ( SELECT DISTINCT column1 FROM test_database_2) b
ON (a.column1 = b.column1);
Run Code Online (Sandbox Code Playgroud)
有没有办法改进上面的语句集合,以解释查询是针对非常大的表运行的?
这是一个跟踪问题,这里的帖子是Regex用于匹配URL的最后两部分
我想知道我是否可以使用内置的sql函数来完成相同类型的模式匹配而不使用正则表达式.特别是我在想是否有办法将字符串反转为www.stackoverflow.com到com.stackoverflow.www然后应用连接到split('com.stackoverflow.www,'.',1)|| split('com.stackoverflow.www,'.',2)我会完成但我不确定这是否可行.
这是一般问题描述:
我试图弄清楚最好的sql函数只是匹配由a分隔的最后两个字符串.在网址中.
例如www.stackoverflow.com我只想匹配stackoverflow.com
我遇到的问题是一些字符串可能有很多句号
a-abcnewsplus.i-a277eea3.rtmp.atlas.cdn.yimg.com
也应该只返回yimg.com
我正在使用的URL集没有任何路径信息,所以可以假设字符串的最后一部分总是.org或.com或那种性质的东西.
当针对www.stackoverflow.com运行时,sql函数将返回stackoverflow.com,并且在上述条件下针对a-abcnewsplus.i-a277eea3.rtmp.atlas.cdn.yimg.com运行时将返回yimg.com?我不想在解决方案中使用正则表达式只是sql字符串操作函数.
这是对这里发布的问题的后续跟进,这是针对输入文件名执行nohup的shell脚本.
我只是试图找到修改下面的no_hup脚本来执行SQL脚本的整个目录而不是单个文件.所以我试图找到一个很好的方法来修改下面的脚本来执行整个文件目录:
如何编写shell脚本以便我可以针对名为test的目录运行,该目录包含文件foo1.sql,foo2.sql,foo3.sql
./nohup_sh测试
这将产生输出
nohup psql -d db -f test/foo1.sql>&test/foo1.out&
nohup psql -d db -f test/foo2.sql>&test/foo2.out&
nohup psql -d db -f test/foo3.sql>&test/foo3.out&
这是我在上一个名为nohup_sh的答案中使用的代码
#!/bin/bash
outputFile="$(echo $1 | cut -d\. -f 1).out"
nohup psql -d db -f "$1" >& "$outputFile" &
Run Code Online (Sandbox Code Playgroud)