title: SQL Case Statement with a Subselect
body: <pre><code>   select case when exists(
                                select top 1 1 
                                from dg_world_records wr (nolock) 
                                where wr.gametypeid = 4 
                                and wr.playerid = 1
                             ) 
      then totaltime = (select min(totaltime) from dg_world_records (nolock) where playerid = 1 and gametypeid = 4)
      else totaltime = (select max(totaltime) from dg_world_records (nolock) where gametypeid = 4)
      end
  </code></pre>
  
  <p>My guess is that this can't be done, but I'm trying to do a subselect within a sql case statement. Is this possible? </p>
  
  <p>I'm trying to do a lookup based of finding or not finding a value in a table. I can do this by breaking up the statement, but I was wondering if it's possible to do this in a single statement. </p>
  
  <p>Here is the table schema:</p>
  
  <pre><code>CREATE TABLE [dbo].[DG_WORLD_RECORDS](
  [WorldRecordId] [int] IDENTITY(1,1) NOT NULL,
  [GameTypeId] [int] NOT NULL,
  [PlayerId] [int] NOT NULL,
  [NumberOfValues] [int] NOT NULL,
  [TotalTime] [int] NOT NULL,
  [DateOfRecord] [datetime] NOT NULL,
  [GameId] [int] NULL,
  [UTCInserted] [datetime] NOT NULL CONSTRAINT [DF_DG_WORLD_RECORDSII_UTCInserted]  DEFAULT (getutcdate()),
  [UTCUpdated] [datetime] NOT NULL CONSTRAINT [DF_DG_WORLD_RECORDSII_UTCUpdated]  DEFAULT (getutcdate()),
  [SrvName] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_DG_WORLD_RECORDSII_SrvName]  DEFAULT (@@servername),
  </code></pre>
  
  <p>CONSTRAINT [PK_DG_WORLD_RECORDS] PRIMARY KEY CLUSTERED 
  (
      [WorldRecordId] ASC
  )
  ) </p>