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

  • SEARCH
  • Home
  • 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 8157051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:08:56+00:00 2026-06-06T17:08:56+00:00

I am trying to write a query where for each day I get the

  • 0

I am trying to write a query where for each day I get the minimum and maximum price for each item from price details table.

In price details table prices are set multiple times a day so there are many records for the same date. So I want a table where there is one row for each date and then join that table to the same table so for each distinct date I want the minimum and maximum value.

USE [a_trading_system]
GO

/****** Object:  Table [dbo].[price_details]    Script Date: 07/01/2012 17:28:31 ******/
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

SQL Query

select distinct 
substring(convert(varchar(12),p1.price_set_date), 0, 12),
p2.exch_ticker,
(select MIN(buy_price) from price_details ),
(select MAX(buy_price) from price_details )
from price_details as p1

left join price_details as p2 on p2.exch_ticker = p1.exch_ticker 

where p1.exch_ticker = p2.exch_ticker

group by p1.price_set_date, p2.exch_ticker

Summary

Table has many prices set on the same day. Want min and max values for each day for each exch ticker.

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-06T17:08:57+00:00Added an answer on June 6, 2026 at 5:08 pm

    A simple group by should work:

    select  cast(price_set_date as date) as [Date]
    ,       exch_ticker 
    ,       min(buy_price) as MinPrice
    ,       max(buy_price) as MaxPrice
    from    price_details as p
    group by 
            exch_ticker
    ,       cast(price_set_date as date)
    

    Not sure why your example query is using a self join. If there’s a good reason please add an explanation to your question.

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

Sidebar

Related Questions

I am trying to write a query to get count of employees in each
I'm trying to write a query that will return the closest match from a
I'm trying to write a query to join a user table to an activity
I am trying to write the query for existing table data. TABLE_A and TABLE_B
I'm trying to write a query to pull some info from our database and
I have a table containing user-account permissions and I'm trying to write a query
I am trying to write a SQL query that pulls from 3 tables and
I'm trying to write a query that returns the most recent GPS positions from
I'm trying to write a query that will return ... Each distinct user id
I'm new to LINQ and trying to write a query which chooses the table

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.