2011-02-02 206 views
1

我有一個表中的201101爲2011年1月,爲201102 2011年2月的形式accounting_period等
我試圖總結本季度的一列(eff_cc)。也就是說,我想要得到1月份的數據,2011年3月的第一季度的日期等數據。SQL Case語句的where子句

因此,我在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 
+3

**什麼**數據庫,哪個版本?? – 2011-02-02 22:03:42

+0

什麼不行?你是否收到錯誤或無效的數據? – 2011-02-02 22:05:45

+0

我不明白,爲什麼你要與`GETDATE()`比較,輸出取決於今天的日期?......你不是隻想爲每個日期的排序器加總和(eff_cc)存儲在你的桌子上? – Lamak 2011-02-02 22:08:44

回答

0

試試這個(我假設它的SQL Server):

SELECT phase_code, 
     accounting_period, 
     SUM(eff_cc) OVER(PARTITION BY phase_code, yr, qt)AS bd_eff_qtd 
    FROM (SELECT a.*, 
       CAST(Substring(accounting_period, 1, 4) AS INT)  yr, 
       (CAST(Substring(accounting_period, 5, 2) AS INT) - 1)/ 3 qt 
      FROM prj_detail a) a 

如:

CREATE TABLE #prj_detail 
(
phase_code VARCHAR(10), 
accounting_period VARCHAR(10), 
eff_cc INT 
) 
INSERT INTO #prj_detail 
SELECT '1', '201101', 1 
UNION 
SELECT '1', '201102', 2 
UNION 
SELECT '1', '201103', 2 
UNION 
SELECT '1', '201104', 1 
UNION 
SELECT '1', '201105', 1 
UNION 
SELECT '1', '201106', 1 
UNION 
SELECT '1', '201107', 3 


SELECT phase_code, 
     accounting_period, 
     SUM(eff_cc) OVER(PARTITION BY phase_code, yr, qt)AS bd_eff_qtd 
    FROM (SELECT a.*, 
       CAST(Substring(accounting_period, 1, 4) AS INT)  yr, 
       (CAST(Substring(accounting_period, 5, 2) AS INT) - 1)/ 3 qt 
      FROM #prj_detail a) a 
0

這不是寫一個CASE語句的正確方法,因爲它是,它返回一個BOOLEAN,它在SQL Server中不能單獨存在。只是他們分成3個OR子句

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 
1

您可以使用CTE此:

(我還做了使用交易日期,而不是GETDATE()的所有條目的假設)

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 
1

感謝大家的提示和USEF ul響應(沒有居高臨下)

我有點根據您的意見設計了一個解決方案。實質上,我創建了一個包含相關數據的子查詢和一個新列(Qtr)。此列將評估accounting_period,併爲每行分配1,2,3或4。
然後,我裹另一個選擇解決此子查詢,用where子句比較「QTR」到當前季度(從GETDATE)

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 

也許這是低效的,但我一週的時間,這一次,所以表現不是一個大問題。 再次感謝所有。