繁体   English   中英

需要帮助将此代码段从 Oracle 转换为 SQL 服务器

[英]Need help converting this code snippet from Oracle to SQL Server

select pln_products as "Products",round((sum(PLN_ANNUAL)/12),2) AS "Monthly Charges",count(pln_products) as "Count"
from test1
where pln_start_date<=(SELECT LAST_DAY(:select_date) FROM dual)
and ((pln_end_date is null) or (pln_end_date >= (SELECT TRUNC(LAST_DAY(ADD_MONTHS(:select_date,-1))+1) from dual)))
and PLN_CUSTOMER_NAME= :PLN_CUSTOMER_NAME
group by pln_products;

您不使用 SQL 中的双撇号“。您使用单撇号”。

您还必须先声明变量,以便稍后在查询中使用它们。

我使用的日期函数看起来有点复杂,但它们可以工作。

declare @PLN_CUSTOMER_NAME nvarchar(150) = ...  -- define the Variables
declare @select_date date = ...

select pln_products as 'Products'
    ,round((sum(PLN_ANNUAL)/12),2) AS 'Monthly Charges'
    ,count(pln_products) as 'Count'
from test1
where pln_start_date <= (SELECT dateadd(day, -1, dateadd(month, 1, datefromparts(year(@select_date),month(getdate()),1))) FROM dual)
    and ((pln_end_date is null) or (pln_end_date >= (SELECT dateadd(day, -1, dateadd(month, 2, datefromparts(year(@select_date),month(getdate()), 1))) from dual)))
    and PLN_CUSTOMER_NAME= @PLN_CUSTOMER_NAME
group by pln_products

希望我能帮助你... Max

暂无
暂无

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

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