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 8875981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:06:33+00:00 2026-06-14T19:06:33+00:00

I am trying to get the results of the query below: select distinct REQ_ID

  • 0

I am trying to get the results of the query below:

select distinct
       REQ_ID
     , ID
     , MAX(STEP) as step
     , SUBSTRING(p1.NAME,9, len(p1.NAME)) as _index_
     , p2.value as location

from HISTORY h

LEFT JOIN Parameter p1 
on     p1.WP_ID=h.ID
   AND (   (    p1.NAME like 'name_' 
            AND p1.VALUE like h.ID COLLATE DATABASE_DEFAULT ) 
        OR (    p1.NAME like 'name__' 
            AND p1.VALUE like h.ID COLLATE DATABASE_DEFAULT) )

LEFT JOIN Parameter p2 
on     p2.WP_ID=h.ID 
   AND p2.PA_NAME = 'Location' + (SUBSTRING(p1.NAME,9, len(p1.NAME)) ) 

WHERE h.ROLE = 'rock'

GROUP BY REQ_ID, ID, step, p1.name, p2.value

The problem is that the query is returning more than 1 results (in my case there are 6) and the MAX(STEP) value is not returning the max, I can see values like 0,1,3,0,1,2
Is there a way to get only the result with the max(step)? the step field is varchar(1)

  • 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-06-14T19:06:34+00:00Added an answer on June 14, 2026 at 7:06 pm

    When dealing with first record in a group, the often simplest approach is to use ROW_NUMBER(). Without knowing more about your schema and data, I can say that the following query will work, but it may be possible to simplifiy it further…

    WITH
      sequenced_data AS
    (
      SELECT
        REQ_ID,
        ID,
        ROW_NUMBER() OVER (PARTITION BY req_id, id ORDER BY step DESC) AS sequence_id,
        STEP,
        SUBSTRING(p1.NAME,9, len(p1.NAME)) as _index_,
        p2.value as location
      FROM
        HISTORY    h
      LEFT JOIN
        Parameter  p1
          ON  p1.WP_ID = h.ID
          AND (   (p1.NAME like 'name_' AND p1.VALUE  like h.ID COLLATE DATABASE_DEFAULT ) 
               OR (p1.NAME like 'name__' AND p1.VALUE  like h.ID COLLATE DATABASE_DEFAULT)
              )
      LEFT JOIN
        Parameter  p2
          ON  p2.WP_ID = h.ID
          AND p2.PA_NAME = 'Location' + (SUBSTRING(p1.NAME,9, len(p1.NAME)) ) 
      WHERE
        h.ROLE = 'rock'
      GROUP BY
        REQ_ID,
        ID,
        STEP,
        SUBSTRING(p1.NAME,9, len(p1.NAME)),
        p2.value
    )
    SELECT
      *
    FROM
      sequenced_data
    WHERE
      sequence_id = 1
    

    This will give only one row per req_id, id combination, and will always pick the row with the highest step value.

    NOTE: I don’t know if the GROUP BY is still needed. I left it in simply because you had it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the below code to show my query results. Unfortunately it's
I have been trying to get the results for the query below and add
I am trying the following query and get only zero results (I am supposed
I have a 3 tables that I'm trying to join and get distinct results.
I am trying get the values using SP.The query is below. create proc [dbo].[GetOrdersByUserID11]
I am trying to get the sql query below to work but I am
I'm trying to GROUP_CONCAT the results returned by the following query: SELECT CONCAT(p.product_name, (,
I'm trying to get a result of FOR XML query as a text. I've
I am trying to get the results of this code this way: title: Ben
I am using Entity Framework. I am trying to get the results from linq

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.