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

  • SEARCH
  • Home
  • 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 7082941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:07:01+00:00 2026-05-28T07:07:01+00:00

I have a T-SQL table that contains the following columns: Date, StationCode, HDepth, and

  • 0

I have a T-SQL table that contains the following columns: Date, StationCode, HDepth, and MaxDepth. Each row in the MaxDepth column is set to 0 by default. What I am trying to do is find the maximum HDepth by Date and StationCode, and update the MaxDepth to a column on these rows. I have written a SELECT statement to find where the maximums occur and it is:

SELECT StationCode, [Date], MAX(HDepth) AS Maximum FROM dbo.[DepthTable] GROUP BY [Date], StationCode

How could I put this query into an Update statement to set the MaxDepth to 1 on the rows that are returned by this query?

  • 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-28T07:07:02+00:00Added an answer on May 28, 2026 at 7:07 am

    You might try something like this:

    UPDATE a
    SET MaxDepth = 1
    FROM dbo.[DepthTable] AS a
        JOIN (
            -- Your original query
            SELECT StationCode, [Date], MAX(HDepth) AS Maximum
            FROM dbo.[DepthTable]
            GROUP BY [Date], StationCode
        ) AS b ON a.StationCode = b.StationCode
            AND a.[DATE] = b.[DATE]
            AND a.HDepth = b.Maximum -- Here we get only the max rows
    

    However, if a column is simply based upon other columns, then you might think about putting this logic into a view (to avoid update anomalies). The select for such a view might look like:

    SELECT a.[Date], a.StationCode, a.HDepth,
        CASE WHEN b.Maximum IS NULL THEN 0 ELSE 1 END AS MaxDepth
    FROM dbo.[DepthTable] AS a
        LEFT JOIN (
            -- Your original query
            SELECT StationCode, [Date], MAX(HDepth) AS Maximum
            FROM dbo.[DepthTable]
            GROUP BY [Date], StationCode
        ) AS b ON a.StationCode = b.StationCode
            AND a.[DATE] = b.[DATE]
            AND a.HDepth = b.Maximum -- Here we get only the max rows
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL Server that contains the following columns : Id
I have a table (T1) in t-sql with a column (C1) that contains almost
I have a SQL table with two columns Date and User and contains the
Working on postgres SQL. I have a table with a column that contains values
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a table in a sql server 2008 database that contains bunch of
I have a table which contains my ads that can be searched in sql-server-2008.
I have a table that contains a column called 'id' that is an INT
I have a table (lets say it has one column called 'colLanguage') that contains
I have a flattened table that contains columns that represent groups that need to

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.