Unix Technical Forum

Determine winner from two distinct lines

This is a discussion on Determine winner from two distinct lines within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:35 AM
John Grenier
 
Posts: n/a
Default Determine winner from two distinct lines

Hi,

I have to determine the "standing" (WIN - TIE - LOSS) from
confrontations between two teams on a contest. The table matchResults
has fields cont_id, team_id and contest_result (int).

TABLE matchResults
cont_id team_id contest_result
1 1 3
1 2 5
2 2 4
2 3 4
3 3 3
3 1 4


What I want is to create a view that determines the following
information

VIEW teamMatchStatus
cont_id team_id cont_status
1 1 loss
1 2 win
2 2 tie
2 3 tie
3 3 loss
3 1 win

and eventualy calculate the standings

TABLE standings
team_id win tie loss
1 1 0 1
2 1 1 0
3 0 1 1


Anyone can help me out with this?

Regards,

Jon

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:35 AM
Mischa Sandberg
 
Posts: n/a
Default Re: Determine winner from two distinct lines

create view teamMatchStatus as
select matchResults.cont_id, matchResults.team_id,
case sign(team_score - mid)
when 1 then 'win'
when 0 then 'tie'
else 'loss'
end cont_status
from matchResults
join (
select cont_id, avg(team_score) mid
from matchResults
group by cont_id
) matchMids
on matchMids.cont_id = matchResults.cont_id

create view teamStandings as
select team_id,
sum(case when cont_status = 'win' then 1 else 0 end) win,
sum(case when cont_status = 'tie' then 1 else 0 end) tie,
sum(case when cont_status = 'loss' then 1 else 0 end) loss,
from teamMatchStatus
group by team_id


"John Grenier" <jgrenier95@hotmail.com> wrote in message
news:40f2f3cc$0$16465$c397aba@news.newsgroups.ws.. .
> Hi,
>
> I have to determine the "standing" (WIN - TIE - LOSS) from
> confrontations between two teams on a contest. The table matchResults
> has fields cont_id, team_id and contest_result (int).
>
> TABLE matchResults
> cont_id team_id contest_result
> 1 1 3
> 1 2 5
> 2 2 4
> 2 3 4
> 3 3 3
> 3 1 4
>
>
> What I want is to create a view that determines the following
> information
>
> VIEW teamMatchStatus
> cont_id team_id cont_status
> 1 1 loss
> 1 2 win
> 2 2 tie
> 2 3 tie
> 3 3 loss
> 3 1 win
>
> and eventualy calculate the standings
>
> TABLE standings
> team_id win tie loss
> 1 1 0 1
> 2 1 1 0
> 3 0 1 1
>
>
> Anyone can help me out with this?
>
> Regards,
>
> Jon
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:35 AM
John Gilson
 
Posts: n/a
Default Re: Determine winner from two distinct lines

"John Grenier" <jgrenier95@hotmail.com> wrote in message
news:40f2f3cc$0$16465$c397aba@news.newsgroups.ws.. .
> Hi,
>
> I have to determine the "standing" (WIN - TIE - LOSS) from
> confrontations between two teams on a contest. The table matchResults
> has fields cont_id, team_id and contest_result (int).
>
> TABLE matchResults
> cont_id team_id contest_result
> 1 1 3
> 1 2 5
> 2 2 4
> 2 3 4
> 3 3 3
> 3 1 4
>
>
> What I want is to create a view that determines the following
> information
>
> VIEW teamMatchStatus
> cont_id team_id cont_status
> 1 1 loss
> 1 2 win
> 2 2 tie
> 2 3 tie
> 3 3 loss
> 3 1 win


CREATE VIEW TeamMatchStatus (contest, team, result)
AS
SELECT R1.cont_id, R1.team_id,
CASE WHEN R1.contest_result = R2.contest_result
THEN 'tie'
WHEN R1.contest_result > R2.contest_result
THEN 'win'
ELSE 'loss'
END
FROM MatchResults AS R1
INNER JOIN
MatchResults AS R2
ON R1.cont_id = R2.cont_id AND
R1.team_id <> R2.team_id

> and eventualy calculate the standings
>
> TABLE standings
> team_id win tie loss
> 1 1 0 1
> 2 1 1 0
> 3 0 1 1


