2016-08-20 5 views
2

私は、私が興味を持っています。戻るSQLデータは

1列が日付で、2つのカラムを持つテーブルを持って、もう一方はタグ名です。

同じ日付に一致する別の列にタグ名を戻したいと思います。

サブクエリを使用してこれを実現できますが、効率的な方法がありますか?私は約20-30k行を検索して約30万件の検索結果を得ようとしています。

日付を確実に並べるための最も効率的な方法は何ですか。

これは私の質問です。

はあなたが

SELECT ah1.DateTime,ah1.Value as TPH, 
(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM1_NXG.NXG_I' 
and datetime = ah1.DateTime 
) as EM1_Current, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM2_NXG.NXG_I' 
and datetime = ah1.DateTime 
) as EM2_Current, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM1_NXG.NXG_SPEED' 
and datetime = ah1.DateTime 
) as EM1_Speed, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM2_NXG.NXG_SPEED' 
and datetime = ah1.DateTime 
) as EM2_Speed, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM1_NXG.NXG_P' 
and datetime = ah1.DateTime 
) as EM1_Power, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM2_NXG.NXG_P' 
and datetime = ah1.DateTime 
) as EM2_Power, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM1_NXG.NXG_TRQ' 
and datetime = ah1.DateTime 
) as EM1_Torque, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM2_NXG.NXG_TRQ' 
and datetime = ah1.DateTime 
) as EM2_Torque, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM1_NXG.NXG_TRQ_UTIL' 
and datetime = ah1.DateTime 
) as EM1_Torque_U, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_EM2_NXG.NXG_TRQ_UTIL' 
and datetime = ah1.DateTime 
) as EM2_Torque_U, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_TE754001G.PVAI' 
and datetime = ah1.DateTime 
) as EM1_NDE, 

(select value 
from dbo.AnalogHistory 
where tagname = 'LS_TE754001H.PVAI' 
and datetime = ah1.DateTime 
) as EM1_DE 

    FROM [Runtime].[dbo].[AnalogHistory] ah1 
    where TagName = 'CR_WQI752010.PVAI' 
    and wwResolution = '600000' 
    and DateTime > '20160816' 
    and wwRetrievalMode = 'cyclic' 

答えて

3

おそらく、あなたは条件付きの集約を使用することができますありがとうございました(各タグ名が列に並ぶようにするために、私は値を必要とします)。このような何か:

select datetime, 
     max(case when tagname = 'LS_EM1_NXG.NXG_I' then value end) as val1, 
     . . . 
from dbo.AnalogHistory 
group by datetime ; 
0

あなたは、以下のような場合に使用することができます。..

select 
    case when tagname = 'LS_EM1_NXG.NXG_I' 
    and datetime = ah1.DateTime then value end as em1 
    --do for all tags 
    from 
    table 
    FROM [Runtime].[dbo].[AnalogHistory] ah1 
    where wwResolution = '600000' 
    and DateTime > '20160816' 
    and wwRetrievalMode = 'cyclic' 
0
SELECT 
date(datetime) as d, 
(select 
     group_concat(tagname) 
    from 
     dbo.AnalogHistory 
    where 
     date(datetime) = d 
     group by date(datetime))as res 
FROM 
dbo.AnalogHistory 
group by date(datetime);