Solved

Help with a view

Posted on 2013-10-22
18
421 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 32
Help Required 3 97
Sql server, import complete table, using vb.net 9 35
MS SQL BCP Extra Lines Between Records 2 19
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

777 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