CREATE VIEW TeamStandings (team, win, loss, tie)
AS
SELECT team,
COUNT(CASE WHEN result = 'win' THEN 1 END),
COUNT(CASE WHEN result = 'loss' THEN 1 END),
COUNT(CASE WHEN result = 'tie' THEN 1 END)
FROM TeamMatchStatus
GROUP BY team

--
JAG

> Anyone can help me out with this?
>
> Regards,
>
> Jon
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:36 AM
Steve Kass
 
Posts: n/a
Default Re: Determine winner from two distinct lines

John,

Here's a sneaky solution that might be more efficient than the other
suggestions, since it doesn't require a self-join of matchResults. It will
work so long as the contest results are integers from 0 to 99999, but that
restriction can be widened.


CREATE TABLE matchResults (
cont_id int,
team_id int,
contest_result int,
primary key (cont_id, team_id)
)
insert into matchResults values (1,1,3)
insert into matchResults values (1,2,5)
insert into matchResults values (2,2,4)
insert into matchResults values (2,3,4)
insert into matchResults values (3,3,3)
insert into matchResults values (3,1,4)
go

create table Two (
n int
)
insert into Two values(-1)
insert into Two values(1)
go

create view results as
select
cont_id,
case when n = 1 then minx else maxx end/100000 as team_id,
case sign(n*(minx%100000 - maxx%100000))
when -1 then 'loss' when 0 then 'tie' else 'win' end as result
from (
select
cont_id,
min(team_id*100000 + contest_result) as minx,
max(team_id*100000 + contest_result) as maxx
from matchResults
group by cont_id
) M, Two
go

select * from results
order by cont_id, team_id

select *
from (
select
team_id,
count(case when result = 'win' then 1 end) as Win,
count(case when result = 'tie' then 1 end) as Tie,
count(case when result = 'loss' then 1 end) as Loss
from results
group by team_id
) S
order by Win - Loss desc
go

drop view results
drop table Two
drop table matchResults

-- Steve Kass
-- Drew University
-- Ref: 9EA47FEA-D8EE-47E9-8ED7-555BA50BF93A

John Grenier wrote:
> Hi,
>
> I have to determine the "standing" (WIN - TIE - LOSS) from
> confrontations between two teams on a contest. The table matchResults
> has fields cont_id, team_id and contest_result (int).
>
> TABLE matchResults
> cont_id team_id contest_result
> 1 1 3
> 1 2 5
> 2 2 4
> 2 3 4
> 3 3 3
> 3 1 4
>
>
> What I want is to create a view that determines the following
> information
>
> VIEW teamMatchStatus
> cont_id team_id cont_status
> 1 1 loss
> 1 2 win
> 2 2 tie
> 2 3 tie
> 3 3 loss
> 3 1 win
>
> and eventualy calculate the standings
>
> TABLE standings
> team_id win tie loss
> 1 1 0 1
> 2 1 1 0
> 3 0 1 1
>
>
> Anyone can help me out with this?
>
> Regards,
>
> Jon
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:37 AM
Isaac Blank
 
Posts: n/a
Default Re: Determine winner from two distinct lines

Very clever indeed. But this business of folding and unfolding is an
overhead, too. I wonder if MSSQL has something similar to analythic
fuctions, so one can write something like this (I am using Oracle syntax):

SELECT cont_id, team_id, CASE WHEN contest_result < highest_score THEN
'loss' WHEN contest_result > lowest_score THEN 'win' ELSE 'tie' END
FROM (
SELECT cont_id, team_id, contest_result,
MAX(contest_result) over (PARTITION BY cont_id ) highest_score,
MIN(contest_result) over (PARTITION BY cont_id ) lowest_score
FROM matchresults)



"Steve Kass" <skass@drew.edu> wrote in message
news:CMkJc.2885$mL5.1657@newsread1.news.pas.earthl ink.net...
> John,
>
> Here's a sneaky solution that might be more efficient than the other
> suggestions, since it doesn't require a self-join of matchResults. It

