我们在SQL Server 2012中有以下SQL脚本。我们将在数据库转换时在postgresql(HAWQ 1.3.1)中编写类似的脚本
SELECT *
FROM tablename_1
LEFT OUTER JOIN
(SELECT
SUM(b.OrderValue) AS OrderValue, b.OrderDate, b.Description
FROM
(SELECT *
FROM tablename_2 rcd
LEFT JOIN
(SELECT Distinct
afv.Item, afv.Description, afd.KeyField
FROM tablename_3 afd
JOIN tablename_3 afv ON afv.FormType = afd.FormType
AND afv.FieldName = afd.FieldName
AND afv.Item = afd.AlphaValue
WHERE
afd.FormType = 'CUS'
AND afd.FieldName = 'COR002') a ON a.KeyField = rcd.Customer
WHERE
OrderDate >= CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()), 101)) b
GROUP BY
b.OrderDate, b.Description) c ON rtr.CorpAcctName = c.Description
我们尝试并编写了以下脚本:
以上脚本编译成postgresql(VERSION HAWQ 1.3.1)
SELECT * from tablename_1 rtr LEFT OUTER JOIN
(SELECT SUM(b."OrderValue") as OrderValue,b."OrderDate", b."Description" from
(SELECT * from tablename_2 rcd
LEFT JOIN
( SELECT Distinct afv."Item", afv."Description", afd."KeyField"
FROM tablename_2 afd
Join tablename_3 afv on afv."FormType" = afd."FormType" and afv."FieldName"=afd."FieldName" and afv."Item"=afd."AlphaValue"
Where afd."FormType" = 'CUS'and afd."FieldName" = 'COR002') a
ON a."KeyField" =rcd."Customer" where "OrderDate">=TO_CHAR((CURRENT_DATE -(CURRENT_DATE-INTERVAL '1 DAY)) ,'MM-DD-YYYY')) b
group by b."OrderDate", b."Description") c
on rtr."CorpAcctName"=c."Description"
也试过:
**
**
答案 0 :(得分:1)
我想从current_date
获得月份的第一天
所以你想要在当月开始之后创建的所有内容。
一个简单的
where "OrderDate" >= date_trunc('month', current_date)
会这样做。
有关date_trunc()
方法的详细信息,请参见手册:
http://www.postgresql.org/docs/current/static/functions-datetime.html
您应该了解您的表达式TO_CHAR((CURRENT_DATE -(CURRENT_DATE-INTERVAL '1 DAY')) ,'MM-DD-YYYY')
正在做什么,以便您以后避免该错误:
首先:current_date - interval '1 day'
从interval
,which yields a timestamp
:{昨天'在date
减去00:00:00
。
然后你从今天的日期减去它,所以current_date - timestamp '2015-11-13 00:00:00.0'
(如果今天是2015-11-14)。
这会产生interval
:0 years 0 mons 1 days 0 hours 0 mins 0.00 secs
然后将interval
传递给格式化传递间隔的to_char()
函数。由于它只有“1天”,没有年份,没有月份,因此在其上应用格式字符串'MM-DD-YYYY'
的结果确实会产生00-01-0000
。
然后,您将此字符值与真实date
进行比较 - 这也是您不应该做的事情。
你应该真正摆脱那些可怕的引用标识符。他们比值得多麻烦