Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在周末或假日加入最近的货币兑换日?_Sql_Tsql_Join_Greatest N Per Group - Fatal编程技术网

Sql 如何在周末或假日加入最近的货币兑换日?

Sql 如何在周末或假日加入最近的货币兑换日?,sql,tsql,join,greatest-n-per-group,Sql,Tsql,Join,Greatest N Per Group,我有一张不同货币的交易表,其中一些日期是在周末或假日。例如: Date currency_code ammount 20-02-2017 EUR 55 18-02-2017 GBP 33 17-02-2017 EUR 44.55 我的Currency表的示例如下: SELECT rate,date, currency_code FROM exchangeTable: rate Date

我有一张不同货币的交易表,其中一些日期是在周末或假日。例如:

Date       currency_code    ammount
20-02-2017 EUR              55
18-02-2017 GBP              33
17-02-2017 EUR              44.55
我的
Currency
表的示例如下:

SELECT rate,date, currency_code FROM exchangeTable:

rate      Date          currency_code
53,35     13-02-2017    ADP   
53,35     14-02-2017    ADP    
182,4     16-02-2017    ADP    
192,45    17-02-2017    ADP    
191,31    20-02-2017    ADP   
是否有一个简单的子查询可以在join语句中使用,如果是在周末或假日,它会将最近的货币日期连接到我的交易日期?我想我应该在这里使用分区,但我没有太多的经验

left join (?????????) a on a.date = b.date and a.currency_code= b.currency_code

您可以使用派生表、
ROW\u NUMBER
和分区来解决此问题。这样做的目的是使用带有
dw
(星期几)参数的
DATEPART
函数来忽略任何周六和周日,从而消除周末。对于假期,你必须有一个假期日期表,因为假期完全是主观的

ROW_NUMBER
允许您在给定自定义顺序和分区的情况下获取行号索引。我们按货币进行分区,因此每当我们使用新货币时,索引都会重置,并且我们按
DATE DESC
排序,因此每种货币的最近日期为
1

-- create a sample table with the date, currency, and exchange rate
create table rates (
    id int identity(1,1) primary key,
    date date not null,
    currency char(3) not null,
    rate decimal(10,2) not null
)
go

-- create table of holidays we'll use for excluding rates records later
create table holidays (
    id int identity(1, 1) primary key,
    date date not null,
    name varchar(100) not null
)

-- create some sample data
-- Feb 18 and 19 are Saturday and Sunday
insert into rates (date, currency, rate) values
('2017-02-16', 'GBP', 1.23),
('2017-02-17', 'GBP', 1.24),
('2017-02-18', 'GBP', 1.25),
('2017-02-19', 'GBP', 1.26),
('2017-02-20', 'GBP', 1.27),
('2017-02-16', 'SGD', 2.23),
('2017-02-17', 'SGD', 2.24),
('2017-02-18', 'SGD', 2.25),
('2017-02-19', 'SGD', 2.26),
('2017-02-20', 'SGD', 2.27);

insert into holidays (date, name) values
('2017-02-20', 'National Cherry Pie Day'); -- this is a real thing


with t as (
    select id,
           date,
           currency,
           rate,
           row_number() over (partition by currency order by date desc) as age
    from   rates
    where  datepart(dw, date) not in (1, 7) -- sunday, saturday
           and date not in (select date from holidays) -- exclude holiday rates
)
select * from t where age = 1;

您可以使用派生表、
ROW\u NUMBER
和分区来解决此问题。这样做的目的是使用带有
dw
(星期几)参数的
DATEPART
函数来忽略任何周六和周日,从而消除周末。对于假期,你必须有一个假期日期表,因为假期完全是主观的

ROW_NUMBER
允许您在给定自定义顺序和分区的情况下获取行号索引。我们按货币进行分区,因此每当我们使用新货币时,索引都会重置,并且我们按
DATE DESC
排序,因此每种货币的最近日期为
1

-- create a sample table with the date, currency, and exchange rate
create table rates (
    id int identity(1,1) primary key,
    date date not null,
    currency char(3) not null,
    rate decimal(10,2) not null
)
go

-- create table of holidays we'll use for excluding rates records later
create table holidays (
    id int identity(1, 1) primary key,
    date date not null,
    name varchar(100) not null
)

-- create some sample data
-- Feb 18 and 19 are Saturday and Sunday
insert into rates (date, currency, rate) values
('2017-02-16', 'GBP', 1.23),
('2017-02-17', 'GBP', 1.24),
('2017-02-18', 'GBP', 1.25),
('2017-02-19', 'GBP', 1.26),
('2017-02-20', 'GBP', 1.27),
('2017-02-16', 'SGD', 2.23),
('2017-02-17', 'SGD', 2.24),
('2017-02-18', 'SGD', 2.25),
('2017-02-19', 'SGD', 2.26),
('2017-02-20', 'SGD', 2.27);

insert into holidays (date, name) values
('2017-02-20', 'National Cherry Pie Day'); -- this is a real thing


with t as (
    select id,
           date,
           currency,
           rate,
           row_number() over (partition by currency order by date desc) as age
    from   rates
    where  datepart(dw, date) not in (1, 7) -- sunday, saturday
           and date not in (select date from holidays) -- exclude holiday rates
)
select * from t where age = 1;

首先,您需要使用相同的
CurrencyCode
Currency
表中的所有行
连接起来,然后您可以使用
RANK()分区(…
函数选择与交易日期相比最新日期的行

SELECT
    *
FROM
    (SELECT
        t.*,
        c.*,
        RANK() OVER (PARTITION BY t.ID ORDER BY ABS(DATEDIFF(d, t.[Date], c.[Date])) ASC, c.[Date] DESC) rn
    FROM
        Transactions t
    INNER JOIN
        Currency c ON t.CurrencyCode = c.CurrencyCode) t
WHERE
    t.rn = 1

首先,您需要使用相同的
CurrencyCode
Currency
表中的所有行
连接起来,然后您可以使用
RANK()分区(…
函数选择与交易日期相比最新日期的行

SELECT
    *
FROM
    (SELECT
        t.*,
        c.*,
        RANK() OVER (PARTITION BY t.ID ORDER BY ABS(DATEDIFF(d, t.[Date], c.[Date])) ASC, c.[Date] DESC) rn
    FROM
        Transactions t
    INNER JOIN
        Currency c ON t.CurrencyCode = c.CurrencyCode) t
WHERE
    t.rn = 1

您是否尝试过在末尾包含一个
WHERE
子句?类似于
WHERE a.Date=b.Date
?用分组查询替换?????是否尝试过在末尾包含
WHERE
子句?类似于
WHERE a.Date=b.Date
?用分组查询替换。