will
> work so long as the contest results are integers from 0 to 99999, but that
> restriction can be widened.
>
>
> CREATE TABLE matchResults (
> cont_id int,
> team_id int,
> contest_result int,
> primary key (cont_id, team_id)
> )
> insert into matchResults values (1,1,3)
> insert into matchResults values (1,2,5)
> insert into matchResults values (2,2,4)
> insert into matchResults values (2,3,4)
> insert into matchResults values (3,3,3)
> insert into matchResults values (3,1,4)
> go
>
> create table Two (
> n int
> )
> insert into Two values(-1)
> insert into Two values(1)
> go
>
> create view results as
> select
> cont_id,
> case when n = 1 then minx else maxx end/100000 as team_id,
> case sign(n*(minx%100000 - maxx%100000))
> when -1 then 'loss' when 0 then 'tie' else 'win' end as result
> from (
> select
> cont_id,
> min(team_id*100000 + contest_result) as minx,
> max(team_id*100000 + contest_result) as maxx
> from matchResults
> group by cont_id
> ) M, Two
> go
>
> select * from results
> order by cont_id, team_id
>
> select *
> from (
> select
> team_id,
> count(case when result = 'win' then 1 end) as Win,
> count(case when result = 'tie' then 1 end) as Tie,
> count(case when result = 'loss' then 1 end) as Loss
> from results
> group by team_id
> ) S
> order by Win - Loss desc
> go
>
> drop view results
> drop table Two
> drop table matchResults
>
> -- Steve Kass
> -- Drew University
> -- Ref: 9EA47FEA-D8EE-47E9-8ED7-555BA50BF93A
>
> John Grenier wrote:
> > Hi,
> >
> > I have to determine the "standing" (WIN - TIE - LOSS) from
> > confrontations between two teams on a contest. The table matchResults
> > has fields cont_id, team_id and contest_result (int).
> >
> > TABLE matchResults
> > cont_id team_id contest_result
> > 1 1 3
> > 1 2 5
> > 2 2 4
> > 2 3 4
> > 3 3 3
> > 3 1 4
> >
> >
> > What I want is to create a view that determines the following
> > information
> >
> > VIEW teamMatchStatus
> > cont_id team_id cont_status
> > 1 1 loss
> > 1 2 win
> > 2 2 tie
> > 2 3 tie
> > 3 3 loss
> > 3 1 win
> >
> > and eventualy calculate the standings
> >
> > TABLE standings
> > team_id win tie loss
> > 1 1 0 1
> > 2 1 1 0
> > 3 0 1 1
> >
> >
> > Anyone can help me out with this?
> >
> > Regards,
> >
> > Jon
> >
> > *** Sent via Devdex http://www.devdex.com ***
> > Don't just participate in USENET...get rewarded for it!


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 04:37 AM
--CELKO--
 
Posts: n/a
Default Re: Determine winner from two distinct lines

>> I have to determine the "standing" (WIN - TIE - LOSS) from
confrontations between two teams on a contest. The table matchResults
has fields [sic] cont_id, team_id and contest_result (int). <<

Please learn that a column is nothing whatsoever like a field before
you write any more code.

The basic problem is that your data model is fundamentally wrong. A
match has more attributes -- a date, two teams (not the same) and two
scores (them and us) at least. I will bet that EVERY answer you get
from other people will keep your crappy DDL and kludge such a table
for the Matches together inside a query Reject the kludges and fix
the **real** problem. Engineering heueristic: mop up the water, but
fix the leak.

CREATE TABLE Matches
(march_date DATETIME NOT NULL,
home_team INTEGER NOT NULL
REFERENCES Teams(team_nbr),
visitor_team INTEGER NOT NULL
REFERENCES Teams(team_nbr),
CHECK (home_team <> visitor_team)
home_score INTEGER NOT NULL,
visitor_score INTEGER NOT NULL);

I have a series of articles coming out shortly on splitting facts over
multiple tables, columns and rows. This is a classic example. Are you
a George Carlin fan? You are doing his baseball scores routine in
SQL!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 04:39 AM
Steve Kass
 
Posts: n/a
Default Re: Determine winner from two distinct lines

Isaac,

There is overhead, but with only two values of the folded value
per cont_id, it's probably not relevant here. In a different
situation, indexing the analog of
(cont_id, the computed column team_id*100000 + contest_result)
might help.

