1

我正在制作一个小型数据库交易系统,但我遇到了重复问题,我不确定如何解决。基本上,我有一张价格表,其中包含设定价格的日期时间,我也有一张表,其中包含进行交易的时间。我想根据交易日期时间获得正确的价格。

USE [a_trading_system]
GO

/****** Object:  Table [dbo].[Trade]    Script Date: 06/30/2012 14:49:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Trade](
[trade_id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
[trade_volume] [int] NOT NULL,
[trade_action] [varchar](5) NOT NULL,
[trade_date] [datetime] NOT NULL,
[timestap] [timestamp] NOT NULL,
[trader_id] [int] NOT NULL,
[exch_ticker] [varchar](8) NOT NULL,
CONSTRAINT [PK_Trades] PRIMARY KEY CLUSTERED 
(
[trade_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Trade]  WITH CHECK ADD  CONSTRAINT [FK_Trade_Contract] FOREIGN   KEY([exch_ticker])
REFERENCES [dbo].[Contract] ([exch_ticker])
GO

ALTER TABLE [dbo].[Trade] CHECK CONSTRAINT [FK_Trade_Contract]
GO

ALTER TABLE [dbo].[Trade]  WITH CHECK ADD  CONSTRAINT [FK_Trade_Trader] FOREIGN   KEY([trader_id])
REFERENCES [dbo].[Trader] ([trader_id])
GO

ALTER TABLE [dbo].[Trade] CHECK CONSTRAINT [FK_Trade_Trader]
GO

ALTER TABLE [dbo].[Trade] ADD  CONSTRAINT [DF_Trades_trade_id]  DEFAULT (newid()) FOR [trade_id]
GO



USE [a_trading_system]
GO

/****** Object:  Table [dbo].[Contract]    Script Date: 06/30/2012 14:56:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Contract](
[exch_ticker] [varchar](8) NOT NULL,
[exch_name] [varchar](50) NULL,
[portfolio_id] [varchar](8) NOT NULL,
[region_cd] [varchar](5) NULL,
 CONSTRAINT [PK_Contract] PRIMARY KEY CLUSTERED 
(
[exch_ticker] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Contract]  WITH CHECK ADD  CONSTRAINT [FK_Contract_portfolio]    FOREIGN KEY([portfolio_id])
REFERENCES [dbo].[portfolio] ([portfolio_id])
GO

ALTER TABLE [dbo].[Contract] CHECK CONSTRAINT [FK_Contract_portfolio]
GO

ALTER TABLE [dbo].[Contract]  WITH CHECK ADD  CONSTRAINT [FK_Contract_region] FOREIGN   KEY([region_cd])
REFERENCES [dbo].[Region] ([region_cd])
GO

ALTER TABLE [dbo].[Contract] CHECK CONSTRAINT [FK_Contract_region]
GO



USE [a_trading_system]
GO

/****** Object:  Table [dbo].[price_details]    Script Date: 06/30/2012 14:58:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[price_details](
[price_id] [int] IDENTITY(1,1) NOT NULL,
[exch_ticker] [varchar](8) NOT NULL,
[price_set_date] [datetime] NOT NULL,
[buy_price] [decimal](7, 2) NOT NULL,
[sell_price] [decimal](7, 2) NOT NULL,
 CONSTRAINT [PK_price_detail] PRIMARY KEY CLUSTERED 
(
[price_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[price_details]  WITH CHECK ADD  CONSTRAINT    [FK_price_details_Contract] FOREIGN KEY([exch_ticker])
REFERENCES [dbo].[Contract] ([exch_ticker])
GO

ALTER TABLE [dbo].[price_details] CHECK CONSTRAINT [FK_price_details_Contract]
GO

看法

USE [a_trading_system]
GO

/****** Object:  View [dbo].[V_all_uk]    Script Date: 06/30/2012 14:39:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[V_all_uk]
AS

SELECT distinct
co.exch_ticker,
--co.region_cd, 
po.portfolio_type, 
r.region_name, 
r.currency,
t.trade_id,
t.trade_volume,
t.trade_action,
t.trade_date,
pr.buy_price,

--(select distinct pr.buy_price from price_details pr
--where pr.price_set_date <= t.trade_date or  pr.price_set_date >= t.trade_date) as    price_details,

--MIN(t.trade_date) as trade_date,
--pr.buy_price,
--pr.sell_price,

--pr.price_set_date, --This is the cause of duplication
--pr.price_set_time,case when t.trade_date IS NOT NULL then 

--case 
--when t.trade_action = 'Buy' then 
--t.trade_volume * max(pr.buy_price)
--else
--case when trade_action = 'Sell' then
--t.trade_volume * max(pr.sell_price)
--end 
--end as 'trade_value' ,
tr.trader_name,
tr.trader_address, 
tr.phone
FROM dbo.Contract as co 
INNER JOIN dbo.Portfolio as po ON co.portfolio_id = po.portfolio_id 
INNER JOIN dbo.region as r ON co.region_cd = r.region_cd 
INNER JOIN dbo.Trade as t ON co.exch_ticker = t.exch_ticker 
INNER JOIN dbo.trader as tr ON t.trader_id = tr.trader_id
inner join dbo.price_details as pr on pr.exch_ticker = t.exch_ticker

where r.region_cd = 'UK'

--group by 

--co.exch_ticker,
--co.region_cd, 
--po.portfolio_type, 
--r.region_name, 
--r.currency,
--t.trade_id,
--t.trade_volume,
--t.trade_action,
--pr.buy_price,
--pr.sell_price,
--tr.trader_name,
--tr.trader_address, 
--tr.phone

GO

如果您还需要查看数据,这些是三个主要表,因为我通常不会在此网站上发布 SQL 问题。

解释

如果价格设置为 12:20,价格为 100,则 12:40 价格为 80。这是两个日期范围。因此,如果我在 12:30 买入,那么我将以 100 的价格买入,因为那是最后的价格。我也在视图中进行连接,以便查看所有数据。我现在会发布。

谢谢

4

2 回答 2

1

要获取特定交易日期之前的最新价格:

select buy_price, sell_price
  from price_details
  where exch_ticker = @exch_ticker and price_set_date =
    ( select max( price_set_date )
        from price_details
        where exch_ticker = @exch_ticker and price_set_date <= @trade_date )

您可能想在exch_ticker/ trade_date(desc) 上添加一个索引到price_details.

于 2012-06-30T14:43:16.783 回答
0

以下假设 SQL Server 2005 或更高版本。

这个想法是首先加入Tradeprice_details过滤掉时间大于相应交易时间的价格:

SELECT ...
FROM dbo.Trade t
  INNER JOIN dbo.price_details pr ON pr.exch_ticker = t.exch_ticker

以上将为您设置一行,其中每笔交易都获得了截至交易时间的所有价格。现在只需对价格进行排名并获得最新的价格:

WITH trade_prices AS (
  SELECT
    t.*,                -- actually you might want to review the list
    pr.price_set_date,  -- of columns being pulled from the two tables
    pr.buy_price,
    pr.sell_price,
    rnk = ROW_NUMBER() OVER (PARTITION BY t.trade_id ORDER BY pr.price_set_date DESC)
  FROM dbo.Trade t
    INNER JOIN dbo.price_details pr ON pr.exch_ticker = t.exch_ticker
)
SELECT *
FROM trade_prices
WHERE rnk = 1

要将其合并到您的视图中,您只需:

1)添加trade_pricesCTE,

2) 将两个连接 toTrade和 to替换为 toprice_details的连接trade_prices

3)将trade_prices.rnk = 1条件添加到WHERE子句中。

当然,该trader表现在将被连接到trade_prices而不是 to Trade。而且您还需要将表别名prt视图的选择列表中更改为您选择分配给trade_prices.

于 2012-06-30T18:28:52.600 回答