Union Query?
- From: Mark J <MarkJ@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Nov 2006 12:56:02 -0800
If anyone could help with this i would greatly appreciate it.
i have a database that has 5000 tags and i need to have a query return the
following result.
Tag Time Value
A1 16:34 7.23
A1 23:51 1.32
A2 13:12 0.67
A2 22:14 2.89
The following query is for only one tag to give max/min and thier timestamp,
how do i code for the above result?
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(dd,-1,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
(SELECT Top 1 TagName, CONVERT(varchar(15), DateTime, 108) as [Time], value
= convert(decimal(38, 3), Value)
FROM v_AnalogHistory
WHERE TagName IN ('A1')
AND wwVersion = 'Latest'
AND wwRetrievalMode = 'Cyclic'
AND wwRowCount = 1000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
and (value = (SELECT min(value) as value
FROM v_AnalogHistory
WHERE TagName IN ('A1')
AND wwVersion = 'Latest'
AND wwRetrievalMode = 'Cyclic'
AND wwRowCount = 1000
AND DateTime >= @StartDate
AND DateTime <= @EndDate)))
union
(SELECT Top 1 TagName, CONVERT(varchar(15), DateTime, 108) as [Time], value
= convert(decimal(38, 3), Value)
FROM v_AnalogHistory
WHERE TagName IN ('A1')
AND wwVersion = 'Latest'
AND wwRetrievalMode = 'Cyclic'
AND wwRowCount = 1000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
and (value = (SELECT max(value) as value
FROM v_AnalogHistory
WHERE TagName IN ('A1')
AND wwVersion = 'Latest'
AND wwRetrievalMode = 'Cyclic'
AND wwRowCount = 1000
AND DateTime >= @StartDate
AND DateTime <= @EndDate)))
.
- Prev by Date: RE: newest data mining techniques?
- Next by Date: Re: Forecasting Model Viewer - Help
- Previous by thread: RE: newest data mining techniques?
- Next by thread: Re: Forecasting Model Viewer - Help
- Index(es):
Relevant Pages
|