Analytic functions aren't in SQL Server 2000, but they have been announced
for SQL Server 2005, and I believe they follow the ANSI standard closely, as
Oracle does. I believe they are working in the public beta of SQL Server
2005 Express Edition, which is available now:

http://lab.msdn.microsoft.com/express/sql/default.aspx

The express edition, which will be free and freely distributable, is the
SQL Server 2005 engine minus some of the full version's rich toolset, and
restricted to smaller scale use: 1 CPU, 1 GB of memory, and a size limit
of 4 GB per database.

SK



Isaac Blank wrote:

> Very clever indeed. But this business of folding and unfolding is an
> overhead, too. I wonder if MSSQL has something similar to analythic
> fuctions, so one can write something like this (I am using Oracle syntax):
>
> SELECT cont_id, team_id, CASE WHEN contest_result < highest_score THEN
> 'loss' WHEN contest_result > lowest_score THEN 'win' ELSE 'tie' END
> FROM (
> SELECT cont_id, team_id, contest_result,
> MAX(contest_result) over (PARTITION BY cont_id ) highest_score,
> MIN(contest_result) over (PARTITION BY cont_id ) lowest_score
> FROM matchresults)
>
>
>
> "Steve Kass" <skass@drew.edu> wrote in message
> news:CMkJc.2885$mL5.1657@newsread1.news.pas.earthl ink.net...
>
>>John,
>>
>> Here's a sneaky solution that might be more efficient than the other
>>suggestions, since it doesn't require a self-join of matchResults. It

>
> will
>
>>work so long as the contest results are integers from 0 to 99999, but that
>>restriction can be widened.
>>
>>
>>CREATE TABLE matchResults (
>> cont_id int,
>> team_id int,
>> contest_result int,
>> primary key (cont_id, team_id)
>>)
>>insert into matchResults values (1,1,3)
>>insert into matchResults values (1,2,5)
>>insert into matchResults values (2,2,4)
>>insert into matchResults values (2,3,4)
>>insert into matchResults values (3,3,3)
>>insert into matchResults values (3,1,4)
>>go
>>
>>create table Two (
>> n int
>>)
>>insert into Two values(-1)
>>insert into Two values(1)
>>go
>>
>>create view results as
>>select
>> cont_id,
>> case when n = 1 then minx else maxx end/100000 as team_id,
>> case sign(n*(minx%100000 - maxx%100000))
>> when -1 then 'loss' when 0 then 'tie' else 'win' end as result
>>from (
>> select
>> cont_id,
>> min(team_id*100000 + contest_result) as minx,
>> max(team_id*100000 + contest_result) as maxx
>> from matchResults
>> group by cont_id
>>) M, Two
>>go
>>
>>select * from results
>>order by cont_id, team_id
>>
>>select *
>>from (
>> select
>> team_id,
>> count(case when result = 'win' then 1 end) as Win,
>> count(case when result = 'tie' then 1 end) as Tie,
>> count(case when result = 'loss' then 1 end) as Loss
>> from results
>> group by team_id
>>) S
>>order by Win - Loss desc
>>go
>>
>>drop view results
>>drop table Two
>>drop table matchResults
>>
>>-- Steve Kass
>>-- Drew University
>>-- Ref: 9EA47FEA-D8EE-47E9-8ED7-555BA50BF93A
>>
>>John Grenier wrote:
>>
>>>Hi,
>>>
>>>I have to determine the "standing" (WIN - TIE - LOSS) from
>>>confrontations between two teams on a contest. The table matchResults
>>>has fields cont_id, team_id and contest_result (int).
>>>
>>>TABLE matchResults
>>>cont_id team_id contest_result
>>>1 1 3
>>>1 2 5
>>>2 2 4
>>>2 3 4
>>>3 3 3
>>>3 1 4
>>>
>>>
>>>What I want is to create a view that determines the following
>>>information
>>>
>>>VIEW teamMatchStatus
>>>cont_id team_id cont_status
>>>1 1 loss
>>>1 2 win
>>>2 2 tie
>>>2 3 tie
>>>3 3 loss
>>>3 1 win
>>>
>>>and eventualy calculate the standings
>>>
>>>TABLE standings
>>>team_id win tie loss
>>>1 1 0 1
>>>2 1 1 0
>>>3 0 1 1
>>>
>>>
>>>Anyone can help me out with this?
>>>
>>>Regards,
>>>
>>>Jon
>>>
>>>*** Sent via Devdex http://www.devdex.com ***
>>>Don't just participate in USENET...get rewarded for it!

