Hi @Vivek D
Have you tried adding Indexes,like this:
CREATE /*UNIQUE*/ NONCLUSTERED INDEX ix_DollarAmount ON TableName (column1, column2, column3, column4, column5, column6,DollarAmount)
In addition,if your aggregate functions are MAX() or MIN(), you could also consider using Ranking Functions.
Check this sample:
CREATE TABLE #temp(COL_A INT,COL_B INT,COL_C VARCHAR(10))
INSERT INTO #temp VALUES(1,111,'abc'),(1,122,'def'),(1,133,'ghi'),(2,222,'jkl'),(2,211,'mno'),(3,333,'pqr')
--Solution 1
SELECT T1.*
FROM #temp T1 JOIN (SELECT COL_A,MAX(COL_B)COL_B
FROM #temp
GROUP BY COL_A )T2 ON T1.COL_A=T2.COL_A AND T1.COL_B=T2.COL_B
--Solution 2
;WITH CTE AS
(
SELECT *,ROW_NUMBER()OVER(PARTITION BY COL_A ORDER BY COL_B DESC) AS RowNum
FROM #temp
)
SELECT COL_A,COL_B,COL_C
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.