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 8818865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:12:44+00:00 2026-06-14T05:12:44+00:00

I have a table with 3 column +—————+————————-+——-+ | InstrumentId | Date | Price

  • 0

I have a table with 3 column

+---------------+-------------------------+-------+
| InstrumentId  |          Date           | Price |
+---------------+-------------------------+-------+
|            39 | 2012-10-31 00:00:00.000 |   150 |
|            39 | 2012-11-01 00:00:00.000 |   160 |
|            39 | 2012-11-01 00:00:00.000 |   200 |
|            40 | 2012-10-31 00:00:00.000 |   150 |
|            40 | 2012-11-01 00:00:00.000 |   140 |
|            40 | 2012-11-01 00:00:00.000 |   200 |
|            50 | 2012-10-31 00:00:00.000 |   150 |
|            50 | 2012-11-01 00:00:00.000 |   150 |
|            50 | 2012-11-01 00:00:00.000 |   150 |
+---------------+-------------------------+-------+

I need to recive next result:

+--------------+-------+
| InstrumentId | Price |
+--------------+-------+
|           39 |   200 |
|           40 |     0 |
|           50 |   150 |
+--------------+-------+

rules:
if price for same InstrumentId is growing or is equal => return last price (that means every next price greater or equal to a previous price.
For instance Id 39: 150 <= 160 <= 200 => return 200)
if any price for same InstrumentId is less than previous => return 0 (see instrumentId 40)

I can do that with a cursor… but I think that exist a simply workaround to do this.
Any ideas?

test data:

DECLARE @table TABLE(
    instrumentId INT NOT NULL,
    priceListDate DATETIME NOT NULL,
    price DECIMAL NOT NULL 
)

INSERT INTO @table
(
    instrumentId,
    priceListDate,
    price
)
VALUES( 39, '2012-10-31 00:00:00.000',  150),
(39,'2012-11-01 00:00:00.000',  160),
(39,'2012-11-01 00:00:00.000',  200),
(40,'2012-10-31 00:00:00.000',  150),
(40,'2012-11-01 00:00:00.000',  140),
(40,'2012-11-01 00:00:00.000',  200),
(50,'2012-10-31 00:00:00.000',  150),
(50,'2012-11-01 00:00:00.000',  150),
(50,'2012-11-01 00:00:00.000',  150)
  • 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-14T05:12:45+00:00Added an answer on June 14, 2026 at 5:12 am

    Let me know if this works ok. I’m guessing you won’t ever have a price = -1 in your table, I think that would cause problems for the current solution.

    WITH    CTE
          AS ( SELECT   RN = ROW_NUMBER() OVER ( ORDER BY instrumentId ) ,
                        *
               FROM     @table
             )
    SELECT  CASE WHEN MIN(X.xPrice) = -1 THEN 0
                 ELSE MAX(X.xPrice)
            END 'price' ,
            X.instrumentId
    FROM    ( SELECT    CASE WHEN [Current Row].instrumentId = [Next Row].instrumentId
                             THEN CASE WHEN [Current Row].price > [Next Row].price
                                       THEN -1
                                       ELSE [Current Row].price
                                  END
                             ELSE CASE WHEN [Previous Row].instrumentId = [Current Row].instrumentId
                                       THEN CASE WHEN [Previous Row].price <= [Current Row].price
                                                 THEN [Current Row].price
                                                 ELSE -1
                                            END
                                       ELSE [Current Row].price
                                  END
                        END 'xPrice' ,
                        [Current Row].RN ,
                        [Current Row].instrumentId
              FROM      CTE [Current Row]
                        LEFT JOIN CTE [Previous Row] ON [Previous Row].RN = [Current Row].RN
                                                        - 1
                        LEFT JOIN CTE [Next Row] ON [Next Row].RN = [Current Row].RN
                                                    + 1
            ) X
    GROUP BY X.instrumentId
    

    It might seem a bit convoluted but the basic idea is to determine the next and previous row of the current on in order to test the value of the price column for that row.

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

Sidebar

Related Questions

I am using Mysql database. I have 3 table-: table Column tier_price customer_id,sku(Unique),price,website catalog_product
I have a table column of data type DATE. I am inserting through Java
I have table with column Percentage varchar(10) Data in that table is Pecentage 2/10
I have a table column I’m trying to expand and hide. jQuery seems to
I have a table column called post_tags within a table called posts where assigned
I have a table column where each row has one of three states, and
Imagine I have this table: Column A | Column B | Column C ------------------------------
If I have a table column with data and create an index on this
I have table and this table contain result column with some entries. I just
I have a table with column for storing product code. | product_id | Product-name

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.