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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:17:04+00:00 2026-06-17T21:17:04+00:00

I have a table with some columns: WON param | machine | inputdate |

  • 0

I have a table with some columns:

WON   param      |  machine   |    inputdate     |  val
---|-------------|------------|------------------|--------|
18 | PAR.SPTDM.X1|    MM01    | 20/01/2013 12:43 |  2.5   |
18 | PAR.SPTDM.Y1|    MM01    | 20/01/2013 12:43 |  3,4   |
22 | PAR.SPTDM.X1|    MM01    | 22/01/2013 16:10 |  1.2   |
22 | PAR.SPTDM.Y1|    MM01    | 22/01/2013 16:10 |  1.7   |
33 | PAR.SPTDM.X1|    MM03    | 22/01/2013 16:13 |  2.34  |
33 | PAR.SPTDM.Y1|    MM03    | 22/01/2013 16:13 |  2,21  |
27 | PAR.LAS.PWR |    MM10    | 25/01/2013 08:14 |  100.5 |
14 | PAR.LAS.UV  |    MM10    | 18/01/2013 17:27 |  134.8 |
41 | PAR.LAS.UV  |    SLA4    | 27/01/2013 09:14 |  2,1   |
62 | PAR.LAS.UV  |    SLA5    | 27/01/2013 11:15 |  14.6  |

Some of these rows get duplicated (for reasons unknown to me, it’s the backend for a management system). Also the value column is a string, it needs conversion to numbers.

The database is readonly, and I want to pull in data into an Excel sheet based on a few conditions:

  • group by machine and param
  • filter on param: only 'PAR.SPTDM.%'
  • filter on machine: only 'MM%'
  • ignore val’s with a comma, convert the rest to numbers

This works without any problems using the following query:

SELECT DISTINCT t1.param, t1.machine, cast(t1.val as float), t1.inputdate
FROM dbbackend t1
WHERE (t1.machine Like 'MM%')
    AND (t1.param Like 'PAR.SPTDM.%')
    AND (t1.val<>'') And (t1.val not Like '%,%')

Now I want to only retrieve the records for each machine and each parameter (of interest) with the latest inputdate. First I tried

SELECT DISTINCT t1.param, t1.machine, max(cast(t1.val as float)), max(t1.inputdate)
FROM dbbackend t1
WHERE (t1.machine Like 'MM%')
    AND (t1.param Like 'PAR.SPTDM.%')
    AND (t1.val<>'') And (t1.val not Like '%,%')
GROUP BY machine, param

But this gave me val‘s which weren’t in the dataset, correct machine/param/inputdate, but wrong val.

Next try was

SELECT DISTINCT t1.param, t1.machine, cast(t1.val as float), t1.inputdate
FROM dbbackend t1
WHERE t1.WON IN
    (
    SELECT latestrec.WON FROM (
        SELECT DISTINCT max(t2.WON), t2.param, t2.machine, max(t2.inputdate)
        FROM dbbackend t2
        WHERE (t2.machine Like 'MM%')
            AND (t2.param Like 'PAR.SPTDM.%')
            AND (t2.val<>'') And (t2.val not Like '%,%')
            GROUP BY machine, param
        ) as latestrec
    )

But this wouldn’t work, MS Query told me

Could not add the table ‘(‘.

So can someone tell me why I get wrong values with the first call, and why the second isn’t working, or what is the correct method? I also suspect MS query to trip over that nested subquery :/

  • 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-17T21:17:06+00:00Added an answer on June 17, 2026 at 9:17 pm

    You could try this CTE with the ROW_NUMBER function:

    WITH x AS (SELECT t1.param, 
                    t1.machine, 
                    Cast(t1.val AS FLOAT) AS val, 
                    t1.inputdate, 
                    RN=Row_number() 
                         OVER( 
                           partition BY t1.machine, t1.param 
                           ORDER BY t1.inputdate DESC) 
             FROM   dbbackend t1 
             WHERE  ( t1.machine LIKE 'MM%' ) 
                    AND ( t1.param LIKE 'PAR.SPTDM.%' ) 
                    AND ( t1.val <> '' ) 
                    AND ( t1.val NOT LIKE '%,%' )) 
    SELECT param, 
           machine, 
           val, 
           inputdate 
    FROM   x 
    WHERE  rn = 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL table in which some columns, when viewed in SQL Server
I have a table of 26 million records, and wanted to add some columns
I have 2 tables with some columns and another table that I want its
I have a table that, some of its columns are unknown at compile time.
I have some data I am querying. The table is composed of two columns
I have a table with several columns. Sometimes some of these column fields may
I have some table DOCUMENTS that have a column TYPE_ID and a table named
I have table and this table contain result column with some entries. I just
I have the query to select some columns from two tables, which must contain
I have a table with a column 'A'. Some rows have 14 digits for

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.