>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 04:40 AM
Isaac Blank
 
Posts: n/a
Default Re: Determine winner from two distinct lines

Steve,

I guess was not clear enough. Of course packing two values into a single
column and ant then unpacking is not a big deal. What I view as overhead
are the steps of assembling the games and then disassembling them back into
original form. Goes against the non-procedural nature of SQL.


"Steve Kass" <skass@drew.edu> wrote in message
news:Tt1Kc.5777$Qu5.2859@newsread2.news.pas.earthl ink.net...
> Isaac,
>
> There is overhead, but with only two values of the folded value
> per cont_id, it's probably not relevant here. In a different
> situation, indexing the analog of
> (cont_id, the computed column team_id*100000 + contest_result)
> might help.
>
> Analytic functions aren't in SQL Server 2000, but they have been

announced
> for SQL Server 2005, and I believe they follow the ANSI standard closely,

as
> Oracle does. I believe they are working in the public beta of SQL Server
> 2005 Express Edition, which is available now:
>
> http://lab.msdn.microsoft.com/express/sql/default.aspx
>
> The express edition, which will be free and freely distributable, is the
> SQL Server 2005 engine minus some of the full version's rich toolset, and
> restricted to smaller scale use: 1 CPU, 1 GB of memory, and a size limit
> of 4 GB per database.
>
> SK
>
>
>
> Isaac Blank wrote:
>
> > Very clever indeed. But this business of folding and unfolding is an
> > overhead, too. I wonder if MSSQL has something similar to analythic
> > fuctions, so one can write something like this (I am using Oracle

syntax):
> >
> > SELECT cont_id, team_id, CASE WHEN contest_result < highest_score THEN
> > 'loss' WHEN contest_result > lowest_score THEN 'win' ELSE 'tie' END
> > FROM (
> > SELECT cont_id, team_id, contest_result,
> > MAX(contest_result) over (PARTITION BY cont_id ) highest_score,
> > MIN(contest_result) over (PARTITION BY cont_id ) lowest_score
> > FROM matchresults)
> >
> >
> >
> > "Steve Kass" <skass@drew.edu> wrote in message
> > news:CMkJc.2885$mL5.1657@newsread1.news.pas.earthl ink.net...
> >
> >>John,
> >>
> >> Here's a sneaky solution that might be more efficient than the other
> >>suggestions, since it doesn't require a self-join of matchResults. It

> >
> > will
> >
> >>work so long as the contest results are integers from 0 to 99999, but

