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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:52:22+00:00 2026-05-22T18:52:22+00:00

I have a requirement to create a view and the business scenario is explained

  • 0

I have a requirement to create a view and the business scenario is explained below

Consider i am having table Products(all product information) and Settings(settings for a country/state/City)

Now i have to create a view which gives product information by considering settings, It might be possible to have cities/states/country have there own settings.
Design of the view
It means first i need to check
1. any city is having there custom settings then output those records
UNION ALL
2. any state is having there custom settings then output those records by excluding cities under this state in step 1
UNION ALL
3. any country is having there custom settings or not then output those records by excluding cities ans states records in step1 and step2

This is the design which i thought of, is there anything wrong in the design?

Performance improving
With this existing design its taking 5 minutes for a query to run without any indexes on view and base tables.

Now what is the best option for me to improve the performance.
Creating indexed views or create index on base tables? which one helps me to make the query run in seconds 🙂

Sample Data

Product Table
enter image description here

Settings table

enter image description here

Expected Output

enter image description here

  • 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-22T18:52:22+00:00Added an answer on May 22, 2026 at 6:52 pm

    I can’t work out why your (P2 – Blue) result is showing. I re-wrote your samples as SQL, and created what I thought you wanted (whilst waiting for your expected output), and mine only produces one row (P1 – Red)

    create table dbo.Product (
        ProductID int not null,
        Name char(2) not null,
        StateId char(2) not null,
        CityId char(2) not null,
        CountryId char(2) not null,
        Price int not null,
        Colour varchar(10) not null,
        constraint PK_Product PRIMARY KEY (ProductID)
    )
    go
    insert into dbo.Product (ProductID,Name,StateId,CityId,CountryId,Price,Colour)
    select 1,'P1','S1','C1','C1',150,'Red' union all
    select 2,'P2','S2','C2','C1',100,'Blue' union all
    select 3,'P3','S1','C3','C1',200,'Green'
    go
    create table dbo.Settings (
        SettingsID int not null,
        StateId char(2) null,
        CityId char(2) null,
        CountryId char(2) null,
        MaxPrice int not null,
        MinPrice int not null,
        constraint PK_Settings PRIMARY KEY (SettingsID)
    )
    go
    insert into dbo.Settings (SettingsID,StateId,CityId,CountryId,MaxPrice,MinPrice)
    select 1,null,null,'C1',1000,150 union all
    select 2,'S1',null,'C1',2000,100 union all
    select 3,'S1','C3','C1',3000,300
    go
    

    And now the actual view:

    create view dbo.Products_Filtered
    with schemabinding
    as
        with MatchedSettings as (
        select p.ProductID,MAX(MinPrice) as MinPrice,MIN(MaxPrice) as MaxPrice
        from
            dbo.Product p
                inner join
            dbo.Settings s
                on
                    (p.CountryId = s.CountryId or s.CountryId is null) and
                    (p.CityId = s.CityId or s.CityId is null) and
                    (p.StateId = s.StateId or s.StateId is null)
        group by
            p.ProductID
        )
        select
            p.ProductID,p.Name,p.CityID,p.StateId,p.CountryId,p.Price,p.Colour
        from
            dbo.Product p
                inner join
            MatchedSettings ms
                on
                    p.ProductID = ms.ProductID and
                    p.Price between ms.MinPrice and ms.MaxPrice
    

    What I did was to combine all applicable settings, and then assumed that we applied the most restrictive settings (so take the MAX MinPrice specified and MIN MaxPrice).

    Using those rules, the (P2 – Blue) row is ruled out, since the only applicable setting is setting 1 – which has a Min price of 150.

    If I reverse it, so that we try to be as inclusive as possible (MIN MinPrice and MAX MaxPrice), then that returns (P1 – Red) and (P3 – Green) – but still not (P2 – Blue)

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

Sidebar

Related Questions

I have a organization name table with the following structure given below: CREATE TABLE
I have a requirement to create a windows form control which has to detect
I have a requirement to create a simple windows forms application that allows an
I have a requirement to create two different maps in C++. The Key is
I have a requirement to create a multi level treeview in ASP.Net (with VB)
If I have a requirement to create a data structure that has the following
For a project we have a requirement to create an interfacedefinition that will return
I am new to MySQL coming from Oracle. I have a requirement to create
I have a requirement to take client side XAML (from Silverlight) and create a
I have a requirement in sharepoint where I want to Create the top link

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.