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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:06:22+00:00 2026-05-14T05:06:22+00:00

I have a product table which simplifies to this: create table product(id int primary

  • 0

I have a product table which simplifies to this:

create table product(id int primary key identity, productid int, year int, quarter int, price money)

and some sample data:

insert into product select 11, 2010, 1, 1.11
insert into product select 11, 2010, 2, 2.11
insert into product select 11, 2010, 3, 3.11
insert into product select 12, 2010, 1, 1.12
insert into product select 12, 2010, 2, 2.12
insert into product select 13, 2010, 1, 1.13

Prices are can be changed each quarter, but not all products get a new price each quarter. Now I could duplicate the data each quarter, keeping the price the same, but I’d rather use a view.

How can I create a view that can be used to return prices for (for example) quarter 2? I’ve written this to return the current (=latest) price:

CREATE VIEW vwCurrentPrices AS
    SELECT *
    FROM
    (
      SELECT  *, ROW_NUMBER() OVER (PARTITION BY productid  ORDER BY year DESC, quarter DESC) AS Ranking        
      FROM product
    ) p
    WHERE p.Ranking = 1

I’d like to create a view so I can use queries like select * from vwProduct where quarter = 2

  • 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-05-14T05:06:22+00:00Added an answer on May 14, 2026 at 5:06 am
    CREATE TABLE quarters ([year] int, [quarter] int, PRIMARY KEY([year],[quarter]))
    
    INSERT INTO quarters ([year], [quarter])
    SELECT 2000 + number/4 [year], number%4 + 1 [quarter]
    FROM master.dbo.spt_values
    WHERE type = 'P' AND number BETWEEN 0 and 199
    
    CREATE VIEW pricing WITH SCHEMABINDING AS 
    WITH pricerank AS
        (SELECT q.[year], q.[quarter], p.productid, p.price, 
        p.[year] effectiveyear, p.[quarter] effectivequarter, 
        ROW_NUMBER() OVER (PARTITION BY p.productid, 
        q.[year], q.[quarter] ORDER BY p.[year] DESC, p.[quarter] DESC) AS ranking 
        FROM dbo.quarters q INNER JOIN dbo.product p
        ON p.[year] = q.[year] AND p.[quarter] <= q.[quarter] OR p.[year] < q.[year]
        WHERE q.[year] <= YEAR(GETDATE()))
    SELECT [year], [quarter], productid, price
    FROM pricerank
    WHERE ranking = 1
    
    CREATE UNIQUE CLUSTERED INDEX IX_pricing ON pricing([year],[quarter],productid)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.