Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:16:39+00:00 2026-05-29T04:16:39+00:00

In my table MeterReading, I have -Id(Primary) -ProjectMeterId -MeterRead -ReadDate -ReadCount I have multiple

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:16:40+00:00Added an answer on May 29, 2026 at 4:16 am
    ;WITH CTE
         AS (SELECT *,
                    MIN(ReadDate) OVER (PARTITION BY ProjectMeterId) AS Mn
             FROM   MeterReading)
    UPDATE CTE
    SET    ReadCount = 1234
    WHERE  ReadDate = Mn  
    

    Or

    UPDATE m1
    SET    ReadCount = 1234
    FROM   MeterReading m1
    WHERE  NOT EXISTS(SELECT *
                      FROM   MeterReading m2
                      WHERE  m1.ProjectMeterId = m2.ProjectMeterId
                             AND m2.ReadDate < m1.ReadDate)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table A has a number of rows, only some of which have (multiple) children
Table: Relatives emp_id dep_id (composite primary key) We have to restrict one employee to
Table: UserId, Value, Date. I want to get the UserId, Value for the max(Date)
table data of 2 columns category and subcategory i want to get a collection
table 1(ob): name,address table 2(address): dname,addr I need to update ob.address with address.addr when
Table users id - Primary Key, AI username - varchar (50) Table logins login_time
Table in MySQL have 1 problem column: creation_date . During inserting a new row
Table has 1 500 000 records, 1 250 000 of them have field =
table 'sample' contaning fields [AffiliateId] as a primary key, [AffiliateCode] L, [Name] , [AddressId]
Table Category (c) has a 1:many relationship with Table Question: a Category can have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.