当前位置: 代码迷 >> Sql Server >> !查询同列最大值并比较大小
  详细解决方案

!查询同列最大值并比较大小

热度:57   发布时间:2016-04-27 11:37:31.0
在线等!查询同列最大值并比较大小
有如下表结构
col1 col2
5 20
6 16
7 10
现在要在col2中查询数据,当查询出来的数据要小于同列中最大值的3/4时 col1取0,并按col1排序

col1 col2
6 16
5 20
0 10


------解决方案--------------------
SQL code
declare @t table (col1 int,col2 int)insert into @tselect 5,20 union allselect 6,16 union allselect 7,10select col1=case when col2<col3 then 0 else col1 end,col2 from (select *,(select max(col2)*0.75 from @t) col3 from @t b) aa order by 1 desc/*col1        col2----------- -----------6           165           200           10*/
------解决方案--------------------
SQL code
ALTER Procedure [dbo].[WTM_PersonalResult_GetAllPersonalResultViewByGameIDAndMatchGameName]@OrderName varchar(1),@GameID int,@MatchGameName nvarchar(5)AS  with ta as (SELECT  dbo.WTM_Order.Score,         dbo.WTM_GamePlan.PlayerNumber,         dbo.WTM_Player.PlayerName,         dbo.WTM_Team.TeamName,         dbo.WTM_PersonalResult.PersonalResultID,         dbo.WTM_PersonalResult.GamePlaneID,         dbo.WTM_PersonalResult.MatchGameID,         dbo.WTM_PersonalResult.Rank,         dbo.WTM_PersonalResult.TotalLoop,         dbo.WTM_PersonalResult.TotalTime,         dbo.WTM_PersonalResult.BestLoop,         dbo.WTM_PersonalResult.BestLoopTime,         dbo.WTM_PersonalResult.IsValid,         dbo.WTM_MatchGame.MatchGameName,         dbo.WTM_MatchGame.GameIDFROM         dbo.WTM_Player INNER JOIN             dbo.WTM_GamePlan ON dbo.WTM_Player.PlayerID = dbo.WTM_GamePlan.PlayerID INNER JOIN             dbo.WTM_PersonalResult ON dbo.WTM_GamePlan.GamePlanID = dbo.WTM_PersonalResult.GamePlaneID INNER JOIN             dbo.WTM_Team ON dbo.WTM_Player.TeamID = dbo.WTM_Team.TeamID INNER JOIN             dbo.WTM_Order ON dbo.WTM_PersonalResult.Rank = dbo.WTM_Order.Rank INNER JOIN             dbo.WTM_MatchGame ON dbo.WTM_PersonalResult.MatchGameID = dbo.WTM_MatchGame.MatchGameIDWhere [email protected] And [email protected] And [email protected])  Select PlayerNumber,PlayerName,TeamName,        PersonalResultID, GamePlaneID, MatchGameID,        TotalLoop, TotalTime, BestLoop, BestLoopTime,        IsValid, MatchGameName, GameID,             [Rank] =(case when TotalLoop<(select MAX(TotalLoop) From ta)*3/4 then 0 else [Rank] end)                From ta  ORDER BY IsValid desc,(Case When tb.Rank<>0 then tb.Rank else 254 end)
  相关解决方案