Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:34:23+00:00 2026-06-07T04:34:23+00:00

I am making a small database trading system and I have a problem with

  • 0

I am making a small database trading system and I have a problem with duplication which I am unsure how to solve. Basically I have a table with prices with the datetime which that price was set and I also have a table with the time a trade was made. I want to get the correct price based on trade datetime.

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

View

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

These are the three main tables if you need to see data as well just say because I don’t usually post SQL questions on this website.

Explanation

If a price is set at 12:20 and the price is 100 then at 12:40 the price is 80. These are two date ranges. So if I buy at 12:30 then I am buying at the price of 100 because that is last price. I am also doing my joins in a view so I can see all data. I will post that now.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:34:24+00:00Added an answer on June 7, 2026 at 4:34 am

    To get the latest price prior to a particular trade date:

    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 )
    

    You may want to add an index on exch_ticker/trade_date(desc) to price_details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a small iPhone application in which I have implemented the database
I'm making a small DataBase with MySQL Workbench. I have a main table, called
I have a small problem. I am making a site that has Tags and
I'm making a small system for personal use on that I want to handle
I am current making some small JS game in Netbeans and I have started
I am making a small calculator (just for JavaScript learning) and I have two
I am making a small C# GUI application that reads table-like (cells, rows, columns)
Im making a (small) site in php & mysql. The mysql database consists of
I need a small help in making a mysql database structure I am making
I have been working on a program, simulating a small database where I could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.