?
Solved

Help with a view

Posted on 2013-10-22
18
Medium Priority
?
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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 49

Expert Comment

by:PortletPaul
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 49

Expert Comment

by:PortletPaul
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 49

Accepted Solution

by:
PortletPaul 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 49

Expert Comment

by:PortletPaul
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 49

Expert Comment

by:PortletPaul
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

752 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