我的任务是提出翻译以下数据的方法:
保存在 [OccuredAtUtc] 中的FROM 日期如下所示:
2016-11-19 10:35:13.5790000
2016-12-21 12:16:58.5080000
2017-01-13 12:16:58.5080000
2017-01-13 12:16:58.5080000
2017-02-13 12:16:58.5080000
2017-03-13 12:16:58.5080000
2017-04-01 12:16:58.5080000
2017-04-02 18:11:53.3090000
.
.
.
TO 过滤仅过去3个月所有格式的记录:' yyyy-MM' ... // 我不知道当月!(2017-04)
我已经写了一些代码:
SELECT
Format([OccuredAtUtc], 'yyyy-MM') AS 'Temp', -- temporary column to show, how original dates look like
CASE WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -1, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -2, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -3, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
ELSE NULL
END AS 'Time'
FROM @TABLE
WHERE ( 'Time' IS NOT NULL )
--WHERE ( [OccuredAtUtc] NOT LIKE '2017-04%' ) AND ( [OccuredAtUtc] NOT LIKE '2016-12%' ) AND ( [OccuredAtUtc] NOT LIKE '2016-11%' )
GROUP BY Format([OccuredAtUtc], 'yyyy-MM')
ORDER BY [Time] ASC
>>>OUTPUT:
__Temp__|_Time__
2016-12 | NULL
2017-04 | NULL
2016-11 | NULL
2017-01 | 2017-01
2017-02 | 2017-02
2017-03 | 2017-03
[Temp] COLUMN仅显示具有NULL
的单元格后面的数据
我需要删除所有行,[Time]列中的NULL为。正如您所看到的,代码中的WHERE条件不起作用,可能是某些问题用数据格式?我不知道为什么它不听“时间”#39;时间' :(
PS:它有效,如果我使用代号为WHERE,按照列表从[OccuredAtUtc]中手动过滤月份。但是,通过编写合适的月份列表,我无法每月更改一次查询
PS2:我不想使用任何@variable方法,因为SSRS中的数据集无法与它们很好地协同工作。一个简单的查询,用于过滤最近3个月的数据,而不是当前月份。
或者有没有办法怎么说...嘿CASE,如果ELSE发生,不要 显示这条线?
提前谢谢!!!!
答案 0 :(得分:1)
您将无法在where子句中引用“时间”列别名。
尝试使用子查询或cte。
SELECT *
FROM (
SELECT
Format([OccuredAtUtc], 'yyyy-MM') AS 'Temp', -- temporary column to show, how original dates look like
CASE WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -1, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -2, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -3, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
ELSE NULL
END AS 'Time'
FROM @TABLE
--WHERE ( 'Time' IS NOT NULL )
--WHERE ( [OccuredAtUtc] NOT LIKE '2017-04%' ) AND ( [OccuredAtUtc] NOT LIKE
'2016-12%' ) AND ( [OccuredAtUtc] NOT LIKE '2016-11%' )
GROUP BY Format([OccuredAtUtc], 'yyyy-MM'))s
WHERE Time IS NOT NULL
ORDER BY [Time] ASC
这是因为SQL查询操作顺序:
1. FROM clause
2. WHERE clause
3. GROUP BY clause
4. HAVING clause
5. SELECT clause
6. ORDER BY clause
在'SELECT'子句之前评估'WHERE'子句。
答案 1 :(得分:-2)
'Time'是一个永远不会为null的文字字符串。它的界限说'JoeC'是空的。这是因为你不能在where子句中使用'around field names。您可以使用[时间],时间或“时间”。
EDIT 您不能在where子句中使用别名。您需要重复代码或使用子查询/ CTE:
With cteQry As
(
SELECT
Format([OccuredAtUtc], 'yyyy-MM') AS Temp, -- temporary column to show, how original dates look like
CASE WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -1, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -2, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
WHEN ( Format([OccuredAtUtc], 'yyyy-MM') = Format((DATEADD(MM, -3, GETDATE())), 'yyyy-MM') ) THEN Format([OccuredAtUtc], 'yyyy-MM')
ELSE NULL
END AS Time
FROM @TABLE
)
Select Temp From cteQry
WHERE Time IS NOT NULL
GROUP BY Temp
ORDER BY [Time] ASC