现在,我有一个看起来像这样的观点:
CREATE VIEW [dbo].[vwContaminationHistory]
AS
SELECT
ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) AS DetectorID,
MIN(TIMESTAMP) AS TIMESTAMP,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM
ContaminationHistory
GROUP BY
DetectorID, ScannerType,
YEAR(TIMESTAMP), MONTH(TIMESTAMP), DAY(TIMESTAMP),
DATEPART(HOUR, TIMESTAMP)
GO
这将返回以下行:
Row DetectorID TimeStamp ScannerType Pollution VFactor
---------------------------------------------------------------------------------
2 948 2017-03-17 12:44:34.0000000 +01:00 hbd1 0 99
3 948 2017-03-17 13:01:49.0000000 +01:00 hbd1 0 99
4 948 2017-03-17 14:04:29.0000000 +01:00 hbd1 0 99
5 948 2017-03-17 12:44:34.0000000 +01:00 hbd2 0 99
6 948 2017-03-17 13:01:49.0000000 +01:00 hbd2 0 99
每新的小时返回1行。
现在,列Row
为Not Null
。这很好。但是我希望列TimeStamp
具有相同的功能。
我试图做的是:
CREATE VIEW [dbo].[vwContaminationHistorydateee]
AS
SELECT
ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) AS DetectorID,
ISNULL(MIN(TIMESTAMP) OVER (ORDER BY TIMESTAMP), '1900-01-01 12:00:00.0000000 +00:00') AS TIMESTAMP,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM
ContaminationHistory
GROUP BY
DetectorID, ScannerType,
YEAR(TIMESTAMP), MONTH(TIMESTAMP), DAY(TIMESTAMP),
DATEPART(HOUR, TIMESTAMP)
GO
但是,当我执行此操作时,我收到错误:
Msg 8120,Level 16,State 1,Procedure vwContaminationHistorydateeee, 15号线 列'ContaminationHistory.TimeStamp'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
当我删除年,月,日和小时组时,用TimeStamp替换它们。视图有效。但是它没有按时间戳分组,它会给我每小时15个结果。虽然我想要1。
如何将TimeStamp
列转换为NOT NULL
并保留group by子句,因为我现在拥有它?
更新
我将视图更改为:
CREATE VIEW [dbo].[vwContaminationHistorydate2]
AS
SELECT
ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) as DetectorID,
ISNULL(MIN(TimeStamp) OVER (ORDER BY TimeStamp), '1900-01-01 12:00:00.0000000 +00:00') as TimeStamp,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM
ContaminationHistory
GROUP BY
DetectorID, ScannerType,
YEAR(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
MONTH(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DAY(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DATEPART(HOUR, ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00'))
GO
仍然得到错误:
Msg 8120,Level 16,State 1,Procedure vwContaminationHistorydate2, 第14行 列'ContaminationHistory.TimeStamp'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
当我运行此视图时:
CREATE VIEW [dbo].[vwContaminationHistory2]
AS
SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) AS DetectorID,
MIN(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')) AS TIMESTAMP,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM ContaminationHistory
GROUP BY DetectorID,
ScannerType,
YEAR(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
MONTH(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DAY(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DATEPART(HOUR, ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00'))
GO
结果是正确的。但是,TimeStamp行设置为null,我希望它不是Null。
答案 0 :(得分:1)
试试这个并希望它适合你
CREATE VIEW [dbo].[vwContaminationHistory]
AS
SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) AS DetectorID,
ISNULL(MIN(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),'1900-01-01 12:00:00.0000000 +00:00') AS TIMESTAMP,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM ContaminationHistory
GROUP BY DetectorID,
ScannerType,
YEAR(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
MONTH(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DAY(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DATEPART(HOUR, ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00'))
GO
或者你可以这样做
SELECT Row,
DetectorID,
ISNULL(TIMESTAMP, '1900-01-01 12:00:00.0000000 +00:00') AS TIMESTAMP,
ScannerType,
Pollution,
VFactor
FROM [dbo].[vwContaminationHistory]
示例运行 -
;WITH cte_SampleData(Row,DetectorID,TimeStamp,ScannerType,Pollution,VFactor) AS
(
SELECT 2,948,'2017-03-17 12:44:34.0000000 +01:00','hbd1',0,99 UNION ALL
SELECT 3,948,'2017-03-17 13:01:49.0000000 +01:00','hbd1',0,99 UNION ALL
SELECT 4,948,'2017-03-17 14:04:29.0000000 +01:00','hbd1',0,99 UNION ALL
SELECT 5,948,'2017-03-17 12:44:34.0000000 +01:00','hbd2',0,99 UNION ALL
SELECT 5,948,NULL,'hbd2',0,99 UNION ALL
SELECT 6,948,'2017-03-17 13:01:49.0000000 +01:00','hbd2',0,99
)
--SELECT * FROM cte_SampleData
SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
MAX(DetectorID) AS DetectorID,
ISNULL(MIN(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),'1900-01-01 12:00:00.0000000 +00:00') AS TIMESTAMP,
MAX(ScannerType) AS ScannerType,
MAX(Pollution) AS Pollution,
MAX(VFactor) AS VFactor
FROM cte_SampleData
GROUP BY DetectorID,
ScannerType,
YEAR(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
MONTH(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DAY(ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00')),
DATEPART(HOUR, ISNULL(TIMESTAMP,'1900-01-01 12:00:00.0000000 +00:00'))
答案 1 :(得分:0)
首先:
ISNULL(ROW_NUMBER() OVER (ORDER BY DetectorID), 9999) AS Row,
非常奇怪。 Row_Number总是返回一些值,所以它永远不会为null。如果你想确定当DetectorId为NULL时你应该写:
ROW_NUMBER() OVER (ORDER BY ISNULL (DetectorID, 9999)) AS Row,
但是你需要记住所有NULL的DetectorId将是最后一行。
其次: 我会用子查询来做,有些像这样:
DECLARE @Sample Table
(
DetectorId Int, TimeStamp DateTime, ScannerType Varchar (50), Pollution Int, VFactor Int
)
Insert Into @Sample Values
(1, '2017-03-17 12:44:34', 'aaa', 10, 1220)
,(1, '2017-03-17 12:44:34', 'aaa', 43, 1520)
,(1, '2017-03-17 12:44:34', 'aaa', 12, 7220)
,(1, NULL, 'aaa', 10, 120)
,(1, '2017-03-17 12:51:34', 'aaa', 12, 500)
,(1, '2017-03-17 12:57:34', 'aaa', 14, 1000)
,(1, '2017-03-17 13:44:34', 'aaa', 10, 120)
,(1, '2017-03-17 14:44:34', 'aaa', 10, 120)
,(NULL, '2017-03-17 14:54:34', 'aaa', 10, 120)
--SELECT *
--FROM @Sample
SELECT
ROW_NUMBER () OVER (ORDER BY ISNULL (X.DetectorID, 9999)) AS Row
,Max (X.DetectorID) As DetectorId
--,X.TimeStampRowNumber
,X.TimeStamp As TimeStamp
,Max (X.ScannerType) As ScannerType
,Max (X.Pollution) As Pollution
,Max (X.VFactor) As VFactor
FROM
(
SELECT
DetectorID AS DetectorID,
RANK ()
OVER
(
PARTITION BY
DATEPART (YEAR, TimeStamp)
,DATEPART (MONTH, TimeStamp)
,DATEPART (DAY, TimeStamp)
,DATEPART (HOUR, TimeStamp)
ORDER BY
ISNULL (TimeStamp, DATEFROMPARTS (1990, 01, 01))
) AS TimeStampRowNumber,
ISNULL (TimeStamp, DATEFROMPARTS (1990, 01, 01)) As TimeStamp,
ScannerType AS ScannerType,
Pollution AS Pollution,
VFactor AS VFactor
FROM
@Sample --ContaminationHistory
) AS X
WHERE X.TimeStampRowNumber = 1
Group By
X.DetectorID, X.TimeStamp
ORDER BY 1
所以..首先你需要制作一个额外的列,它给你每小时的所有第一次测量,然后添加条件。 我希望我明白你想得到什么样的结果。