Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Finding top 3 rows based on condition - Urgent!

Author  Topic 

mrm23
Posting Yak Master

198 Posts

Posted - 2010-07-29 : 07:11:22
hi all,

i have a query which gives the percentage of time against each user.
I have 6 records in total. I must now select top 3 records from it in the last 30 days. i.e top 3 users in the last 30 days.

there is a timeuploaded column which gives this last 30 days.

this is my original query:

select tbl3.Worlds, CONVERT(FLOAT,tbl3.WorldSessionLength)/CONVERT(FLOAT,tbl4.TotalSessionLength) AS Perc from
(select (CASE WHEN world LIKE '%sunset%valley%' THEN 'SunsetValley'
WHEN world LIKE '%River%view%' THEN 'RiverView'
WHEN world LIKE '%China%' THEN 'China'
WHEN world LIKE '%France%' THEN 'France'
WHEN world LIKE '%Twin%Brook%' THEN 'TwinBrook'
WHEN world LIKE '%Egypt%' THEN 'Egypt'
ELSE 'Miscellaneous' END) AS Worlds,
SUM(tbl1.sessionlength) as WorldSessionLength from
(select world, sessionlength, telemetry_id from world inner join world_telemetry
on (world.id = world_telemetry.world_id)) as tbl1


group by (CASE WHEN world LIKE '%sunset%valley%' THEN 'SunsetValley'
WHEN world LIKE '%River%view%' THEN 'RiverView'
WHEN world LIKE '%China%' THEN 'China'
WHEN world LIKE '%France%' THEN 'France'
WHEN world LIKE '%Twin%Brook%' THEN 'TwinBrook'
WHEN world LIKE '%Egypt%' THEN 'Egypt'
ELSE 'Miscellaneous' END)) as tbl2,
(select SUM(sessionlength) as TotalSessionLength from sessioninfo) as tbl3

**********************************************************************

The TimeUploaded column comes from table sessioninfo.

now i need top 3 * from this query where timeuploaded in (dateadd(day,-30,timeuploaded))

i tried and got the result set but now when i am trying to tune it further i am not getting.

it is bit urgent. please help me.

mrm23
Posting Yak Master

198 Posts

Posted - 2010-07-29 : 07:26:14
Can anyone help me please? i am not able to figure out what is my mistake here...
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 07:52:01
Maybe this:
select top 3 * from
(
SELECT tbl3.worlds,
CONVERT(FLOAT, tbl3.worldsessionlength) /
CONVERT(FLOAT, tbl4.totalsessionlength) AS perc
FROM (SELECT ( CASE
WHEN world LIKE '%sunset%valley%' THEN 'SunsetValley'
WHEN world LIKE '%River%view%' THEN 'RiverView'
WHEN world LIKE '%China%' THEN 'China'
WHEN world LIKE '%France%' THEN 'France'
WHEN world LIKE '%Twin%Brook%' THEN 'TwinBrook'
WHEN world LIKE '%Egypt%' THEN 'Egypt'
ELSE 'Miscellaneous'
END ) AS worlds,
SUM(tbl1.sessionlength) AS worldsessionlength
FROM (SELECT world,
sessionlength,
telemetry_id
FROM world
INNER JOIN world_telemetry
ON ( world.id = world_telemetry.world_id )) AS tbl1
GROUP BY ( CASE
WHEN world LIKE '%sunset%valley%' THEN 'SunsetValley'
WHEN world LIKE '%River%view%' THEN 'RiverView'
WHEN world LIKE '%China%' THEN 'China'
WHEN world LIKE '%France%' THEN 'France'
WHEN world LIKE '%Twin%Brook%' THEN 'TwinBrook'
WHEN world LIKE '%Egypt%' THEN 'Egypt'
ELSE 'Miscellaneous'
END )) AS tbl2,
(SELECT SUM(sessionlength) AS totalsessionlength
FROM sessioninfo
where timeuploaded >= dateadd(day,-30,(dateadd(d,datediff(d,0,getdate()),0)))) AS tbl3
)dt
order by perc DESC



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -