Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Monday, March 19, 2012

Maximum value of mulitple columns

Hi all

Using sql server 2005, Im trying in a query to get the maximum value of multiple columns of a table for each of its records.

What im trying to get is the last date an index was used using the table sys.dm_db_usage_stats using the date fields (last_user_seek, last_user_update...).

I looked around the forum for a solution but those i found dont seem to apply really easily to a query.

Anyone got a suggestion?

Dale:

There have been a number of discussions about a similar issue; give a look to this post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=733186&SiteID=1

|||

Dale L.,

Try using new operators UNPIVOT and "CROSS APPLY".

Code Snippet

create table dbo.t1 (

pk int not null identity unique,

c1 int,

c2 int,

c3 int

)

go

insert into dbo.t1(c1, c2, c3) values(1, 2, 3)

insert into dbo.t1(c1, c2, c3) values(4, 6, 5)

insert into dbo.t1(c1, c2, c3) values(9, 7, 8)

go

select

a.pk,

b.max_value

from

dbo.t1 as a

cross apply

(

select

max(unpvt.[value]) as max_value

from

(

select

t.pk,

t.c1 as [1],

t.c2 as [2],

t.c3 as [3]

from

dbo.t1 as t

where

t.pk = a.pk

) as pvt

unpivot

([value] for [col] in ([1], [2], [3])) as unpvt

group by

unpvt.pk

) as b

go

drop table dbo.t1

go

AMB

Monday, February 20, 2012

Max() and Query...

Hi i got the following problem... i got a Table with multiple Scores for
People.... Something Like This
ID NAME POINTS
1 JOE 15
2 JOE 20
3 CHRIS 19
4 MATT 40
5 CHRIS 1
6 MATT 30
i need a query that will return the max of points that a player did along
with the name of the player and the id, if i didn't have the id i just
could group by, but the id is messing my group by, and returning me
everything... how can i do it '
Thanks.
AlexAlejandro K. wrote:
> Hi i got the following problem... i got a Table with multiple Scores
> for People.... Something Like This
> ID NAME POINTS
> 1 JOE 15
> 2 JOE 20
> 3 CHRIS 19
> 4 MATT 40
> 5 CHRIS 1
> 6 MATT 30
> i need a query that will return the max of points that a player did
> along with the name of the player and the id, if i didn't have the id
> i just could group by, but the id is messing my group by, and
> returning me everything... how can i do it '
> Thanks.
> Alex
Select ID,Name,maxpoints
FROM table t inner join (
SELECT Name,Max(Points) maxpoints
FROM table
GROUP BY Name) q
on t.Name=q.Name and t.points = q.maxpoints
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||Try using a derived table:
create table t1 ([ID] int identity, [NAME] varchar(16), POINTS int)
insert into t1 ([NAME], POINTS) values ('JOE', 15)
insert into t1 ([NAME], POINTS) values ('JOE', 20)
insert into t1 ([NAME], POINTS) values ('CHRIS', 19)
insert into t1 ([NAME], POINTS) values ('MATT', 40)
insert into t1 ([NAME], POINTS) values ('CHRIS', 1)
insert into t1 ([NAME], POINTS) values ('MATT', 30)
select a.[ID],
b.[NAME],
b.[POINTS]
from t1 a,
(select [NAME],
MAX(POINTS) as POINTS
from t1
group by [NAME]) b
where a.[NAME] = b.[NAME]
and a.POINTS = b.POINTS
-- Jesse
"Alejandro K." <AleK@.AleK.com> wrote in message
news:Xns95F8A6D6A6BD2AleKAleKcom@.207.46.248.16...
> Hi i got the following problem... i got a Table with multiple Scores for
> People.... Something Like This
> ID NAME POINTS
> 1 JOE 15
> 2 JOE 20
> 3 CHRIS 19
> 4 MATT 40
> 5 CHRIS 1
> 6 MATT 30
> i need a query that will return the max of points that a player did along
> with the name of the player and the id, if i didn't have the id i just
> could group by, but the id is messing my group by, and returning me
> everything... how can i do it '
> Thanks.
> Alex