从Jaspersoft Studio JDBC到Sql Server的日期不喜欢

时间:2018-12-01 01:24:45

标签: sql sql-server jasper-reports

我尝试使用以下脚本的服务器差异,以尝试将日期从sql-server拉入没有运气的jasper report studio项目中。以下内容未产生任何错误,但发出警告:“日期未被识别为内置函数名称。是否仍要继续?”我相信问题与jdbc如何处理Dates与Sql Server的处理方式有关,但我不确定如何前进。我该怎么做才能解决此问题。

    SELECT * FROM 
    (SELECT '' AS id3, ISNULL(ROUND(SUM(amount), 0), 0) AS amount_3, DATE('now','-3 month')  AS Date_3 FROM Sales WHERE date('YEAR', purchase_date) =  date('YEAR', Date_3)  AND DATE('MONTH', purchase_date) = date('MONTH', Date_3)) C
    LEFT JOIN
    (SELECT '' AS id2, ISNULL(ROUND(SUM(amount), 0), 0) AS amount_2, DATE('now','-2 month')  AS Date_2 FROM Sales WHERE date('YEAR', purchase_date) =  date('YEAR', Date_2)  AND DATE('MONTH', purchase_date) = date('MONTH', Date_2)) B
     ON id3 = id2 LEFT JOIN
    (SELECT '' AS id1, ISNULL(ROUND(SUM(amount), 0), 0) AS amount_1, DATE('now','-1 month')  AS Date_1 FROM Sales WHERE date('YEAR', purchase_date) =  date('YEAR', Date_1)  AND DATE('MONTH', purchase_date) = date('MONTH', Date_1)) A
     ON id2 = id1

My previous attempt, which worked well in SSMS, looked something like the following:

 SELECT * FROM 
(SELECT '' AS id3, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_3, DATEADD(month, -3, getdate()) AS Date_3 FROM SALES  GROUP BY purchase_date HAVING YEAR(purchase_date) = YEAR(DATEADD(month, 3, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -3, getdate())) C
LEFT JOIN
(SELECT '' AS id2, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_2, DATEADD(month, -2, getdate()) AS Date_2 FROM SALES  GROUP BY purchase_date HAVING YEAR(purchase_date) = YEAR(DATEADD(month, -2, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -2, getdate())) B
ON id3 = id2 LEFT JOIN
(SELECT '' AS id1, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_1, DATEADD(month, -1, getdate()) AS Date_1 FROM SALES  GROUP BY purchase_date HAVING YEAR(purchase_date) = YEAR(DATEADD(month, -1, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -1, getdate())) A
 ON id2 = id1

1 个答案:

答案 0 :(得分:0)

当JDBC说代码有错误时,它最终说谎。以下代码确实有效。

 SELECT * FROM 
(SELECT '' AS id3, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_3, DATEADD(month, -3, getdate()) AS Date_3 FROM SALES  WHERE YEAR(purchase_date) = YEAR(DATEADD(month, 3, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -3, getdate())) C
LEFT JOIN
(SELECT '' AS id2, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_2, DATEADD(month, -2, getdate()) AS Date_2 FROM SALES  WHERE YEAR(purchase_date) = YEAR(DATEADD(month, -2, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -2, getdate())) B
ON id3 = id2 LEFT JOIN
(SELECT '' AS id1, ISNULL(ROUND(SUM(amount),0),0) FORMAT(purchase_date, 'MMM yy') AS Date_db_1, DATEADD(month, -1, getdate()) AS Date_1 FROM SALES  WHERE YEAR(purchase_date) = YEAR(DATEADD(month, -1, getdate())) AND MONTH(purchase_date) = MONTH(DATEADD(month, -1, getdate())) A
 ON id2 = id1