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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:54:48+00:00 2026-06-13T22:54:48+00:00

Using a view in SQL Server 2000 Table1: id z1 z2 z3 z4 type

  • 0

Using a view in SQL Server 2000

Table1:

id  z1      z2      z3      z4   type
--------------------------------------
01A 300     400     300     400  01
2B  300     400     300     500  02
3C  700     600     400     300  01
04A 500     400     800     900  01
05B 400     300     400     300  02
06  150     200     200     150  03
....

Table2:

type  value1         value2
------------------------------------    
01    0      300
01    301    500          
02    0      200
02    201    400
03
.....

I want to select the table1 rows based on table2 range:

Combination for table1- max(Z1, Z2) and max(Z3, Z4)

If Max(z1,z2) range is less than or equal to table2 max(value2) where table1.type = table2.type
If Max(z1, z2) range is less than or equal to table2 max(value2) where table1.type = table2.type

If z1, Z2 range less than or equal to table2 then i want to display the z1, z2 rows otherwise null
If z3, z4 range less than or equal to table2 then i want to display the z3, z4 rows otherwise null

Expected output

Table1:

 id  z1      z2      z3      z4   type
    --------------------------------------

01A 300     400     300     400  01 ' `Both (z1, z2), (z3, z4) rows are matching with table2 for type 01`
2B  300     400     null     null  02 ' (z1, z2) are matching, (z3, z4) rows are not matching with table2 for type 02
3C  null    null     400     300  01 ' (z1, z2) rows are not matching, (z3, z4) rows are matching with table2 for type 01
04A 500     400     null     null  01 ' (z1, z2) rows are matching, (z3, z4) rows are not matching with table2 for type 01
05B 400     300     400     300  02 ' Both (z1, z2), (z3, z4) rows are matching with table2 for type 02

....

Currently I’m using a view, I don’t want to change to stored procedure because most of the report are using this view.

How to do it in sql..?

  • 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-13T22:54:48+00:00Added an answer on June 13, 2026 at 10:54 pm

    This seems to give the expected results. (I’d have preferred to use a CTE, but you specified 2000).

    Sample data:

    declare @T1 table (Id varchar(3) not null,z1 int not null,z2 int not null,z3 int not null,z4 int not null,type char(2) not null)
    insert into @T1 (id,z1,z2,z3,z4,type) values
    ('01A',300,400,300,400,'01'),
    ('2B',300,400,300,500,'02'),
    ('3C',700,600,400,300,'01'),
    ('04A',500,400,800,900,'01'),
    ('05B',400,300,400,300,'02'),
    ('06',150,200,200,150,'03')
    
    declare @T2 table (type char(2) not null,value1 int not null,value2 int not null)
    insert into @T2 (type,value1,value2) values
    ('01',0,300),
    ('01',301,500),
    ('02',0,200),
    ('02',201,400)
    

    Query:

    select
        t1.Id,
        CASE WHEN t2.type is not null then z1 END as z1,
        CASE WHEN t2.type is not null then z2 END as z2,
        CASE WHEN t3.type is not null then z3 END as z3,
        CASE WHEN t3.type is not null then z4 END as z4,
        t1.type
    from
        @T1 t1
            left join
        (select type,MAX(value2) as val2 from @T2 group by type) t2
            on
                t1.type = t2.type and
                t1.z1 <= t2.val2 and
                t1.z2 <= t2.val2
            left join
        (select type,MAX(value2) as val2 from @T2 group by type) t3
            on
                t1.type = t3.type and
                t1.z3 <= t3.val2 and
                t1.z4 <= t3.val2
    where
        t2.type is not null or
        t3.type is not null
    

    Result:

    Id   z1          z2          z3          z4          type
    ---- ----------- ----------- ----------- ----------- ----
    01A  300         400         300         400         01
    2B   300         400         NULL        NULL        02
    3C   NULL        NULL        400         300         01
    04A  500         400         NULL        NULL        01
    05B  400         300         400         300         02
    

    I’m not sure what the relevance of your question title was, the verbiage in the middle is quite confusing, and I made a guess at the final WHERE clause since the 06 row seems to disappear.

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

Sidebar

Related Questions

Using a view in SQL Server 2000 Table1 : id z1 z2 z3 z4
How to upload image URL in SQL server and retrieve in grid view using
I am using SQL Server 2000 and feeling stuck about how to do this:
I´m using Delphi 5 with SQL Server 2000 here. I have created an ADOQuery
I'm using MS Query Analyzer (as part of SQL Server 2000) to write T-SQL
I am learning how to create view using SQL server. I am trying to
Does anyone have experience with using indexed view in MS SQL Server 2008? I
I'm currently using SQL 2000 (it's a vendor server - don't ask...), and I'm
I'm using ASP.net membership system with SQL Server 2000 database. What I want to
I am using SQL Server 2008 Enterprise. I have yearly customer data from 2000

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.