Improve company productivity with a Business Account.Sign Up

x
?
Solved

Help with a view

Posted on 2013-10-22
18
Medium Priority
?
487 Views
Last Modified: 2013-10-23
Hello.

I need help creating a view.
(it would be easy using a cte but I need this to work on sql 2000 so i cant use CTE)

I have a table with

ID, Alternate Version

And I want to select rows with max (version) and min (alt)
IE, version has higher prio than alternate..

say that I have

ID VER ALT
1   1     1
2   1     1
2   1    2
2  2     2
2   3   2
2  3 4

The view should return

1  2  1  (for row with id 1)
2  3  2  (for row with id 2)   (not 2  3 1 since it don't exist. so its not just to select id max(ver) min(alt)  from the table.
0
Comment
Question by:pucko73
  • 9
  • 5
  • 3
  • +1
18 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39593303
>> And I want to select rows with max (version) and min (alt)

Do you want max(version) and min(alt) record for each ID?

>> 1  2  1  (for row with id 1)

How did you get this record. There is no such record in your input data (i.e. 1 1 1).
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39593324
select ID,Min(alernate),max(version) from  [tablename]
group by ID
0
 

Author Comment

by:pucko73
ID: 39593341
Won't work as I wrot.

select id,alt,ver from tst
:
1      1      1
2      1      1
2      1      2
2      2      2
2      3      2
2      3      4

select ID,Min(alt),max(ver) from  tst
group by ID

1      1      1
2      1      4

select * from tst where id=2 and alt = 1 and ver = 4
gives no result.

I expect result as this:

with cte  
AS  
(  
   select id,alt,ver from tst t  
   where ver = (select max(ver) from tst t2 where t2.id=t.id)  
)  
select   * from cte  
where cte.alt = (select min(alt) from tst t3 where t3.id=cte.id and t3.ver=cte.ver)

1      1      1
2      3      4
0
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 

Author Comment

by:pucko73
ID: 39593350
sorry sarath, wrote wrong data. Se my last example instead.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39593351
DECLARE @T1 TABLE (
ID INT,
alt INT,
ver INT)

INSERT INTO @T1
select id,alt,ver from tst t  
   where ver = (select max(ver) from tst t2 where t2.id=t.id)

select   * from @T1 cte
where cte.alt = (select min(alt) from tst t3 where t3.id=cte.id and t3.ver=cte.ver)
0
 

Author Comment

by:pucko73
ID: 39593367
I want to cretae a veiw
cant use variables in a view.

I want it to work on sql 2000. CTE wont work on SQL2000

Othervise my example abowe will work......

I can always crete a helper view... but that's not what I want.

create view helper as
select id,alt, max(ver) as ver from tst group by id,alt

and then:
create view TheRealView
as
select id,min(alt),max(ver) from helper group by id
0
 
LVL 50

Expert Comment

by:Paul
ID: 39593368
the query below matches your expected results:
/*
| ID | ALT | VER |
|----|-----|-----|
|  1 |   1 |   1 |
|  2 |   3 |   4 |
*/

SELECT
        id
      , alt
      , ver
FROM (
      SELECT
              id
            , alt
            , ver
            , max(ver) over (partition BY id)      AS max_ver
            , min(alt) over (partition BY id, ver) AS min_alt
      FROM tst
      ) AS derived
WHERE ver = max_ver
AND alt = min_alt
;


CREATE TABLE tst
    ([id] int, [alt] int, [ver] int)
;
    
INSERT INTO tst
    ([id], [alt], [ver])
VALUES
    (1, 1, 1),
    (2, 1, 1),
    (2, 1, 2),
    (2, 2, 2),
    (2, 3, 2),
    (2, 3, 4)
;
: http://sqlfiddle.com/#!3/59804/4

Open in new window

0
 

Author Comment

by:pucko73
ID: 39593374
Yes but Over partition by came with SQL2005 and don't work on SQL 2000
0
 
LVL 50

Expert Comment

by:Paul
ID: 39593380
sorry, just noticed that comment about 2000, yep, won't work there
0
 

Author Comment

by:pucko73
ID: 39593385
But I think you put me in the correct direction anyway:


This seems to work. (if I'm not wrong)

SELECT
        id
      , min(alt)
      , max(ver)
FROM (
      SELECT
              id
                  ,alt
                  ,max(ver) as ver
           
           
      FROM tst group by id,alt,ver
      ) AS derived group by id
0
 
LVL 50

Accepted Solution

by:
Paul earned 2000 total points
ID: 39593386
try this one:
SELECT
        tst.id
      , tst.alt
      , tst.ver
FROM tst
INNER JOIN (
             SELECT id, max(ver) AS max_ver FROM tst GROUP BY id
           ) AS mv
             ON tst.id = mv.id AND tst.ver = mv.max_ver
INNER JOIN (
             SELECT id, ver, min(alt) AS min_alt FROM tst GROUP BY id, ver
           ) AS ma
             ON tst.id = ma.id AND tst.alt = ma.min_alt
;

-- http://sqlfiddle.com/#!3/59804/7

Open in new window

0
 

Author Closing Comment

by:pucko73
ID: 39593390
Thanks.... my comment abowe did not work. But this works like a charm.....
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39593397
one correction

SELECT
        id
      , min(alt)
      , ver FROM (
      SELECT
              id
                  ,alt
                  ,max(ver) as ver
           
           
      FROM tst group by id,alt,ver
      ) AS derived group by id
0
 
LVL 50

Expert Comment

by:Paul
ID: 39593411
Thanks! Cheers, Paul

btw: I added tag to the question regarding sql 2000
Many of us look at topics/tags to figure out the environment
- so if you have more 2k questions I suggest using a tag.
0
 

Author Comment

by:pucko73
ID: 39593413
well it wont....

I have to change to  AS derived group by id,ver otervise it won't runt.
And it also wont give me the result that I want:
The accepted answer works fine though

1      1      1
2      1      1
2      1      2
2      3      4
0
 

Author Comment

by:pucko73
ID: 39593415
Ahh ok thank Paul.

There was no sql 2000 in the selection when creating the question.
Only sql2005 and 2008
0
 
LVL 50

Expert Comment

by:Paul
ID: 39593422
yes, that's right the topics won't allow selection of sql 2k, but you can type in your own tags

watch for it at the next question.
0
 

Author Comment

by:pucko73
ID: 39593823
ok thanks
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

585 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question