Solved

Help with a view

Posted on 2013-10-22
18
409 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 40

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
 

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 48

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 48

Expert Comment

by:PortletPaul
ID: 39593380
sorry, just noticed that comment about 2000, yep, won't work there
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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 48

Accepted Solution

by:
PortletPaul earned 500 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 48

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 48

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now