In my table MeterReading, I have
-Id(Primary)
-ProjectMeterId
-MeterRead
-ReadDate
-ReadCount
I have multiple entries for a given ProjectMeterId. I want to update ReadCount of only those rows where ReadDate is minimum (for a given ProjectMeterId) i.e.
Update MeterReading
set ReadCount = 1234
where (ReadDate is minimun for a given ProjectMeterId)
NOTE: There are many ProjectMeterId in the table and hence multiple values have to be updated. How should I do it in a query, without declaring any SP, declaring table variables and all?? Cause doing that way, it takes more than 10 minutes to update all entries.
This is what am doing right now:
UPDATE TTable
SET TTable.ReadCount= 222
From
(
Select * From MeterReading where Id in
(
Select Id From
(
SELECT Min(Id)as Id, MIN(ReadDate) as ReadDate, ProjectMeterId FROM MeterReading
WHERE ProjectMeterId IS NOT NULL AND ProjectId IS NOT NULL Group By ProjectMeterId
) as temp1
)
) TTable,
(
Select * From MeterReading where Id in
(
Select Id From
(
SELECT MIN(ReadDate) as ReadDate, ProjectMeterId FROM MeterReading
WHERE ProjectMeterId IS NOT NULL AND ProjectId IS NOT NULL Group By ProjectMeterId
) as temp2
)
) STable
Where STable.ProjectMeterId = TTable.ProjectMeterId
but Min(id) inside subQueries gives me that row which has minimum Id specific to a ProjectMeterId and not ReadDate.
What Should I Do?
Or