我的查询如下。
SELECT T1.[AcctCode],T1.[Segment_0],SUM(T0.[DebLTotal]) AS BUDGET, SUM(T3.[Debit]) AS DEBIT,
AcctName = CASE WHEN T1.[Segment_0] LIKE '6001%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6002%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6003%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6004%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6005%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6006%%' THEN 'Operating Cost'
ELSE T1.[AcctName] END
FROM OBGT T0 INNER JOIN OACT T1 ON T0.[AcctCode]=T1.[AcctCode] INNER JOIN OBGS T2
ON T0.[Instance] = T2.[AbsId] INNER JOIN JDT1 T3 ON T1.[AcctCode] = T3.[Account] INNER JOIN OASC T4 ON T1.[Project]=T4.[Code]
where T1.[Segment_0] like '60%%'
GROUP BY T1.[AcctCode],T1.[Segment_0],T1.[AcctName]
此查询为我提供了不同的运营成本值。我需要像WHEN T1.[Segment_0] LIKE '6002%%,6003%%,6004%%' THEN 'Operating Cost'
这样的东西。可能吗?我不需要一个接一个地喜欢不同的喜欢。我需要一个人作为运营成本。请帮帮我。
答案 0 :(得分:3)
也许您的问题是您在unzip
中包含AcctName
而不是group by
的结果。也许这就是你想要的:
case
编辑:
您可能还想从SELECT T1.[AcctCode],T1.[Segment_0],SUM(T0.[DebLTotal]) AS BUDGET, SUM(T3.[Debit]) AS DEBIT,
AcctName = (CASE WHEN T1.[Segment_0] LIKE '6001%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6002%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6003%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6004%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6005%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6006%%' THEN 'Operating Cost'
ELSE T1.[AcctName] END)
FROM OBGT T0 INNER JOIN OACT
T1
ON T0.[AcctCode]=T1.[AcctCode] INNER JOIN
OBGS T2
ON T0.[Instance] = T2.[AbsId] INNER JOIN
JDT1 T3
ON T1.[AcctCode] = T3.[Account] INNER JOIN
OASC T4
ON T1.[Project] = T4.[Code]
where T1.[Segment_0] like '60%%'
GROUP BY T1.[AcctCode], T1.[Segment_0],
(CASE WHEN T1.[Segment_0] LIKE '6001%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6002%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6003%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6004%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6005%%' THEN 'Operating Cost'
WHEN T1.[Segment_0] LIKE '6006%%' THEN 'Operating Cost'
ELSE T1.[AcctName]
END);
和Segment_0
中移除select
。没有样本数据,很难说清楚。
答案 1 :(得分:2)
SQL Server LIKE可以执行pattern matching
尝试CASE WHEN T1.[Segment_0] LIKE '600[123456]%%' THEN 'Operating Cost'
答案 2 :(得分:1)
如果T1.[Segment_0]
只是数字值,您可以将此语法与between
:
case when substring(T1.[Segment_0], 1, 4) between 6001 and 6006 then 'Operating Cost' else T1.[AcctName] end
否则您可以使用IN
:
case when substring(T1.[Segment_0], 1, 4) in ('6001','6002','6003','6004','6005','6006') then 'Operating Cost' else T1.[AcctName] end