[英]How to use the average function with multiple conditional statements using SQL in Visual Studio SSRS 2008
[英]How to use multiple conditional statements using SQL in Visual Studio SSRS 2008
我試圖使用Visual Studio SSRS 2008從SQL數據庫中提取計算的數據。嘗試在select / from / where語句中使用條件條件時遇到問題。 我要嘗試執行的一個示例是從每15分鍾抽取的樣本中找出24小時內的故障總數(從昨天的10:00 am到今天的10:00 am)。 我正在報告一家天然氣廠,當閥門打開時,天然氣會流經閥門。 穿過特定打開閥的氣體在燃燒室中被點燃。 發生這種情況時,如果閥門打開並且燃燒室中的溫度低於1400度,則會發生故障。 我目前有一個查詢,每15分鍾會給我有關閥狀態和燃燒室溫度的數據。 如我所說,我只關心閥門的狀態為2(打開),並且燃燒室溫度同時小於1400。 我已經在下面復制了我當前的查詢以及結果數據的樣本。 如您所見,當前查詢不包含我的必要條件。 我需要添加看起來像這樣的代碼:
COUNT (IF RTO_FCV601_SSTS=2 AND RTO_COMB1TEMP < 1400)
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(HOUR,-24,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
SELECT temp.TagName ,DateTime,Value
From (
SELECT *
FROM History
WHERE History.TagName IN ('RTO_FCV601_STS','RTO_COMB1TEMP')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 96
AND wwVersion = 'Latest'
AND DateTime >= @StartDate
AND DateTime <= @EndDate) temp
WHERE temp.StartDateTime >= @StartDate
Resulting data:
TagName DateTime Value
RTO_FCV601_STS 2013-04-08 15:44:40.6600000 2
RTO_COMB1TEMP 2013-04-08 15:44:40.6600000 1663
RTO_FCV601_STS 2013-04-08 15:59:50.1340000 2
RTO_COMB1TEMP 2013-04-08 15:59:50.1340000 1604
RTO_FCV601_STS 2013-04-08 16:14:59.6080000 2
RTO_COMB1TEMP 2013-04-08 16:14:59.6080000 1557
RTO_FCV601_STS 2013-04-08 16:30:09.0810000 0
RTO_COMB1TEMP 2013-04-08 16:30:09.0810000 1704
RTO_FCV601_STS 2013-04-08 16:45:18.5550000 2
RTO_COMB1TEMP 2013-04-08 16:45:18.5550000 1576
RTO_FCV601_STS 2013-04-08 17:00:28.0290000 2
RTO_COMB1TEMP 2013-04-08 17:00:28.0290000 1645
如果無法在SQL中解決此問題,希望我可以在Visual Studio SSRS中找到一種方法。 先謝謝您的幫助!
在我的想法中,並且不看您的表。...我說,如果僅針對這兩個州,您的DateTime是唯一的,盡管我建議您(也是我的想法),但嘗試在下面的Datetime進行連接有點不合邏輯...您可以自己優化它...但是無論如何您都可以使用“ case when”語句
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(HOUR,-24,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
SELECT Sum(case when temp.Value = 2 and temp2.value< 1400 then 1 else 0 end )
AS 'failureCount'
From (
SELECT *
FROM History
WHERE History.TagName IN ('RTO_FCV601_STS')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 96
AND wwVersion = 'Latest'
AND DateTime >= @StartDate
AND DateTime <= @EndDate)as temp
join
(SELECT * FROM History
WHERE History.TagName IN ('RTO_COMB1TEMP')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 96
AND wwVersion = 'Latest'
AND DateTime >= @StartDate
AND DateTime <= @EndDate)as temp2 on temp.DateTime = temp2.DateTime
WHERE temp.StartDateTime >= @StartDate and temp2.StartDateTime >= @StartDate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.