这种情况似乎应该是一项简单的任务,但我无法提出解决方案。想象一下八列,其中四列是位列(开/关),另外四列是读数值。
我想从该表创建一个视图,其中另一列用于四个读取值的范围,基于其对应的传感器警报<>的那些值。 1 ..
出于示例目的,我们可以使用以下值。
第1栏 - 12.44
Column1Alarm - 0
第2栏 - 99.43
Column2Alarm - 0
第3栏 - 4.43
Column3Alarm - 1
第4栏 - 43.33
Column4Alarm - 0
对于此示例,范围中包含的唯一值应该是来自Column1,2和4的值。
谢谢, 汤姆
答案 0 :(得分:1)
根据@HLGM评论编辑,使其更加健壮。
请注意,在它的当前形式中,我假设
时NULL
如果这还不够,OP可能会澄清应该返回的内容。
;WITH Alarm (C1, C1Alarm, C2, C2Alarm, C3, C3Alarm, C4, C4Alarm) AS (
SELECT 12.44, 0, 99.43, 0, 4.43, 1, 43.33, 0
UNION ALL SELECT 12.44, 1, 99.43, 0, 4.43, 1, 43.33, 0
UNION ALL SELECT 1, 0, 2, 1, 3, 1, 4, 1
UNION ALL SELECT 1, 1, 2, 1, 3, 1, 4, 1
)
, AddRowNumbers AS (
SELECT rowNumber = ROW_NUMBER() OVER (ORDER BY C1)
, C1, C1Alarm
, C2, C2Alarm
, C3, C3Alarm
, C4, C4Alarm
FROM Alarm
)
, UnPivotColumns AS (
SELECT rowNumber, value = C1 FROM AddRowNumbers WHERE C1Alarm = 0
UNION ALL SELECT rowNumber, C2 FROM AddRowNumbers WHERE C2Alarm = 0
UNION ALL SELECT rowNumber, C3 FROM AddRowNumbers WHERE C3Alarm = 0
UNION ALL SELECT rowNumber, C4 FROM AddRowNumbers WHERE C4Alarm = 0
)
SELECT C1, C1Alarm
, C2, C2Alarm
, C3, C3Alarm
, C4, C4Alarm
, COALESCE(range1.range, range2.range)
FROM AddRowNumbers rowNumber
LEFT OUTER JOIN (SELECT rowNumber, range = MAX(value) - MIN(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) > 1) range1 ON range1.rowNumber = rowNumber.rowNumber
LEFT OUTER JOIN (SELECT rowNumber, range = AVG(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) = 1) range2 ON range2.rowNumber = rowNumber.rowNumber
;WITH Alarm (C1, C1Alarm, C2, C2Alarm, C3, C3Alarm, C4, C4Alarm) AS (
SELECT 12.44, 0, 99.43, 0, 4.43, 1, 43.33, 0
UNION ALL SELECT 12.44, 1, 99.43, 0, 4.43, 1, 43.33, 0
UNION ALL SELECT 1, 0, 2, 1, 3, 1, 4, 1
UNION ALL SELECT 1, 1, 2, 1, 3, 1, 4, 1
)
, AddRowNumbers AS (
SELECT rowNumber = ROW_NUMBER() OVER (ORDER BY C1)
, C1, C1Alarm
, C2, C2Alarm
, C3, C3Alarm
, C4, C4Alarm
FROM Alarm
)
, UnPivotColumns AS (
SELECT rowNumber, value = C1 FROM AddRowNumbers WHERE C1Alarm = 0
UNION ALL SELECT rowNumber, C2 FROM AddRowNumbers WHERE C2Alarm = 0
UNION ALL SELECT rowNumber, C3 FROM AddRowNumbers WHERE C3Alarm = 0
UNION ALL SELECT rowNumber, C4 FROM AddRowNumbers WHERE C4Alarm = 0
)
SELECT C1, C1Alarm
, C2, C2Alarm
, C3, C3Alarm
, C4, C4Alarm
, COALESCE(range1.range, range2.range)
FROM AddRowNumbers rowNumber
LEFT OUTER JOIN (SELECT rowNumber, range = MAX(value) - MIN(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) > 1) range1 ON range1.rowNumber = rowNumber.rowNumber
LEFT OUTER JOIN (SELECT rowNumber, range = AVG(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) = 1) range2 ON range2.rowNumber = rowNumber.rowNumber
答案 1 :(得分:1)
OP的评论“即使我有200列......”让我相信在多个地方也需要类似的功能。所以我会创建一个接受“读取”列和“报警”位的函数,并在设置警报时返回NULL。这允许人们利用MIN和MAX处理NULL值的方式。
CREATE FUNCTION UnalarmedReading
(
@Value float,
@Alarm bit
)
RETURNS float
AS
BEGIN
return case when @Alarm=1 then null else @Value end
END
以下是一些测试数据:
create table Readings (
KeyColumn int not null,
Column1 float,
Column1Alarm bit,
Column2 float,
Column2Alarm bit,
Column3 float,
Column3Alarm bit,
Column4 float,
Column4Alarm bit,
)
insert into Readings(
KeyColumn,
Column1,Column1Alarm,
Column2,Column2Alarm,
Column3,Column3Alarm,
Column4,Column4Alarm
) values (
1,
12.44, 0,
99.43, 0,
4.43, 1,
43.33, 0
)
insert into Readings(
KeyColumn,
Column1,Column1Alarm,
Column2,Column2Alarm,
Column3,Column3Alarm,
Column4,Column4Alarm
) values (
2,
124.4, 0,
994.3, 0,
44.3, 1,
433.3, 0
)
要使用MIN和MAX,请忽略:
;with NonAlarmReadings as (
select KeyColumn, dbo.UnalarmedReading(Column1, Column1Alarm) as C1,
dbo.UnalarmedReading(Column2, Column2Alarm) as C2,
dbo.UnalarmedReading(Column3, Column3Alarm) as C3,
dbo.UnalarmedReading(Column4, Column4Alarm) as C4
from Readings
),
Normalized as (
select *
from NonAlarmReadings
unpivot (Reading for BaseColumn in (C1, C2, C3, C4)) as upvt
)
select KeyColumn, min(Reading) as MinReading, max(Reading) as MaxReading,
abs(min(Reading) - max(Reading)) as ReadingRange
from Normalized
group by KeyColumn