clear markdown compare help donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 497175 fiddles created (9403 in the last week).

CREATE TABLE #Example ( UniqueID INT IDENTITY(1,1) , GroupID INT , GroupDate DATETIME , UniqueDate DATETIME ) CREATE CLUSTERED INDEX [CX_1] ON [#Example] ( [UniqueID] ASC ) SET NOCOUNT ON --Populate some test data DECLARE @i INT = 0, @j INT = 5, @UniqueDate DATETIME, @GroupDate DATETIME WHILE @i < 10000 BEGIN IF((@i + @j)%173 = 0) BEGIN SET @UniqueDate = GETDATE()+@i+5 END ELSE BEGIN SET @UniqueDate = GETDATE()+@i END SET @GroupDate = GETDATE()+(@j-1) INSERT INTO #Example (GroupID, GroupDate, UniqueDate) VALUES (@j, @GroupDate, @UniqueDate) SET @i = @i + 1 IF (@i % 5 = 0) BEGIN SET @j = @j+5 END END SET NOCOUNT OFF CREATE NONCLUSTERED INDEX [IX_2_4_3] ON [#Example] ( [GroupID] ASC, [UniqueDate] ASC, [GroupDate] ASC ) INCLUDE ([UniqueID])
 hidden batch(es)


set statistics xml on; -- show execution plan CREATE TABLE #Dummy (a integer NOT NULL, INDEX DummyCC CLUSTERED COLUMNSTORE); -- Identify any UniqueDates that are greater than the GroupDate within their GroupID SELECT calc_maxUD.UniqueID, calc_maxUD.GroupID, calc_maxUD.GroupDate, calc_maxUD.UniqueDate FROM ( SELECT E.UniqueID, E.GroupID, E.GroupDate, E.UniqueDate, maxUniqueDate = MAX(UniqueDate) OVER ( PARTITION BY GroupID) FROM #Example AS E LEFT JOIN #Dummy AS D ON 1 = 0 ) AS calc_maxUD WHERE calc_maxUD.maxUniqueDate > calc_maxUD.GroupDate AND calc_maxUD.maxUniqueDate = calc_maxUD.UniqueDate; DROP TABLE #Example, #Dummy;
UniqueID GroupID GroupDate UniqueDate
172 175 18/02/2020 04:17:17 20/02/2020 04:17:17
260 260 13/05/2020 04:17:17 18/05/2020 04:17:17
431 435 04/11/2020 04:17:17 05/11/2020 04:17:17
519 520 28/01/2021 04:17:17 01/02/2021 04:17:17
778 780 15/10/2021 04:17:17 18/10/2021 04:17:17
1037 1040 02/07/2022 04:17:17 04/07/2022 04:17:17
1125 1125 25/09/2022 04:17:17 30/09/2022 04:17:17
1296 1300 19/03/2023 04:17:17 20/03/2023 04:17:17
1384 1385 12/06/2023 04:17:17 16/06/2023 04:17:17
1643 1645 27/02/2024 04:17:17 01/03/2024 04:17:17
1902 1905 13/11/2024 04:17:17 15/11/2024 04:17:17
1990 1990 06/02/2025 04:17:17 11/02/2025 04:17:17
2161 2165 31/07/2025 04:17:17 01/08/2025 04:17:17
2249 2250 24/10/2025 04:17:17 28/10/2025 04:17:17
2508 2510 11/07/2026 04:17:17 14/07/2026 04:17:17
2767 2770 28/03/2027 04:17:17 30/03/2027 04:17:17
2855 2855 21/06/2027 04:17:17 26/06/2027 04:17:17
3026 3030 13/12/2027 04:17:17 14/12/2027 04:17:17
3114 3115 07/03/2028 04:17:17 11/03/2028 04:17:17
3373 3375 22/11/2028 04:17:17 25/11/2028 04:17:17
3632 3635 09/08/2029 04:17:17 11/08/2029 04:17:17
3720 3720 02/11/2029 04:17:17 07/11/2029 04:17:17
3891 3895 26/04/2030 04:17:17 27/04/2030 04:17:17
3979 3980 20/07/2030 04:17:17 24/07/2030 04:17:17
4238 4240 06/04/2031 04:17:17 09/04/2031 04:17:17
4497 4500 22/12/2031 04:17:17 24/12/2031 04:17:17
4585 4585 16/03/2032 04:17:17 21/03/2032 04:17:17
4756 4760 07/09/2032 04:17:17 08/09/2032 04:17:17
4844 4845 01/12/2032 04:17:17 05/12/2032 04:17:17
5103 5105 18/08/2033 04:17:17 21/08/2033 04:17:17
5362 5365 05/05/2034 04:17:17 07/05/2034 04:17:17
5450 5450 29/07/2034 04:17:17 03/08/2034 04:17:17
5621 5625 20/01/2035 04:17:17 21/01/2035 04:17:17
5709 5710 15/04/2035 04:17:17 19/04/2035 04:17:17
5968 5970 31/12/2035 04:17:17 03/01/2036 04:17:17
6227 6230 16/09/2036 04:17:17 18/09/2036 04:17:17
6315 6315 10/12/2036 04:17:17 15/12/2036 04:17:17
6486 6490 03/06/2037 04:17:17 04/06/2037 04:17:17
6574 6575 27/08/2037 04:17:17 31/08/2037 04:17:17
6833 6835 14/05/2038 04:17:17 17/05/2038 04:17:17
7092 7095 29/01/2039 04:17:17 31/01/2039 04:17:17
7180 7180 24/04/2039 04:17:17 29/04/2039 04:17:17
7351 7355 16/10/2039 04:17:17 17/10/2039 04:17:17
7439 7440 09/01/2040 04:17:17 13/01/2040 04:17:17
7698 7700 25/09/2040 04:17:17 28/09/2040 04:17:17
7957 7960 12/06/2041 04:17:17 14/06/2041 04:17:17
8045 8045 05/09/2041 04:17:17 10/09/2041 04:17:17
8216 8220 27/02/2042 04:17:17 28/02/2042 04:17:17
8304 8305 23/05/2042 04:17:17 27/05/2042 04:17:17
8563 8565 07/02/2043 04:17:17 10/02/2043 04:17:17
8822 8825 25/10/2043 04:17:17 27/10/2043 04:17:17
8910 8910 18/01/2044 04:17:17 23/01/2044 04:17:17
9081 9085 11/07/2044 04:17:17 12/07/2044 04:17:17
9169 9170 04/10/2044 04:17:17 08/10/2044 04:17:17
9428 9430 21/06/2045 04:17:17 24/06/2045 04:17:17
9687 9690 08/03/2046 04:17:17 10/03/2046 04:17:17
9775 9775 01/06/2046 04:17:17 06/06/2046 04:17:17
9946 9950 23/11/2046 04:17:17 24/11/2046 04:17:17
Microsoft SQL Server 2005 XML Showplan
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.518" Build="13.0.5366.0"><BatchSequence><Batch><Statements><StmtSimple StatementText="SELECT&#xa; calc_maxUD.UniqueID,&#xa; calc_maxUD.GroupID,&#xa; calc_maxUD.GroupDate,&#xa; calc_maxUD.UniqueDate&#xa;FROM &#xa;(&#xa;&#x9;SELECT&#xa; E.UniqueID,&#xa; E.GroupID,&#xa; E.GroupDate,&#xa; E.UniqueDate,&#xa; maxUniqueDate = MAX(UniqueDate) OVER (&#xa; PARTITION BY GroupID)&#xa;&#x9;FROM #Example AS E&#xa; LEFT JOIN #Dummy AS D &#xa; ON 1 = 0&#xa;) AS calc_maxUD&#xa;WHERE &#xa; calc_maxUD.maxUniqueDate &gt; calc_maxUD.GroupDate&#xa;&#x9;AND calc_maxUD.maxUniqueDate = calc_maxUD.UniqueDate" StatementId="1" StatementCompId="3" StatementType="SELECT" RetrievedFromCache="false" StatementSubTreeCost="0.0537094" StatementEstRows="547.723" SecurityPolicyApplied="false" StatementOptmLevel="FULL" QueryHash="0x539D12E7CD2A1C44" QueryPlanHash="0x7F9973847EF0C67B" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" CardinalityEstimationModelVersion="130"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"></StatementSetOptions><QueryPlan DegreeOfParallelism="0" MemoryGrant="1024" NonParallelPlanReason="NoParallelPlansInDesktopOrExpressEdition" CachedPlanSize="24" CompileTime="15" CompileCPU="15" CompileMemory="256"><Warnings><MemoryGrantWarning GrantWarningKind="Excessive Grant" RequestedMemory="1024" GrantedMemory="1024" MaxUsedMemory="16"></MemoryGrantWarning></Warnings><MemoryGrantInfo SerialRequiredMemory="8" SerialDesiredMemory="16" RequiredMemory="8" DesiredMemory="16" RequestedMemory="1024" GrantWaitTime="0" GrantedMemory="1024" MaxUsedMemory="16" MaxQueryMemory="648496"></MemoryGrantInfo><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="838808" EstimatedPagesCached="52425" EstimatedAvailableDegreeOfParallelism="1" MaxCompileMemory="1811840"></OptimizerHardwareDependentProperties><OptimizerStatsUsage><StatisticsInfo LastUpdate="2019-08-28T04:17:17.57" ModificationCount="0" SamplingPercent="100" Statistics="[IX_2_4_3]" Table="[#Example____________________________________________________________________________________________________________00000000000F]" Schema="[dbo]" Database="[tempdb]"></StatisticsInfo></OptimizerStatsUsage><TraceFlags IsCompileTime="1"><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><TraceFlags IsCompileTime="0"><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><QueryTimeStats ElapsedTime="0" CpuTime="0"></QueryTimeStats><RelOp NodeId="0" PhysicalOp="Filter" LogicalOp="Filter" EstimateRows="547.723" EstimateIO="0" EstimateCPU="0.00088" AvgRowSize="31" EstimatedTotalSubtreeCost="0.0537094" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Batch"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupDate"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="58" Batches="12" ActualExecutionMode="Batch" ActualElapsedms="0" ActualCPUms="0" ActualScans="0" ActualLogicalReads="0" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualEndOfScans="0" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><Filter StartupExpression="0"><RelOp NodeId="1" PhysicalOp="Window Aggregate" LogicalOp="Window Aggregate" EstimateRows="10000" EstimateIO="0" EstimateCPU="0.00314" AvgRowSize="39" EstimatedTotalSubtreeCost="0.0528294" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Batch"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupDate"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference><ColumnReference Column="Expr1004"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="10000" Batches="12" ActualExecutionMode="Batch" ActualElapsedms="0" ActualCPUms="0" ActualScans="0" ActualLogicalReads="0" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualEndOfScans="0" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><WindowAggregate><DefinedValues><DefinedValue><ColumnReference Column="Expr1004"></ColumnReference><ScalarOperator ScalarString="MAX([tempdb].[dbo].[#Example].[UniqueDate] as [E].[UniqueDate])"><Aggregate Distinct="0" AggType="MAX"><ScalarOperator><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference></Identifier></ScalarOperator></Aggregate></ScalarOperator></DefinedValue></DefinedValues><RelOp NodeId="2" PhysicalOp="Index Scan" LogicalOp="Index Scan" EstimateRows="10000" EstimatedRowsRead="10000" EstimateIO="0.0305324" EstimateCPU="0.011157" AvgRowSize="31" EstimatedTotalSubtreeCost="0.0416894" TableCardinality="10000" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupID"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupDate"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="10000" Batches="12" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualScans="1" ActualLogicalReads="40" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualRowsRead="10000" ActualEndOfScans="0" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueID"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupID"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupDate"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference></DefinedValue></DefinedValues><Object Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Index="[IX_2_4_3]" Alias="[E]" IndexKind="NonClustered" Storage="RowStore"></Object></IndexScan></RelOp></WindowAggregate></RelOp><Predicate><ScalarOperator ScalarString="[Expr1004]&gt;[tempdb].[dbo].[#Example].[GroupDate] as [E].[GroupDate] AND [Expr1004]=[tempdb].[dbo].[#Example].[UniqueDate] as [E].[UniqueDate]"><Logical Operation="AND"><ScalarOperator><Compare CompareOp="GT"><ScalarOperator><Identifier><ColumnReference Column="Expr1004"></ColumnReference></Identifier></ScalarOperator><ScalarOperator><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="GroupDate"></ColumnReference></Identifier></ScalarOperator></Compare></ScalarOperator><ScalarOperator><Compare CompareOp="EQ"><ScalarOperator><Identifier><ColumnReference Column="Expr1004"></ColumnReference></Identifier></ScalarOperator><ScalarOperator><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#Example]" Alias="[E]" Column="UniqueDate"></ColumnReference></Identifier></ScalarOperator></Compare></ScalarOperator></Logical></ScalarOperator></Predicate></Filter></RelOp></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>
 hidden batch(es)