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
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?
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.
Here is the table schema:
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),
CONSTRAINT [PK_DG_WORLD_RECORDS] PRIMARY KEY CLUSTERED
(
[WorldRecordId] ASC
)
)
Don’t know why you are writing this crazy query, but to answer your question, yes it could be done, just move your assignment out of the case statement: