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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:36:04+00:00 2026-05-27T11:36:04+00:00

I am trying to write a query that would select data from multiple lines

  • 0

I am trying to write a query that would select data from multiple lines depending if there is a newer value. This has been giving me headaches for a long time already. The table is structured this way:

UniqID ProblemID  DateTime   Status PersonID ResponsibleID ActionID Comment  Deadline    PartID
15589   15589    01/16 12:11   0       25         48          12      bla1   01/16 12:11  T9865
15592   15589    01/16 12:25   1       48         48          105     bla2   null         null
15601   15589    01/16 13:08   3       56         null        195     bla3   null         N2654
15641   15589    01/17 18:02   3       11         23          35      null   01/18 15:00  null
15705   15589    01/18 10:24   5       23         null        255     bla4   null         null

This is a mistake log and I need to perform several slightly different queries on this data.

  • e.g. if person 48 was involved with this mistake (15589) as PersonID or as ResponsibleID.

This query would have to return

    ProblemID (first)DateTime (last)Status (first)Person (last)Responsible (last)action (first)Comment (last)deadline (last)PartID        
      15589      01/16 12:11       5            25            23                255         bla1        01/18 15:00      N2654
  • e.g show all mistakes for the last 24hours
  • e.g show all mistakes where PartID T9865 was involved

I have made several attempts at this query, but the best that came out was a query that selects all stuff from the last line or the first one. I am actually struggling to figure out how to select the (last)ResponsibleID if it is not contained in the last line. The same for other columns obviously. The only solution I can think of right now is a simple select that would return me all the rows and just filter that necessary data in a foreach loop with PHP. Because selecting different values in multiple lines is just beyond my skill 🙂

Thanks in advance

  • 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-27T11:36:04+00:00Added an answer on May 27, 2026 at 11:36 am

    The only solution I can think of is to repeat the conditions in subselects. I see no other way to get to the first/last column values that are NOT NULL.

    SQL Statement

    ;WITH CurrentProblemID AS (
      SELECT  DISTINCT ProblemID
      FROM    Mistakes 
      WHERE   PersonID = 48 
              OR ResponsibleID = 48
    )
    
    SELECT  [ProblemID] = cpi.ProblemID
            , [(first) DateTime] = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND DateTime IS NOT NULL)
            , [(last) Status] = (SELECT Status FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Status IS NOT NULL))
            , [(first) Person] = (SELECT PersonID FROM Mistakes WHERE DateTime = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND PersonID IS NOT NULL))
            , [(last) Responsible] = (SELECT ResponsibleID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND ResponsibleID IS NOT NULL))
            , [(last) Action] = (SELECT ActionID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND ActionID IS NOT NULL))
            , [(first) Comment] = (SELECT Comment FROM Mistakes WHERE DateTime = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Comment IS NOT NULL))
            , [(last) Deadline] = (SELECT Deadline FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Deadline IS NOT NULL))
            , [(last) PartID] = (SELECT PartID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND PartID IS NOT NULL))
    FROM    CurrentProblemID cpi
    

    Test script

    ;WITH Mistakes AS (
      SELECT  *
      FROM    ( 
                VALUES
                  (15589, 15589, '01/16 12:11', 0, 25, 48, 12, 'bla1', '01/16 12:11', 'T9865')
                  , (15592, 15589, '01/16 12:25', 1, 48, 48, 105, 'bla2', null, null)
                  , (15601, 15589, '01/16 13:08', 3, 56, null, 195, 'bla3', null, 'N2654')
                  , (15641, 15589, '01/17 18:02', 3, 11, 23, 35, null, '01/18 15:00', null)
                  , (15705, 15589, '01/18 10:24', 5, 23, null, 255, 'bla4', null, null)
              ) AS v (UniqID, ProblemID, DateTime, Status, PersonID, ResponsibleID, ActionID, Comment, Deadline, PartID)
    )          
    , CurrentProblemID AS (
      SELECT  DISTINCT ProblemID
      FROM    Mistakes 
      WHERE   PersonID = 48 
              OR ResponsibleID = 48
    )
    
    SELECT  [ProblemID] = cpi.ProblemID
            , [(first) DateTime] = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND DateTime IS NOT NULL)
            , [(last) Status] = (SELECT Status FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Status IS NOT NULL))
            , [(first) Person] = (SELECT PersonID FROM Mistakes WHERE DateTime = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND PersonID IS NOT NULL))
            , [(last) Responsible] = (SELECT ResponsibleID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND ResponsibleID IS NOT NULL))
            , [(last) Action] = (SELECT ActionID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND ActionID IS NOT NULL))
            , [(first) Comment] = (SELECT Comment FROM Mistakes WHERE DateTime = (SELECT MIN(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Comment IS NOT NULL))
            , [(last) Deadline] = (SELECT Deadline FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND Deadline IS NOT NULL))
            , [(last) PartID] = (SELECT PartID FROM Mistakes WHERE DateTime = (SELECT MAX(DateTime) FROM Mistakes WHERE ProblemID = cpi.ProblemID AND PartID IS NOT NULL))
    FROM    CurrentProblemID cpi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write this query, that would calculate the average value of all
I'm trying to write a query that extracts and transforms data from a table
I'm trying to write an SQL query that would search within a CSV (or
I am trying to write a MySQL query that retrieves one record from table
I am trying to use Zend_Db_Select to write a select query that looks somewhat
Am trying to write a query that would output something similar to the last
I'm trying to write Linq query on this Products table based on FacetTypes that
I'm trying to write a query that will display the minimum value (lowest score)
I'm trying to write a query that will pull back the two most recent
I'm trying to write a query that updates rows in a table if a

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.