我必须在财政年度的基础上设定条款 [英] I have to set a condition in where clause on the basis of financial year

查看:79
本文介绍了我必须在财政年度的基础上设定条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我如何根据财政年度设定条件



选择前25名O.OCSEmployeeCode作为UserID,OCSBillNo as BillNo,转换(varchar(10),OCSBillDate,103)为BillDate,Year(OCSBillDate)为mYear,DATEPART(mm,OCSBillDate)为mMonth 
来自HMS_OCSBill O,其中OCSCancelStatus为空并且OCSDiscontinueFlag为空且O.OCSEmployeeCode ='GAA6725'
AND(
情况下DATEPART(mm,getdate())< 4 THEN
((年(OCSBillDate)=年(getdate()) - 1和DATEPART( mm,OCSBillDate)> = 4))OR((年(OCSBillDate)=年(getdate())和DATEPART(mm,OCSBillDate)< = 3))
ELSE
((年( OCSBillDate)=年(getdate())和DATEPART(mm,OCSBillDate)> = 4))END)
年订单(OCSBillDate)DESC;





它给我的错误信息是=附近的syntex。



我尝试了什么:



请帮忙我该如何解决.. .. div class =h2_lin>解决方案

试试这:

  SELECT   TOP   25  O.OCSEmployeeCode  AS  UserID,
OCSBillNo AS BillNo,
CONVERT VARCHAR 10 ),OCSBillDate, 103 AS BillDate,
年(OCSBillDate) ) AS mYear,
DATEPART(mm,OCSBillDate) AS mMonth
FROM HMS_OCSBill O
WHERE OCSCancelStatus IS NULL
AND OCSDiscontinueFlag IS NULL
AND O.OCSEmployeeCode = ' GAA6725'
AND 1 = CASE WHEN DATEPART(mm,getdate())< 4
AND ((年(OCSBillDate)=年(GETDATE()) - 1 AND DATEPART(mm,OCSBillDate)> = 4))
OR ((年(OCSBillDate)=年(GETDATE()) AND DATEPART(mm,OCSBillDate)< = 3 ))
OR ((年(OCSBillDate)=年(GETDATE()) AND DATEPART (mm,OCSBillDate)> = 4))
那么 1
< span class =code-keyword> ELSE
0
END
ORDER BY 年(OCSBillDate) DESC ;


即使没有您的问题,您的查询构造也很糟糕,并且会在存储了几年历史的生产服务器上显示。

您正在使用 getdate()子句时,问题是该函数是易失性的,它的值会随着你的调用而改变。

这意味着服务器无法优化过滤器,工作量取决于表中的记录数。您正在屠杀日期字段的事实是不优化的另一个原因,因为它超出了服务器的理解。



另一种方法是计算你希望之前的 选择的StartDate和EndDate记录。

然后:

  时OCSBillDate    @ StartDate    @ EndDate  



这样, SQL服务器知道如何利用索引,工作负载将取决于与约束匹配的记录数。


please help how can i set a condition on the basis of financial year

Select top 25 O.OCSEmployeeCode as UserID, OCSBillNo as BillNo, convert(varchar(10),OCSBillDate,103) as BillDate, Year(OCSBillDate) as mYear, DATEPART(mm,OCSBillDate) as mMonth
from  HMS_OCSBill O where OCSCancelStatus Is Null And OCSDiscontinueFlag Is Null and O.OCSEmployeeCode='GAA6725' 
AND (
CASE WHEN DATEPART(mm,getdate())<4 THEN 
((Year(OCSBillDate)=year(getdate())-1 AND DATEPART(mm,OCSBillDate)>=4)) OR ((Year(OCSBillDate)=year(getdate()) AND DATEPART(mm,OCSBillDate)<=3))
ELSE
((Year(OCSBillDate)=year(getdate()) AND DATEPART(mm,OCSBillDate)>=4)) END)
 ORDER BY Year(OCSBillDate) DESC;



Its giving me error message that incorect syntex near "=".

What I have tried:

Please help how can i resolve it..

解决方案

Try this:

SELECT TOP 25 O.OCSEmployeeCode AS UserID, 
       OCSBillNo AS BillNo, 
       CONVERT(VARCHAR(10),OCSBillDate,103) AS BillDate, 
       YEAR(OCSBillDate) AS mYear, 
       DATEPART(mm,OCSBillDate) AS mMonth
FROM  HMS_OCSBill O 
WHERE OCSCancelStatus IS NULL 
  AND OCSDiscontinueFlag IS NULL 
  AND O.OCSEmployeeCode='GAA6725' 
  AND 1 = CASE WHEN DATEPART(mm,getdate())<4 
                AND ((YEAR(OCSBillDate)=YEAR(GETDATE())-1 AND DATEPART(mm,OCSBillDate)>=4)) 
                OR  ((YEAR(OCSBillDate)=YEAR(GETDATE()) AND DATEPART(mm,OCSBillDate)<=3)) 
                OR  ((YEAR(OCSBillDate)=YEAR(GETDATE()) AND DATEPART(mm,OCSBillDate)>=4)) 
               THEN 1
               ELSE 0
          END
ORDER BY YEAR(OCSBillDate) DESC;


Even without your problem, your query is badly constructed and will be slooow on a production server storing a few years of history.
You are using getdate() in the when clause, the problem is that the function is volatile, its value changes any time you call it.
This mean that the server can't optimize the filter and the workload depend on the number of records in the table. The fact that you are butchering date fields is another reason to not optimize, because it is beyond the understanding of the server.

The other way to do it is to compute a StartDate and EndDate of records you want before the select.
And then :

when OCSBillDate between @StartDate and @EndDate


This way, the SQL server knows how to take advantage of an index, and the workload will depend on the number of records matching the constraint.


这篇关于我必须在财政年度的基础上设定条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