that
> >>restriction can be widened.
> >>
> >>
> >>CREATE TABLE matchResults (
> >> cont_id int,
> >> team_id int,
> >> contest_result int,
> >> primary key (cont_id, team_id)
> >>)
> >>insert into matchResults values (1,1,3)
> >>insert into matchResults values (1,2,5)
> >>insert into matchResults values (2,2,4)
> >>insert into matchResults values (2,3,4)
> >>insert into matchResults values (3,3,3)
> >>insert into matchResults values (3,1,4)
> >>go
> >>
> >>create table Two (
> >> n int
> >>)
> >>insert into Two values(-1)
> >>insert into Two values(1)
> >>go
> >>
> >>create view results as
> >>select
> >> cont_id,
> >> case when n = 1 then minx else maxx end/100000 as team_id,
> >> case sign(n*(minx%100000 - maxx%100000))
> >> when -1 then 'loss' when 0 then 'tie' else 'win' end as result
> >>from (
> >> select
> >> cont_id,
> >> min(team_id*100000 + contest_result) as minx,
> >> max(team_id*100000 + contest_result) as maxx
> >> from matchResults
> >> group by cont_id
> >>) M, Two
> >>go
> >>
> >>select * from results
> >>order by cont_id, team_id
> >>
> >>select *
> >>from (
> >> select
> >> team_id,
> >> count(case when result = 'win' then 1 end) as Win,
> >> count(case when result = 'tie' then 1 end) as Tie,
> >> count(case when result = 'loss' then 1 end) as Loss
> >> from results
> >> group by team_id
> >>) S
> >>order by Win - Loss desc
> >>go
> >>
> >>drop view results
> >>drop table Two
> >>drop table matchResults
> >>
> >>-- Steve Kass
> >>-- Drew University
> >>-- Ref: 9EA47FEA-D8EE-47E9-8ED7-555BA50BF93A
> >>
> >>John Grenier wrote:
> >>
> >>>Hi,
> >>>
> >>>I have to determine the "standing" (WIN - TIE - LOSS) from
> >>>confrontations between two teams on a contest. The table matchResults
> >>>has fields cont_id, team_id and contest_result (int).
> >>>
> >>>TABLE matchResults
> >>>cont_id team_id contest_result
> >>>1 1 3
> >>>1 2 5
> >>>2 2 4
> >>>2 3 4
> >>>3 3 3
> >>>3 1 4
> >>>
> >>>
> >>>What I want is to create a view that determines the following
> >>>information
> >>>
> >>>VIEW teamMatchStatus
> >>>cont_id team_id cont_status
> >>>1 1 loss
> >>>1 2 win
> >>>2 2 tie
> >>>2 3 tie
> >>>3 3 loss
> >>>3 1 win
> >>>
> >>>and eventualy calculate the standings
> >>>
> >>>TABLE standings
> >>>team_id win tie loss
> >>>1 1 0 1
> >>>2 1 1 0
> >>>3 0 1 1
> >>>
> >>>
> >>>Anyone can help me out with this?
> >>>
> >>>Regards,
> >>>
> >>>Jon
> >>>
> >>>*** Sent via Devdex http://www.devdex.com ***
> >>>Don't just participate in USENET...get rewarded for it!

> >
> >


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 04:41 AM
Steve Kass
 
Posts: n/a
Default Re: Determine winner from two distinct lines



Isaac Blank wrote:

> Steve,
>
> I guess was not clear enough. Of course packing two values into a single
> column and ant then unpacking is not a big deal. What I view as overhead
> are the steps of assembling the games and then disassembling them back into
> original form. Goes against the non-procedural nature of SQL.


It does indeed, but but sometimes funny-looking shoes fit just fine.

SK

>
>
> "Steve Kass" <skass@drew.edu> wrote in message
> news:Tt1Kc.5777$Qu5.2859@newsread2.news.pas.earthl ink.net...
>
>>Isaac,
>>
>> There is overhead, but with only two values of the folded value
>>per cont_id, it's probably not relevant here. In a different
>>situation, indexing the analog of
>>(cont_id, the computed column team_id*100000 + contest_result)
>>might help.
>>
>> Analytic functions aren't in SQL Server 2000, but they have been

>
> announced
>
>>for SQL Server 2005, and I believe they follow the ANSI standard closely,

>
> as
>
>>Oracle does. I believe they are working in the public beta of SQL Server
>>2005 Express Edition, which is available now:
>>
>>http://lab.msdn.microsoft.com/express/sql/default.aspx
>>
>>The express edition, which will be free and freely distributable, is the
>>SQL Server 2005 engine minus some of the full version's rich toolset, and
>>restricted to smaller scale use: 1 CPU, 1 GB of memory, and a size limit
>>of 4 GB per database.
>>
>>SK
>>
>>
>>
>>Isaac Blank wrote:
>>
>>
>>>Very clever indeed. But this business of folding and unfolding is an
>>>overhead, too. I wonder if MSSQL has something similar to analythic
>>>fuctions, so one can write something like this (I am using Oracle

>
> syntax):
>
>>>SELECT cont_id, team_id, CASE WHEN contest_result < highest_score THEN
>>>'loss' WHEN contest_result > lowest_score THEN 'win' ELSE 'tie' END
>>>FROM (
>>>SELECT cont_id, team_id, contest_result,
>>>MAX(contest_result) over (PARTITION BY cont_id ) highest_score,
>>>MIN(contest_result) over (PARTITION BY cont_id ) lowest_score
>>>FROM matchresults)
>>>
>>>
>>>
>>>"Steve Kass" <skass@drew.edu> wrote in message
>>>news:CMkJc.2885$mL5.1657@newsread1.news.pas.ear thlink.net...
>>>
>>>
>>>>John,
>>>>
>>>> Here's a sneaky solution that might be more efficient than the other
>>>>suggestions, since it doesn't require a self-join of matchResults. It
>>>
>>>will
>>>
>>>
>>>>work so long as the contest results are integers from 0 to 99999, but

