简体   繁体   中英

SQL number greater than select results

I'm struggling to think of a way to do this with T-SQL.

I have a table which is populated every 5 seconds with the prices of three currencies (GBP, EUR & USD)

I've created a trigger (after insert), which selects the last 5 records entered for a given currency:

SELECT TOP 5 Price from dbo.prices where coin='GBP' ORDER BY Date Desc

I want to determine if the last inserted currency price is greater than the selected 5 above, how do i do this?

Thanks

As I guess: there cant be two entries for the same currency at one time. Only one insert per currency per some time (5sec). So this should fit yours requirements:

declare @prices  table ([Date] int IDENTITY(1,1) primary key, Price float, coin varchar(3));  
insert into @prices  (coin, Price) values 
('GBP', 3.20),('EUR', 3.14),('USD', 3.14),
('GBP', 3.17),('EUR', 3.16),('USD', 3.11),
('GBP', 3.14),('EUR', 3.13),('USD', 3.16),
('GBP', 3.15),('EUR', 3.12),('USD', 3.17),
('GBP', 3.16),('EUR', 3.17),('USD', 3.11),
('GBP', 3.15),('EUR', 3.14),('USD', 3.12),
('GBP', 3.19),('EUR', 3.14),('USD', 3.16)

select
    case
        when NEW.Price > PREV.Price Then 'yes'
        else 'No'
    end as CURR_JUMP_UP
from
    (
        select top 1 COALESCE(Price,0) Price, [Date]
        from @prices where coin='GBP' order by [Date] desc
    ) NEW
    cross apply
    (
        select MAX(Price) Price from
        (
            select top 5 Price
            from @prices
            where coin='GBP' and [Date]<NEW.[Date]
            order by [Date] desc
        ) t
    ) PREV

Try this query:

DECLARE @AmountLastFiveEntry DECIMAL= (SELECT TOP 5 SUM(Price) FROM dbo.prices WHERE 
    ID NOT IN (SELECT TOP 1 ID 
    FROM dbo.prices where coin='GBP' ORDER BY Date Desc) where coin='GBP' ORDER BY Date Desc)
     IF  @AmountLastFiveEntry<(SELECT TOP 1 Price 
    FROM dbo.prices where coin='GBP' ORDER BY Date Desc)
    BEGIN
    SELECT @AmountLastFiveEntry --To do task
    END

Trigger part is confusing

This will report if the latest price is higher (or equal) to the largest of the prior 5.

declare @currency table (iden int IDENTITY(1,1) primary key, exchange smallint,  coin tinyint);  
insert into @currency (coin, exchange) values 
       (1, 1)
     , (1, 2)
     , (1, 3)
     , (1, 4)
     , (1, 5)
     , (1, 6)
     , (2, 1)
     , (2, 2)
     , (2, 3)
     , (2, 4)
     , (2, 5)
     , (2, 3);

select cccc.coin, cccc.exchange 
     , case when cccc.rn = cccc.rne then 'yes'
            else 'no'
       end  as 'high'
from ( select ccc.iden, ccc.coin, ccc.exchange, ccc.rn 
            , ROW_NUMBER() over (partition by ccc.coin order by ccc.exchange desc, ccc.rn) rne 
       from ( select cc.iden, cc.coin, cc.exchange, cc.rn  
              from ( select c.iden, c.coin, c.exchange 
                          , ROW_NUMBER() over (partition by coin order by iden desc) as rn
                     from @currency c
                   ) cc 
              where cc.rn <= 6 
            ) ccc
     ) cccc
where cccc.rn = 1
order by cccc.coin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM