Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Where子句中的SQL Case语句_Sql_Sql Server_Case_Where - Fatal编程技术网

Where子句中的SQL Case语句

Where子句中的SQL Case语句,sql,sql-server,case,where,Sql,Sql Server,Case,Where,我有一个会计期间表,格式为2011年1月的201101,2011年2月的201102,等等。 我正在尝试对本季度的列(eff_cc)求和。也就是说,我想得到2011年1月、2月和3月第一季度日期的数据总和,等等 所以,我在where子句中使用了Case语句。基本上我(在where条款中)说过: 如果当前月份为1、4、7或10,则获取该月份的数据 如果当前月份为2、5、8或11,则从当前月份和上个月获取数据;及 如果当前月份为3、6、9或12,则从当前月份和前两个月获取数据 不想工作。代码如

我有一个会计期间表,格式为2011年1月的201101,2011年2月的201102,等等。
我正在尝试对本季度的列(eff_cc)求和。也就是说,我想得到2011年1月、2月和3月第一季度日期的数据总和,等等

所以,我在where子句中使用了Case语句。基本上我(在where条款中)说过:

  • 如果当前月份为1、4、7或10,则获取该月份的数据
  • 如果当前月份为2、5、8或11,则从当前月份和上个月获取数据;及
  • 如果当前月份为3、6、9或12,则从当前月份和前两个月获取数据
不想工作。代码如下

select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD, 
from prj_detail
where 
    case month(getdate()) % 3
        when  1 then    -- current month is 1,4,7,10
            accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2)
        when 2 then     -- current month is 2, 5, 8, 11
            (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
            accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2))
        when 3 then     -- current month is 3, 6, 9, 12
            (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
            accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2) or
            accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-2),2))
    end
group by phase_code, accounting_period
试试这个(我假设它是SQL Server):

e、 g:


这不是编写CASE语句的正确方法,因为它返回的是一个布尔值,而在sqlserver中,布尔值不能单独存在。把它们分成3个或多个子句

select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
from prj_detail
where 
(    month(getdate()) % 3 = 1 AND -- current month is 1,4,7,10
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2))
    OR
(    month(getdate()) % 3 = 2 AND -- current month is 2, 5, 8, 11
    (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2)))
    OR
(    month(getdate()) % 3 = 2 AND -- current month is 3, 6, 9, 12
    (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2) or
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-2),2)))
group by phase_code, accounting_period

您可以为此使用CTE:

(我还假设对所有条目使用事务日期而不是getdate())

结果,在插入行几次后:

phase_code  accounting_period   BD_Eff_QTD
b   200807  12000
c   200901  1000
b   201001  4200
a   201004  15600
a   201101  13200
c   201101  11000

感谢你们所有人的及时和有用的回复(不要屈尊)

我根据你的意见设计了一个解决方案。本质上,我创建了一个子查询、相关数据和一个新列(Qtr)。此列评估会计期间,并为每行指定1、2、3或4。
然后,我围绕这个子查询包装了另一个select,并使用where子句将“Qtr”与当前季度(从getDate开始)进行比较

选择相位码,求和(BD_Eff_QTD)作为BD_Eff_QTD
从…起
(
选择阶段代码、会计期间、总和(效率pc)作为BD\U效率QTD,
“Qtr”=
案例

当cast(substring(convert(varchar,accounting_period),5,2)为int)时,哪个数据库,哪个版本?什么不起作用?您是否得到错误或无效数据?我不明白,为什么您要与
GETDATE()
?进行比较,输出取决于今天的日期?…难道您不想
sum(eff_cc)
对于存储在表中的每个日期查询,错误是“附近的语法不正确=”。基本上,我只是想得到季度的最新信息,而不是每个季度的信息。@R Dev-那么如果是2月,你想结果中有两个月,1月、2月各一个月吗?我也想到过类似的事情,但OP回答说他想要季度的是最新信息,而不是每个季度。@Lamak-在重新阅读了一段时间后,它点击了我正在寻找当前季度的季度最新数据。你能发布DDl和样本数据吗?不知道DDI是什么。样本数据:阶段代码,会计期间,有效期cc-------------------------AAMC 201101 25.32 AAMC 201102 13.35 AAMC 201104 15.65 ATT 201101 25.00因此,对于上述数据集,结果应为:AAMC 38.67(=25.32+13.35),因为我们在第一季度ATT 25.00
select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
from prj_detail
where 
(    month(getdate()) % 3 = 1 AND -- current month is 1,4,7,10
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2))
    OR
(    month(getdate()) % 3 = 2 AND -- current month is 2, 5, 8, 11
    (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2)))
    OR
(    month(getdate()) % 3 = 2 AND -- current month is 3, 6, 9, 12
    (accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or 
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2) or
    accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-2),2)))
group by phase_code, accounting_period
CREATE TABLE prj_detail
(phase_code VARCHAR(10)
, transaction_date DATETIME
, eff_cc INT)

INSERT INTO prj_detail
SELECT 'c',GETDATE(),11000
UNION ALL SELECT 'a',GETDATE(),1100
UNION ALL SELECT 'b','01/01/2010',2100
UNION ALL SELECT 'c','01/01/2009',500
UNION ALL SELECT 'a','05/01/2010',7800
UNION ALL SELECT 'b','07/01/2008',6000


WITH PhaseCode (phase_code, accounting_period, eff_cc)

AS 

(SELECT phase_code
,  case month(transaction_date) % 3
        when 1 then    -- current month is 1,4,7,10
            right(Year(transaction_date),4) + Right('0' + rtrim(month(transaction_date)),2)
        when 2 then     -- current month is 2, 5, 8, 11
            right(Year(transaction_date),4) + Right('0' + rtrim(month(transaction_date)-1),2)
        when 3 then     -- current month is 3, 6, 9, 12
            right(Year(transaction_date),4) + Right('0' + rtrim(month(transaction_date)-2),2)
    END accounting_period
, eff_cc
from prj_detail)

SELECT phase_code, accounting_period, SUM(eff_cc) AS BD_Eff_QTD
FROM PhaseCode
GROUP BY phase_code, accounting_period
phase_code  accounting_period   BD_Eff_QTD
b   200807  12000
c   200901  1000
b   201001  4200
a   201004  15600
a   201101  13200
c   201101  11000
select phase_code, sum(BD_Eff_QTD) as BD_Eff_QTD
from 
(
select phase_code, accounting_period, sum(eff_pc) as BD_Eff_QTD,
'Qtr' = 
case
when cast (substring(convert(varchar, accounting_period),5,2) as int) <= 3 then 1
when cast (substring(convert(varchar, accounting_period),5,2) as int) <= 6 then 2
when cast (substring(convert(varchar, accounting_period),5,2) as int) <=9 then 3
else 4
end
from prj_detail
group by phase_code, accounting_period
) X
where CurQtr = datepart(qq,getDate()) 
group by phase_code