>
> that
>
>>>>restriction can be widened.
>>>>
>>>>
>>>>CREATE TABLE matchResults (
>>>> cont_id int,
>>>> team_id int,
>>>> contest_result int,
>>>> primary key (cont_id, team_id)
>>>>)
>>>>insert into matchResults values (1,1,3)
>>>>insert into matchResults values (1,2,5)
>>>>insert into matchResults values (2,2,4)
>>>>insert into matchResults values (2,3,4)
>>>>insert into matchResults values (3,3,3)
>>>>insert into matchResults values (3,1,4)
>>>>go
>>>>
>>>>create table Two (
>>>> n int
>>>>)
>>>>insert into Two values(-1)
>>>>insert into Two values(1)
>>>>go
>>>>
>>>>create view results as
>>>>select
>>>> cont_id,
>>>> case when n = 1 then minx else maxx end/100000 as team_id,
>>>> case sign(n*(minx%100000 - maxx%100000))
>>>> when -1 then 'loss' when 0 then 'tie' else 'win' end as result
>>>
>>>>from (
>>>
>>>> select
>>>> cont_id,
>>>> min(team_id*100000 + contest_result) as minx,
>>>> max(team_id*100000 + contest_result) as maxx
>>>> from matchResults
>>>> group by cont_id
>>>>) M, Two
>>>>go
>>>>
>>>>select * from results
>>>>order by cont_id, team_id
>>>>
>>>>select *
>>>
>>>>from (
>>>
>>>> select
>>>> team_id,
>>>> count(case when result = 'win' then 1 end) as Win,
>>>> count(case when result = 'tie' then 1 end) as Tie,
>>>> count(case when result = 'loss' then 1 end) as Loss
>>>> from results
>>>> group by team_id
>>>>) S
>>>>order by Win - Loss desc
>>>>go
>>>>
>>>>drop view results
>>>>drop table Two
>>>>drop table matchResults
>>>>
>>>>-- Steve Kass
>>>>-- Drew University
>>>>-- Ref: 9EA47FEA-D8EE-47E9-8ED7-555BA50BF93A
>>>>
>>>>John Grenier wrote:
>>>>
>>>>
>>>>>Hi,
>>>>>
>>>>>I have to determine the "standing" (WIN - TIE - LOSS) from
>>>>>confrontations between two teams on a contest. The table matchResults
>>>>>has fields cont_id, team_id and contest_result (int).
>>>>>
>>>>>TABLE matchResults
>>>>>cont_id team_id contest_result
>>>>>1 1 3
>>>>>1 2 5
>>>>>2 2 4
>>>>>2 3 4
>>>>>3 3 3
>>>>>3 1 4
>>>>>
>>>>>
>>>>>What I want is to create a view that determines the following
>>>>>information
>>>>>
>>>>>VIEW teamMatchStatus
>>>>>cont_id team_id cont_status
>>>>>1 1 loss
>>>>>1 2 win
>>>>>2 2 tie
>>>>>2 3 tie
>>>>>3 3 loss
>>>>>3 1 win
>>>>>
>>>>>and eventualy calculate the standings
>>>>>
>>>>>TABLE standings
>>>>>team_id win tie loss
>>>>>1 1 0 1
>>>>>2 1 1 0
>>>>>3 0 1 1
>>>>>
>>>>>
>>>>>Anyone can help me out with this?
>>>>>
>>>>>Regards,
>>>>>
>>>>>Jon
>>>>>
>>>>>*** Sent via Devdex http://www.devdex.com ***
>>>>>Don't just participate in USENET...get rewarded for it!
>>>
>>>

>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:00 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com