4

次のデータを含むテーブルがあります。

TradeDate   Stock   BuySell   DayClose
--------------------------------------
10-Dec-12   ABC        1         11
10-Dec-12   ABC        2         12
11-Dec-12   ABC        1         11.5
11-Dec-12   ABC        2         12.5
11-Dec-12   DEF        1         15
11-Dec-12   DEF        2         16

2012 年 12 月 11 日の特定の日付に対してクエリを実行し、次の出力を取得します。

Stock   Buy     Sell    Mid     Change
--------------------------------------
 ABC    11.5    12.5    12.0    0.5
 DEF    15      16      15.5    

DEF には前日のデータがないため、変更は空白にする必要があります。

次のクエリを作成しました。

Select Stock, 
AVG(CASE BuySell WHEN 1 THEN DayClose END) AS 'Buy', 
AVG(CASE BuySell WHEN 2 THEN DayClose END) As 'Sell', 
Sum(DayClose/2) as 'Mid', 
Sum(Change/2) AS Change

FROM (
select t1.Stock, t1.BuySell, t1.DayClose, Sum(t1.DayClose - t2.DayClose) as Change 

FROM #myTable as t1 inner join #myTable as t2 on 
t1.Stock = t2.Stock

where 
t1.TradeDate = '2012-12-11'  AND 
t2.TradeDate = (SELECT TOP 1 TradeDate FROM #myTable WHERE TradeDate < '2012-12-11' ORDER BY TradeDate DESC)  

GROUP BY 
t1.Stock, t1.buysell, t1.dayclose ) AS P1 GROUP BY stock

この目的のために一時テーブル #mytable を作成しました:

drop table #mytable
CREATE TABLE #myTable 
(
    TradeDate datetime,
    stock varchar(20),
    buysell int, 
    dayclose  decimal(10,2)
)
insert into #mytable values ('10-dec-2012', 'abc' , 1, 11)
insert into #mytable values ('10-dec-2012', 'abc' , 2, 12)
insert into #mytable values ('11-dec-2012', 'abc' , 1, 11.5)
insert into #mytable values ('11-dec-2012', 'abc' , 2, 12.5)
insert into #mytable values ('11-dec-2012', 'def' , 1, 15)
insert into #mytable values ('11-dec-2012', 'def' , 2, 16)

しかし、私は必要な出力を得ることができません。

Stock         Buy         Sell      Mid     Change
--------------------------------------------------------------
abc           11.500000 12.500000   12.00000    1.00    

誰かが私がどこで間違っているのか教えてもらえますか? 私はここで迷っているようです。

ありがとう、モニカ

4

3 に答える 3

1

してみてください:

;WITH T1 as(
SELECT   a.TradeDate
        ,a.stock
        ,SUM(CASE WHEN a.BuySell = 1 THEN a.DayClose ELSE 0 END) Buy
        ,SUM(CASE WHEN a.BuySell = 2 THEN a.DayClose ELSE 0 END) Sell
        ,SUM(a.DayClose) / 2 AS Mid
FROM    #mytable a
GROUP   BY a.TradeDate, a.stock
)SELECT  t.*,
        t.Mid - PR.Mid AS Change
FROM    T1 t
LEFT JOIN   
        T1 PR ON 
        PR.TradeDate = DATEADD(DAY, -1, t.TradeDate)
AND     PR.stock = t.stock
于 2012-12-18T11:14:34.553 に答える
1
Select Stock, 
AVG(CASE BuySell WHEN 1 THEN DayClose END) AS 'Buy', 
AVG(CASE BuySell WHEN 2 THEN DayClose END) As 'Sell', 
Sum(DayClose/2) as 'Mid', 
Sum(Change/2) AS Change

FROM (
select t1.Stock, t1.BuySell, t1.DayClose, Sum( t1.DayClose - t2.DayClose ) as Change 
FROM #myTable as t1 left join #myTable as t2 on t2.TradeDate = (SELECT TOP 1 TradeDate FROM #myTable WHERE TradeDate < t1.TradeDate ORDER BY TradeDate DESC)   
and t1.Stock = t2.Stock and t1.buysell=t2.buysell 

where 
t1.TradeDate = '11-12-2012'  
于 2012-12-18T09:40:40.817 に答える
1

これを試して:

SELECT   a.TradeDate
        ,a.stock
        ,SUM(CASE WHEN a.BuySell = 1 THEN a.DayClose ELSE 0 END) Buy
        ,SUM(CASE WHEN a.BuySell = 2 THEN a.DayClose ELSE 0 END) Sell
        ,SUM(a.DayClose) / 2 AS Mid
INTO    #temp
FROM    #mytable a
GROUP   BY a.TradeDate, a.stock


SELECT  t.*,
        t.Mid - previousRecord.Mid AS Change
FROM    #temp t
LEFT JOIN   
        #temp previousRecord ON 
        previousRecord.TradeDate = DATEADD(DAY, -1, t.TradeDate)
AND     previousRecord.stock = t.stock

DROP TABLE #temp

あとは、日付のデータを選択するだけです。

于 2012-12-18T10:06:29.143 に答える