2012-06-30 53 views
1

我在製作一個小型的數據庫交易系統,我有一個重複的問題,我不確定如何解決。基本上我有一個表格,其中包含該價格設置的日期時間的價格,我也有一張交易時間表。我想根據交易日期時間獲得正確的價格。T-SQL在兩個日期之間得到價格

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的價格買入,因爲那是最後的價格。我也在視圖中加入,以便可以看到所有數據。我現在會發布。

感謝

+0

看看我更新的帖子。我用我的數據庫邏輯創建了一個視圖,但是我得到了重複的行。 –

+0

任何人都可以幫忙嗎? –

回答

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

+0

我可以看到你在做什麼,但我會如何將這個應用於我的觀點? –

+0

視圖更改爲''JOIN'與額外的邏輯price_details'選擇一個合適的價格,並從'貿易爲t'拿起'exch_ticker'和'trade_date'。很快。 – HABO

+0

現在我覺得真的很笨,你能告訴我一個例子嗎?我想要做的就是使用視圖根據交易日期顯示正確的價格。 –

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_prices CTE,

2)替代兩個加入到Tradeprice_details,與加盟trade_prices

3)添加trade_prices.rnk = 1條件爲WHERE條款。

當然,trader表現在將加入到trade_prices而不是向Trade。而且您還需要將視圖的選擇列表中的表別名prt更改爲您選擇分配給trade_prices的表。

+0

你的假設是正確的。我會看看你的解決方案,看看我是否覺得它更有用。 –

相關問題